I know this is a simple question, but I can't figure it out. Consider the following simple XML document:
<root>
<a></a>
<b></b>
<c></c>
<a></a>
<d></d>
<e></e>
<a></a>
<a></a>
</root>
What's the best way to select the nodes <b>
through <e>
using XPath?
I'm looking for something like
/root/*[not(a)]
(which does not do the trick)
By adding square brackets with index. By using position () method in xpath.
It defines a language to find information in an XML file. It is used to traverse elements and attributes of an XML document.
/root/*[not(self::a)]
Answering to add that in XPath 2.0, you can use except
:
/root/(* except a)
For XPath 1.0, Tomalak pointed out, this is the standard way to do it:
/root/*[not(self::a)]
By the way, if someone lands here trying to use this in XSLT 2.0 in a xsl:template/@match
attribute it won't work because @match
takes patterns which although look like XPath expressions, are not XPath expressions. The solution for XPath 1.0 would work in this case.
I realize this is an old question, but I recently ran into a similar problem and used the following xpath to solve it:
/root/*[not(name()='a')]
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