Reputation: 33
I have this XML file:
<xml>
<element>
<name>A</name>
<property no="1" value="OK" />
<property no="2" value="NO" />
<property no="3" value="OK" />
</element>
<element>
<name>B</name>
<property no="1" value="NO" />
<property no="2" value="NO" />
<property no="3" value="OK" />
<property no="4" value="OK" />
</element>
<element>
<name>C</name>
<property no="1" value="NO" />
<property no="2" value="NO" />
</element>
</xml>
I need all elements having "property" nodes with "value"="OK", but I want whole tree and only nodes which match.
I only managed to have either one thing or the other, not both.
So, I can have whole tree of elements having property nodes with value=OK:
//element[property/@value="OK"]
gives me:
<element>
<name>A</name>
<property no="1" value="OK"/>
<property no="2" value="NO"/>
<property no="3" value="OK"/>
</element>
<element>
<name>B</name>
<property no="1" value="NO"/>
<property no="2" value="NO"/>
<property no="3" value="OK"/>
<property no="4" value="OK"/>
</element>
or I can have all properties with value "OK":
//element/property[@value="OK"]
which gives me:
<property no="1" value="OK"/>
<property no="3" value="OK"/>
<property no="3" value="OK"/>
<property no="4" value="OK"/>
This is what I need, though:
<element>
<name>A</name>
<property no="1" value="OK" />
<property no="3" value="OK" />
</element>
<element>
<name>B</name>
<property no="3" value="OK" />
<property no="4" value="OK" />
</element>
Upvotes: 1
Views: 40
Reputation: 111591
I need all elements having "property" nodes with "value"="OK", but I want whole tree and only nodes which match.
I only managed to have either one thing or the other, not both.
The reason you're unable to achieve both results is that you are trying to do too much via XPath.
XPath is for selection; you're trying to do construction.
To construct or reconstruct the nodes that you select with XPath, you have to use the facilities of the hosting language (XSLT, Python, Java, etc). XPath alone is the wrong tool for rearranging that which has been selected.
Upvotes: 2