How do I target all elements in a document except a particular element name?
For example I want to exclude the terminate
elements. They can occur throughout the document.
<root> <terminate attr="1" /> <other> The brown fox jumps over the fence. <terminate> <b>stuff</b> </terminate> </other> </root>
I've tried using the not(..)
operator without success likely because I'm using it wrong.
And frankly trying to Google 'not' is tough!
As defined in the W3 XPath 1.0 Spec, " child::node() selects all the children of the context node, whatever their node type." This means that any element, text-node, comment-node and processing-instruction node children are selected by this node-test.
./* or * selects all child elements of the context node, regardless of name.
You need to wrap the expression that selects the a in parenthesis to group them in a node-set, then apply the predicate filter on position. That will find all a elements except the first one (since there is no a preceding the first one).
The following xpath should work
/root/*[not(self::terminate)]
Also, i think you can do it with this as well
/root/*[not(name()='terminate')]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With