This is my first foray into xpath
and I'm failing miserably.
Consider something like this:
<node1>
<node11>
<dependent>Retain</dependent>
<dependent polarity="positive">Retain</dependent>
<dependent polarity="negative">Discard</dependent>
</node11>
<dependent>Retain</dependent>
<dependent polarity="positive">Retain</dependent>
<dependent polarity="negative">Discard</dependent>
<somethingelse>Retain</somethingelse>
</node1>
I'm looking for an expression that will (while maintaining tree structure)
- return all nodes not named
dependent
, - return nodes named
dependent
that do not have an attribute namedpolarity
- return nodes named
dependent
with attributepolarity
set topositive
.
Above example would thus result in:
<node1>
<node11>
<dependent>Retain</dependent>
<dependent polarity="positive">Retain</dependent>
</node11>
<dependent>Retain</dependent>
<dependent polarity="positive">Retain</dependent>
<somethingelse>Retain</somethingelse>
</node1>
Many attempts at googleing and ChatGPLing have not produced what I want. Below is what I believe should work, but doesn't - polarity='negative'
nodes are retained!?
//*[not(self::dependent) or (descendant-or-self::dependent[not(@polarity)]) or (descendant-or-self::dependent[@polarity='positive'])]
Where do I err?