I am trying to select all the <set>erase</set> elements such that if two or more elements have the <set>erase</set> in hierarchy (Ex: <b> and <d> both have <set>erase</set>) then only the element in parent node name has to be selected(ie <b> in this case).
Sample xml below:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<a>
<b>
<set>erase</set>
<d>
<set>erase</set>
</d>
</b>
<c>
<x></x>
</c>
<e>
<y>
<set>erase</set>
<q></q>
</y>
<z>
<p>
<set>erase</set>
</p>
</z>
</e>
</a>
When I use the query = //set[contains(.,'erase')] I get all <set>erase</set> of nodesList b,d,y,p in result set.
I need help in framing the query to select <set>erase</set> of b , y and p.
Here is the same solution:
One XPath expression that selects exactly the wanted elements is:
XSLT – based verification:
when this transformation is applied on the provided XML document:
The contained XPath expression is evaluated and the names of the selected elements are output — correctly and as expected:
If you need to select the
setchildren of the selected above elements, just append the above XPath expression with/set:Again, XSLT – based verification:
This transformation just evaluates the above XPath expression and copies to the output the correctly selected three
setelements: