因此,我不知道该如何表达这个问题。我想要一个节点列表,并且只选择一个节点,而不选择嵌套节点。例如:
<root>
<my_element>
<my_element>
Some Text
</my_element>
</my_element>
</root>
我知道我已经可以通过使用此xpath做一些我想做的事情:
Context: /
xPath: descendant::my_element[not(ancestor::my_element)]
这将返回此结果集:
<root>
[<my_element>]
<my_element>
Some Text
</my_element>
[</my_element>]
</root>
那就是我想要的预期行为。但我希望能够将上下文更改为:
/my_element
并获得以下结果集:
<root>
<my_element>
[<my_element>]
Some Text
[</my_element>]
</my_element>
</root>
我已尽力查看xPath文档,但我没有提出太多建议。也许有人在这里可以提供一些见解?
谢谢!
编辑-
我希望能够选择一个my_element后代,该后代不是my_element的祖先(不包括上下文节点)。
再次编辑-
进一步解释。
我希望有一个选择
my_element
节点的xpath查询,只要该节点不是my_element
的子代即可。但是,如果将xpath上下文设置为my_element
节点,则我不希望该节点计入表达式。因此,即使xpath实际上是my_element
的子代,它也将匹配下一个my_element
节点。再次编辑-
这是更多示例。
<root>
<a>
<a>
<b>
<a>
Hello!
</a>
</b>
<a>
<b>
Hello Again
<a>
Sub
</a>
</b>
</a>
</a>
</a>
</root>
Context: /root/
Desire: Want to grab all A nodes, so long as they aren't a descendant of A
Result:
<root> == Context
[<a>]
<a>
<b>
<a>
Hello!
</a>
</b>
<a>
<b>
Hello Again
<a>
Sub
</a>
</b>
</a>
</a>
[</a>]
</root>
Context: /root/a/
Desire: Want to grab all A nodes, so long as they aren't a descendant of A, not including the context /root/a/
Result:
<root>
<a> == Context
[<a>]
<b>
<a>
Hello!
</a>
</b>
<a>
<b>
Hello Again
<a>
Sub
</a>
</b>
</a>
[</a>]
</a>
</root>
Context: /root/a/a/
Desire: Want to grab all A nodes, so long as they aren't a descendant of A, not including the context /root/a/a/
Result:
<root>
<a>
<a> == Context
<b>
[<a>]
Hello!
[</a>]
</b>
[<a>]
<b>
Hello Again
<a>
Sub
</a>
</b>
[</a>]
</a>
</a>
</root>
Context: /root/a/a/a/
Desire: Want to grab all A nodes, so long as they aren't a descendant of A, not including the context /root/a/a/a/
Result:
<root>
<a>
<a>
<b>
<a>
Hello!
</a>
</b>
<a> == Context
<b>
Hello Again
[<a>]
Sub
[</a>]
</b>
</a>
</a>
</a>
</root>
我希望这可以使我的愿望更加明确。谢谢大家在努力!
最佳答案
用:
//my_element[not(.//my_element)]
这将选择所有没有任何
my_element
后代的名为my_element
的元素。
关于xml - 捕获不是其他节点的后代的节点,当前上下文除外,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5614834/