select 具有给定祖先属性的所有叶节点的 xpath
xpath to select all leaf nodes with given ancestors attribute
我有一批 xml,其中每个标签都有一个属性 isOptional。这种 xml 的一个例子是
<node1 isOptional="False">
<node2 isOptional="True">
<node22 isOptional="False">
<node3 isOptional="False">text4</node3>
</node22>
</node2>
<node4 isOptional="False">
<node5 isOptional="False">
<node6 isOptional="True">text3</node6>
<node7 isOptional="False">
<node8 isOptional="True">text2</node8>
<node9 isOptional="False">text1</node9>
</node7>
</node5>
</node4>
</node1>
我需要将 xpath 写入 select 所有没有 @isOptional="True"
祖先的叶节点。对于这个例子,结果应该是:
<node9 isOptional="False">text1</node9>
其实我需要这样的东西:
//*[not(*) and @isOptional="False" and count(ancestor::*[@isOptional = "True"] = 0)]
你能帮我得到正确的 xpath 吗?
像这样的 XPath 应该可以工作:
//*[not(child::*) and not(ancestor-or-self::*[@isOptional = 'True'])]
我想你忘了提一个额外的要求。你说的:
I need to write xpath to select all leaf nodes which do not have ancessors with @isOptional="True"
将映射到以下 XPath 表达式:
//*[not(*) and not(ancestor::*[@isOptional = 'True'])]
^^^ "all"
^^^^^^^ "leaf nodes"
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ "no ancestor where
@isOptional equals 'True'"
这将产生(个别结果由 ------
分隔):
<node6 isOptional="True">text3</node6>
-----------------------
<node8 isOptional="True">text2</node8>
-----------------------
<node9 isOptional="False">text1</node9>
但我假设您要添加:此外,目标节点的 isOptional
属性也不得设置为 True
。这导致以下路径表达式,potame 已经正确建议:
//*[not(*) and not(ancestor-or-self::*[@isOptional = 'True'])]
唯一的结果是
<node9 isOptional="False">text1</node9>
因为现在 XPath 表达式包含 ancestor-or-self::
轴,其中包括上下文节点本身。
我有一批 xml,其中每个标签都有一个属性 isOptional。这种 xml 的一个例子是
<node1 isOptional="False">
<node2 isOptional="True">
<node22 isOptional="False">
<node3 isOptional="False">text4</node3>
</node22>
</node2>
<node4 isOptional="False">
<node5 isOptional="False">
<node6 isOptional="True">text3</node6>
<node7 isOptional="False">
<node8 isOptional="True">text2</node8>
<node9 isOptional="False">text1</node9>
</node7>
</node5>
</node4>
</node1>
我需要将 xpath 写入 select 所有没有 @isOptional="True"
祖先的叶节点。对于这个例子,结果应该是:
<node9 isOptional="False">text1</node9>
其实我需要这样的东西:
//*[not(*) and @isOptional="False" and count(ancestor::*[@isOptional = "True"] = 0)]
你能帮我得到正确的 xpath 吗?
像这样的 XPath 应该可以工作:
//*[not(child::*) and not(ancestor-or-self::*[@isOptional = 'True'])]
我想你忘了提一个额外的要求。你说的:
I need to write xpath to select all leaf nodes which do not have ancessors with
@isOptional="True"
将映射到以下 XPath 表达式:
//*[not(*) and not(ancestor::*[@isOptional = 'True'])]
^^^ "all"
^^^^^^^ "leaf nodes"
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ "no ancestor where
@isOptional equals 'True'"
这将产生(个别结果由 ------
分隔):
<node6 isOptional="True">text3</node6>
-----------------------
<node8 isOptional="True">text2</node8>
-----------------------
<node9 isOptional="False">text1</node9>
但我假设您要添加:此外,目标节点的 isOptional
属性也不得设置为 True
。这导致以下路径表达式,potame 已经正确建议:
//*[not(*) and not(ancestor-or-self::*[@isOptional = 'True'])]
唯一的结果是
<node9 isOptional="False">text1</node9>
因为现在 XPath 表达式包含 ancestor-or-self::
轴,其中包括上下文节点本身。