【发布时间】:2023-02-16 20:16:06
【问题描述】:
我有以下源 XML
<root>
<i type="r"/>
<i type="s"/>
<i type="r"/>
<i type="r"/>
<i type="s"/>
<i type="r"/>
<i type="s"/>
</root>
我的目标是将所有事件与所有即时following-siblings (1..n) of type="r".
无包装:
- type="r" 节点没有前面的 type="s" 节点
- type="s" 节点没有直接跟随 type="r" 节点
预期输出:
<i type="r"/>
<wrap>
<i type="s"/>
<i type="r"/>
<i type="r"/>
</wrap>
<wrap>
<i type="s"/>
<i type="r"/>
</wrap>
<i type="s"/>
我一直在尝试使用以下 XSLT sn-p 解决问题:
<xsl:for-each select="./i">
<xsl:choose>
<xsl:when test="current()[./@type='r' and count(preceding-sibling::i[@type='s']) = 0]">
<!-- Processing w/o wrap -->
<xsl:apply-templates select="current()" mode="fill"/>
</xsl:when>
<xsl:when test="current()[./@type='s' and following-sibling::i[@type='s']]">
<!-- Processing w/o wrap -->
<xsl:apply-templates select="current()" mode="fill"/>
</xsl:when>
<xsl:when test="current()[./@type='r' and count(preceding-sibling::i[@type='s']) > 0]">
<!-- Do nothing -->
</xsl:when>
<xsl:when test="current()[./@type='s' and following-sibling::i[1][@type='r']]">
<wrap>
<xsl:apply-templates select="current() | //i[@type='r' and preceding-sibling::i[@type='s']" mode="fill"/>
</wrap>
</xsl:when>
</xsl:choose>
</xsl:for-each>
我总是无法在 .
需要注意的是后续的模板都是在处理所有的节点。由于与问题本身无关,这些模板已被省略。
【问题讨论】: