Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

fetching node values in groups using XPath

Tags:

java

xml

xpath

I have an XML of format

...
<parent>
   <child name='a' />
</parent>
<parent>
   <child name='b' />
   <child name='c' />
   <child name='d' />
</parent>
...

Now, i'm using XPath expression //parent/child[@name] to fetch the child node names. But I'm getting everything in a flat list, in this instance its a list of 4 nodes.

Is there a way (xpath expression) to fetch the nodes grouped by the parent as in [[a],[b,c,d]]

like image 500
Manoj Avatar asked Jan 22 '26 00:01

Manoj


1 Answers

Sounds like you need two loops, one on //parent, and then a sub query for child[@anme]. That should allow you to work with the child names in their groups. XPath will otherwise just return all matching nodes, irrespective of ancestry.

like image 175
Jon Egerton Avatar answered Jan 23 '26 14:01

Jon Egerton



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!