Have a look at the following XML data:
<data>
<test color="red">Red text</test>
<test color="green">green</test>
</data>
Let's say I have several xml-documents with this structure in my database:
CREATE TABLE xmldata (
id bigserial not null,
documents xml)
Now I want to select all possible colors:
SELECT id, xpath('//test', xml) FROM xmldata;
But this returns a table with the id of each document and a text-array of the test-nodes. Furthermore, documents without any "test" node exist in the result as well - with an empty array {}
What I really want is a table like this:
| id | node |
| 1 | <test color="red">Red text</test> |
| 1 | <test color="green">green</test> |
What is the syntax I have to use?
I heard that xpath_table may be the function to use - but this function is marked as deprecated...
(The returned table has to have one line for each occurence of the node I searched for. The node itself maybe an xml-snippet, text or something else - isn't really important)