获取没有特定祖先xml xpath的节点 [英] Obtain nodes that don't have specific ancestor xml xpath
问题描述
我想要 xpath,它获取没有祖先的节点,它是特定节点的第一个后代.
I would like to have xpath, that obtains nodes, that don't have ancestor, which is first descendant of specific node.
假设我们有这样的 xml 文档:
Let's assume we have xml document like this:
<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 节点.
I would like to select all b nodes in document that don't have as their ancestor node //a/c/a[1].
推荐答案
我想选择所有 b 节点没有作为他们的文件祖先节点
//a/c/a[1]
使用这个 XPath 表达式:
//b[not(ancestor::a
[parent::c[parent::a]
and
not(preceding-sibling::a)
]
)
]
这将选择文档中所有没有祖先 a
有父 c
父 的文档中的元素a
and 具有父 c
的 a
祖先不是其父的第一个 a
子.
This selects all b
elements in the document that don't have ancestor a
that has a parent c
that has parent a
and the a
ancestor that has parent c
is not the first a
child of its parent.
给定以下 XML 文档(基于提供的,但格式正确,并且还在应选择的节点中放置了标识文本):
Given the following XML document (based on the provided, but made well-formed and also put identifying text in the nodes that should be selected):
<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>
正好选择了想要的 6 个 b
元素.
exactly the wanted 6 b
elements are selected.
使用 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
元素并将其复制到输出中.产生了想要的、正确的结果:
when this transformation is applied on the above XML document, exactly the wanted b
elements are selected and copied to the output. The wanted, correct result is produced:
<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>
这篇关于获取没有特定祖先xml xpath的节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!