我想要 xpath,它获取没有祖先的节点,它是特定节点的第一个后代。
假设我们有这样的 xml 文档:
<a>
<b>This node</b>
<c>
<a>
<b>not this</b>
<g>
<b>not this</b>
</g>
</a>
<a>
<b>This node</b>
<c/>
</a>
</c>
</a>
<a>
<c>
<a>
<b>not this</b>
</a>
<a>
<b>This node</b>
</a>
<a>
<b>This node</b>
</a>
<a>
<b>This node</b>
</a>
</c>
</a>
<d>
<b>This node</b>
</d>
我想选择文档中没有祖先节点 //a/c/a[1] 的所有 b 节点。
我想选择所有 b 节点
没有作为他们的文件
祖先节点//a/c/a[1]
使用此 XPath 表达式:
//b[not(ancestor::a
[parent::c[parent::a]
and
not(preceding-sibling::a)
]
)
]
这将选择所有b
文档中没有祖先的元素a
有一个父母c
有父母的a
and the a
有父母的祖先c
不是第一个a
其父母的孩子。
给定以下 XML 文档(基于提供的,但格式良好,并将识别文本放入应选择的节点中):
<t>
<a>
<b>This node 1</b>
<c>
<a>
<b>not this</b>
<g>
<b>not this</b>
</g>
</a>
<a>
<b>This node 2</b>
<c/>
</a>
</c>
</a>
<a>
<c>
<a>
<b>not this</b>
</a>
<a>
<b>This node 3</b>
</a>
<a>
<b>This node 4</b>
</a>
<a>
<b>This node 5</b>
</a>
</c>
</a>
<d>
<b>This node 6</b>
</d>
</t>
正是想要的 6b
元素被选择。
使用 XSLT 验证:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="/">
<xsl:copy-of select=
"//b[not(ancestor::a
[parent::c[parent::a]
and
not(preceding-sibling::a)
]
)
]
"/>
</xsl:template>
</xsl:stylesheet>
当这个转换应用于上面的 XML 文档时,正是想要的b
选择元素并将其复制到输出。产生了想要的正确结果:
<b>This node 1</b>
<b>This node 2</b>
<b>This node 3</b>
<b>This node 4</b>
<b>This node 5</b>
<b>This node 6</b>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)