Reputation: 917
<Root>
<SomeElement>
...
<Wanted>.....</Wanted>
<UnWanted>
<Wanted>.....</Wanted>
</UnWanted>
<Wanted>.....</Wanted>
...
</SomeElement>
</Root>
I want to select the "Wanted
" Elements which are inside "SomeElement
" at ANY level but not childs of "UnWanted
" Element.
With the XPath /Root/SomeElement//Wanted
I'm not able to exclude the childs of UnWanted
.
Upvotes: 5
Views: 1619
Reputation: 122364
You could try
/Root/SomeElement//Wanted[not(ancestor::UnWanted)]
This would exclude all Wanted
elements that are a child, grandchild, etc. (at any level) of an UnWanted
element. If you only want to exclude Wanted
elements that are a direct child of UnWanted
(but still include those that are grandchildren, etc.) then change ancestor::
to parent::
/Root/SomeElement//Wanted[not(parent::UnWanted)]
Upvotes: 3