How to do XML canonical comparison with PostgreSQL?

懵懂的女人 提交于 2019-12-23 03:24:05

问题


I need to compare XML values with PostgreSQL 9.X. Example:

 SELECT 1 FROM t WHERE xml1=xml2; -- error for XML datatype

but can do only with casting to text datatype,

 SELECT 1 FROM t WHERE xml1::text=xml2::text; -- OK, but is a text-comparison

that is not the best way to do this. I need a better comparison algorithm... Looking for it, I find that, perhaps, "Canonical XML" can be compared.

How to do "Canonical XML comparison" with PostgreSQL?


NOTE: PostgreSQL team assumes false premisse about XML comparison?

Many documents about XML comparison sugest that Canonical XML is the reference format for that operation. Ex. at Wikipedia they say,

According to the W3C, if two XML documents have the same canonical form, then the two documents are logically equivalent within the given application context.

But, PostgreSQL docs/9.2 says,

The xml data type is unusual in that it does not provide any comparison operators. This is because there is no well-defined and universally useful comparison algorithm for XML data.

Well, for me it is a false premise, and can be hiden a real lack of an important PostgreSQL issue.


回答1:


I contributed to the XML type implementation in PostgreSQL and probably wrote most of the documentation you are citing.

There are a number of reasons why this is currently the way it is:

  • The SQL standard specifies no comparison operator for type xml.
  • At the time the implementation was started, Canonical XML wasn't widely used and understood (at least by the people involved, arguably).
  • There are certain limitations where XML canonicalization doesn't work. Although these might be rarely seen in practice, this would yield the situation where some values of a data type can't be compared, which would lead to problems with indexing for example. (The NaN value of floating-point types is assigned an ordering position for similar reasons.)
  • It's still debatable whether comparison by canonicalization is appropriate for all uses and what users always want.

An implementation of an XML canonicalization function for optional use would certainly be welcome. I would actually like to see a separate xmlcanonical type, but that would be quite a bit more work.



来源:https://stackoverflow.com/questions/15393026/how-to-do-xml-canonical-comparison-with-postgresql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!