我有一份文件,如下:
<root>
<A node="1"/>
<B node="2"/>
<A node="3"/>
<A node="4"/>
<B node="5"/>
<B node="6"/>
<A node="7"/>
<A node="8"/>
<B node="9"/>
</root>
Run Code Online (Sandbox Code Playgroud)
使用xpath,如何选择连续跟随给定A元素的所有B元素?
它类似于跟随-silbing :: B,除了我希望它们只是紧随其后的元素.
如果我在A(节点== 1),那么我想选择节点2.如果我在A(节点== 3),那么我想什么都不选.如果我在A(节点== 4),那么我想选择5和6.
我可以在xpath中执行此操作吗?编辑:它在XSL样式表选择语句中.
EDIT2:我不想将各种元素的node属性用作唯一标识符.我只是为了说明我的观点而包含了node属性.在实际的XML文档中,我没有一个属性,我将其用作唯一标识符.xpath"follow-sibling :: UL [preceding-sibling :: LI [1]/@ node = current()/ @ node]"是节点属性上的键,这不是我想要的.
简短回答(假设current()没问题,因为这是标记的xslt):
following-sibling::B[preceding-sibling::A[1]/@node = current()/@node]
Run Code Online (Sandbox Code Playgroud)
样式表示例:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml"/>
<xsl:template match="/">
<xsl:apply-templates select="/root/A"/>
</xsl:template>
<xsl:template match="A">
<div>A: <xsl:value-of select="@node"/></div>
<xsl:apply-templates select="following-sibling::B[preceding-sibling::A[1]/@node = current()/@node]"/>
</xsl:template>
<xsl:template match="B">
<div>B: <xsl:value-of select="@node"/></div>
</xsl:template>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)
祝好运!
归档时间: |
|
查看次数: |
3363 次 |
最近记录: |