views:

687

answers:

1

This query fails:

SELECT xpath('/my/xpath/expr', my_xml)[1] FROM my_table

ERROR:  syntax error at or near "["

But this one works:

SELECT x[1] FROM
    (SELECT xpath('/my/xpath/expr', my_xml) as x FROM my_table) as ss

My xpath expression always returns only a single value, but the Postgres xpath function returns an array. I want to select the first value in the array. While the subselect works, it's pretty ugly.

Why doesn't the first query work, and is there a cleaner way to do this than the second query?

+4  A: 

How about this:

SELECT (xpath('/my/xpath/expr', my_xml))[1] FROM my_table;
Milen A. Radev
That was a remarkably simply solution! I guess when in doubt, use more parentheses.
DNS