Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XPath to get all child elements except one with specific name?

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!

like image 879
John K Avatar asked Nov 08 '10 05:11

John K


People also ask

What is child :: In XPath?

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.

What does /* mean in XPath?

./* or * selects all child elements of the context node, regardless of name.

How do I ignore the first element in XPath?

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).


1 Answers

The following xpath should work

/root/*[not(self::terminate)]  

Also, i think you can do it with this as well

/root/*[not(name()='terminate')] 
like image 97
Jagmag Avatar answered Sep 22 '22 16:09

Jagmag