我的任务是将嵌套的<ol>从祖先<ol>中分离出来,并使每个<ol>元素处于同一级别。我有这个xml,
<main>
<ol>
<li>The above</li>
<li>Tprojects.</li>
<li>FreeSpan sections.</li>
<li>The above
<ol>
<li>Maximum
<ol>
<li>Middle</li>
</ol>
</li>
<li>Ultimate</li>
</ol>
</li>
<li>The above indicative</li>
<li>Appropriate Live</li>
<li>The above Indicative</li>
</ol>
</main>所以,预期输出,
<main>
<ol>
<li>The above</li>
<li>Tprojects.</li>
<li>FreeSpan sections.</li>
<li>The above</li>
</ol>
<ol>
<li>Maximum</li>
</ol>
<ol>
<li>Middle</li>
</ol>
<ol>
<li>Ultimate</li>
</ol>
<ol>
<li>The above indicative</li>
<li>Appropriate Live</li>
<li>The above Indicative</li>
</ol>
</main>我尝试过使用for-each来实现它,但无法正确完成,下面是我尝试的方法。
<xsl:template match="ol[descendant::ol]">
<xsl:for-each select="li">
<ol>
<li>
<xsl:apply-templates select="node()[not(self::ol)]"/>
</li>
</ol>
<xsl:apply-templates select="ol"/>
</xsl:for-each>
</xsl:template>发布于 2019-07-20 00:58:18
对于您的两个示例,我使用以下命令获得正确的输出
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="#all"
version="3.0">
<xsl:mode on-no-match="shallow-copy"/>
<xsl:output indent="yes"/>
<xsl:template match="ol[descendant::ol]">
<xsl:for-each-group select="descendant::li" group-starting-with="li[. is ../li[1]]">
<xsl:for-each-group select="current-group()" group-ending-with="li[. is ../li[last()]]">
<ol>
<xsl:apply-templates select="current-group()"/>
</ol>
</xsl:for-each-group>
</xsl:for-each-group>
</xsl:template>
<xsl:template match="li">
<xsl:copy>
<xsl:apply-templates select="text()/normalize-space()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>https://xsltfiddle.liberty-development.net/94rmq79/和https://xsltfiddle.liberty-development.net/94rmq79/1
它是XSLT3,但是对于XSLT2处理程序,您当然可以替换显式模板使用的xsl:mode声明来进行标识转换。
正如Alejandro所指出的,模式可以简单地表示为group-starting-with="li[1]"和group-ending-with="li[last()]",请参阅https://xsltfiddle.liberty-development.net/94rmq79/4
https://stackoverflow.com/questions/57116458
复制相似问题