gpt4 book ai didi

XPath : select all following siblings until another sibling

转载 作者:行者123 更新时间:2023-12-03 07:50:52 48 4
gpt4 key购买 nike

这是我的 xml 的摘录:

<node/>
<node/>
<node id="1">content</node>
<node/>
<node/>
<node/>
<node id="2">content</node>
<node/>
<node/>

我定位在 node[@id='1'] .我需要一个 Xpath 来匹配所有 <node/>元素直到下一个非空节点(这里是 node[@id='2'] )。

编辑:
@id 属性只是为了更清楚地解释我的问题,而不是在我原来的 XML 中。我需要一个不使用 @id 属性的解决方案。

我愿意 不是 想匹配 node[@id='2']之后的空 sibling ,所以我不能使用幼稚的 following-sibling::node[text()=''] .

我怎样才能做到这一点?

最佳答案

你可以这样做:

../node[not(text()) and preceding-sibling::node[@id][1][@id='1']]

where '1' is the id of the current node (generate the expression dynamically).

The expression says:

  • from the current context go to the parent
  • select those child nodes that
  • have no text and
  • from all "preceding sibling nodes that have an id" the first one must have an id of 1

If you are in XSLT you can select from the following-sibling axis because you can use the current() function:

<!-- the for-each is merely to switch the current node -->
<xsl:for-each select="node[@id='1']">
<xsl:copy-of select="
following-sibling::node[
not(text()) and
generate-id(preceding-sibling::node[@id][1])
=
generate-id(current())
]
" />
</xsl:for-each>

或更简单(和更有效)的 key :
<xsl:key 
name="kNode"
match="node[not(text())]"
use="generate-id(preceding-sibling::node[@id][1])"
/>

<xsl:copy-of select="key('kNode', generate-id(node[@id='1']))" />

关于XPath : select all following siblings until another sibling,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2161766/

48 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com