我的任务是打破祖先<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>
答案 0 :(得分:2)
对于您的两个样本,我都会得到正确的输出
<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
它是XSLT 3,但是对于XSLT 2处理器,您当然可以替换由显式模板用于身份转换的xsl:mode
声明。