Sezi
Sezi

Reputation: 93

XPath retrieve values hierarchy

Having an XML document

<A id="first">
  <B id="second">
    <C id="third">
    <C id="fourth">
  </B>
  <B id="fifth">
    <C id="sixth">
  </B>
  <B id="seventh">
    ...
</A>

how can I list possible combinations of id attributes while maintaining node hierarchy in the response?

first,second,third
first,second,fourth
first,fifth,sixth
...

Eventually I'd like to execute the query over PostgreSQL database hosted XML document through XML2 extension [1], such that individual lines could be unnested into table rows

SELECT unnest(string_to_array(xpath_list(<column>, '<query>', ','), ','))
FROM <table>

yielding a table similar to

A B C
first second third
first second fourth
first fifth sixth

Not sure it's even possible through XPath as I was not able to find any relevant documentation but would like to consult the approach with comunity before I abandon the idea 🤷

[1] https://www.postgresql.org/docs/current/xml2.html

Upvotes: 0

Views: 47

Answers (1)

Martin Honnen
Martin Honnen

Reputation: 167446

XPath 3.1:

//C!string-join(ancestor-or-self::*/@id, ',') => string-join(codepoints-to-string(10))

gives a single string

first,second,third
first,second,fourth
nfirst,fifth,sixth

Upvotes: 0

Related Questions