Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Child nodes to comma delimited string with Xpath?

Tags:

xml

xpath

I have XML that looks like this:

<node1>
 <item>hello</item>
 <item>world</item>
</node1>

I would like this to output the following using an XPath expression:

hello, world

Is this possible with XPath 1.0? I have been looking around but unable to find anything.

Thanks, Cinegod

like image 363
Cinegod Avatar asked Dec 10 '22 02:12

Cinegod


2 Answers

XPath 2.0 can do string-join(/node1/item, ', '). With XPath 1.0 you can't do that, you would need to use a host language like XSLT or a procedural language exposing an XPath API to iterate over nodes and concatenate values.

like image 175
Martin Honnen Avatar answered Jan 01 '23 10:01

Martin Honnen


In XPath 1.0 you can do that by using concat(), e.g.:

concat(node1/item[1], ", ", node1/item[2])
like image 24
kenorb Avatar answered Jan 01 '23 10:01

kenorb