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

我想在任何级别选择“ Wanted”内部的“”元素,但不选择“ ”元素的子元素。SomeElementUnWanted

使用 XPath /Root/SomeElement//Wanted,我无法排除UnWanted.

4

1 回答 1

3

你可以试试

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

这将排除Wanted作为元素的子、孙等(在任何级别)的所有UnWanted元素。如果您只想排除直接子元素Wanted(但仍包括孙子元素等),则更改为UnWantedancestor::parent::

/Root/SomeElement//Wanted[not(parent::UnWanted)]
于 2013-02-27T10:58:29.273 回答