XPath Selects all items that do not have a specific item in the ancestor list.

<Root>
  <SomeElement>
    ...
    <Wanted>.....</Wanted>
    <UnWanted>
      <Wanted>.....</Wanted>
    </UnWanted>
    <Wanted>.....</Wanted>
    ...
  </SomeElement>
</Root>

I want to select the " Wanted" elements inside " SomeElement" at ANY level, but not the child elements of the " UnWanted" element .

With XPath, /Root/SomeElement//WantedI cannot exclude children UnWanted.

+5
source share
1 answer

You can try

/Root/SomeElement//Wanted[not(ancestor::UnWanted)]

This excludes all elements Wantedthat are children, grandchildren, etc. (at any level) of the element UnWanted. If you want to exclude only those elements Wantedthat are a direct child UnWanted(but still include those that are grandchildren, etc.), change ancestor::toparent::

/Root/SomeElement//Wanted[not(parent::UnWanted)]
+3
source

All Articles