XPath1在SQL / XML中,count个元素返回空 [英] XPath1 at SQL/XML, count elements returns empty

查看:175
本文介绍了XPath1在SQL / XML中,count个元素返回空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 PostgreSQL 9 ,采用 XPath-1 用于 SQL / XML标准兼容性。



此查询正确,返回预期数据:

  SELECT xpath('// img',xhtm)FROM t 

但另一个,计数img元素,返回空(!),

  SELECT xpath('count(// img)',xhtm)FROM t 






注意 1: xhtm 字段内容示例,

 < html>< p>您好! < img src =1.png/>< / p>< img src =2.jpg/>< / html& 

注意 2:当然 array_length ('// img',xhtm),1)显示计数结果,但不是XPath计数。



注意 3:我不知道它是一个通用的SQL / XML特性,还是一个 PosgreSQL Server 9.0.5 特定的错误/问题。


< stackoverflow.com/a/10692439/1945651\">这是SO回答,这个问题显然是在Postgresql 9.2中解决。这个回答说明:


使用PostgreSQL 9.2,文档突然有一句话覆盖xpath函数:



如果XPath表达式返回标量值而不是节点集,则返回单元素数组。

只是我需要!所以关于这个问题,另一个有效的答案是:升级到PostgreSQL 9.2。


下面是我原始答案的其余部分: p>

这似乎是Postgresql的 xpath()函数中的一个已知限制 - 仅计算节点集返回任何东西



一些Google搜索显示了大约2年前的一些讨论,主要来自一个名为Florian Pflug的人: / p>

http://postgresql.1045698.n5.nabble.com/PATCH-Bug-in-XPATH-if-expression-returns-a-scalar-value-td4440233.html




他开发了一个补丁,该补丁应该修复这个问题,并附加到下面的线程(虽然我不完全确定是最新版本):



http://www.postgresql.org/message-id/FC60A4F5-5560-438C-97E8-B6D1FDE1CC2B@phlo.org




对于补丁的优点和缺点,似乎有一些讨论,所以我建议阅读这些线程,以了解涉及的内容:



http://www.postgresql.org /message-id/201106291957.04251.rsmogura@softperience.eu
http://www.postgresql.org/message-id/201106291934.23089.rsmogura@softperience.eu


I am using PostgreSQL 9, that adopts XPath-1 for the SQL/XML standard cumpliance.

This query is ok, returns the expected data:

 SELECT xpath('//img',xhtm) FROM t

but this other one, to count img elements, returns empty (!),

 SELECT xpath('count(//img)',xhtm) FROM t


NOTE 1: a xhtm field content sample,

 <html><p>Hello! <img src="1.png"/></p><img src="2.jpg"/></html>

NOTE 2: of course array_length(xpath('//img',xhtm),1) show the count result, but it is not a XPath counting.

NOTE 3: I don't know if it is a generic SQL/XML peculiarity, or a PosgreSQL Server 9.0.5 specific bug/problem.

解决方案

EDIT: As I noted in the comments below, and as indicated in this SO Answer, this issue was apparently addressed in Postgresql 9.2. That answer states:

With PostgreSQL 9.2 the documentation suddenly has one more sentence covering the xpath function:

If the XPath expression returns a scalar value rather than a node set, a single-element array is returned.

Just what I need! So in relation to the question another valid answer is: Upgrade to PostgreSQL 9.2.

Below is the rest of my original answer:

This seems to be a known limitation in Postgresql's xpath() function - only expressions that evaluate to a node-set return anything; XPath expressions that would return a scalar value just return an empty array.

Some Google searches reveal some discussion about this about 2 years ago primarily from a person named Florian Pflug:

http://postgresql.1045698.n5.nabble.com/PATCH-Bug-in-XPATH-if-expression-returns-a-scalar-value-td4440233.html


He has developed a patch that supposedly fixes the issue and that is attached to the following thread (though I'm not entirely sure this is the latest version):

http://www.postgresql.org/message-id/FC60A4F5-5560-438C-97E8-B6D1FDE1CC2B@phlo.org


There does seem to be some discussion about the pros and cons of his patch, so I'd suggest reading through these threads to be aware of what is involved:

http://www.postgresql.org/message-id/201106291957.04251.rsmogura@softperience.eu http://www.postgresql.org/message-id/201106291934.23089.rsmogura@softperience.eu

这篇关于XPath1在SQL / XML中,count个元素返回空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆