I'm using PostgreSQL 8.4 XPath (XML functions) feature. This is an adapted example found in docs:
SELECT xpath('/my:a/value[.>15]',
'<my:a xmlns:my="http://example.com">
<value>20</value>
<value>10</value>
<value>30</value>
</my:a>',
ARRAY[ARRAY['my', 'http://example.com']]);
This works fine, it returns a list of nodes correctly filtered with "value>15" condition:
xpath
xml[]
---------------------------------------
"{<value>20</value>,<value>30</value>}"
But when I try to use "sum" it returns an empty list instead of a scalar value:
SELECT xpath('sum(/my:a/value[.>15])',
...
result:
xpath
xml[]
-----
"{}"
Any suggestions?
Juan Ferreyra
I stumbled across the same problem and I am pleased to add another answer to the question:
With PostgreSQL 9.2 the documentation suddenly has one more sentence covering the xpath function:
Just what I need! So in relation to the question another valid answer is: Upgrade to PostgreSQL 9.2. In the moment of writing this, version 9.2 is just a beta, but I can confirm that this works:
Version details
Demonstration of solution
(This demonstration was made with Postgre 9.5, because originally I pasted the wrong code)
xpath
does not create new nodes, it only filters the currently existing ones. That's whatXSLT
is for.From the docs:
Since your XPath expression does not return a node set, there are not values in it and there is nothing to return.