XSLT 分组:将父元素添加到子元素中的元素集
XSLT Grouping's : Add parent to set of elements in child elements
使用 for-each-group 我试图通过每个 para 元素的 class 值添加父节点。我尝试应用分组但结果不好,我没有得到所需的输出。我对在这种情况下使用分组感到困惑。在这种情况下有没有更好的添加父节点的方法?
当前 XML:
<?xml version="1.0" encoding="utf-8" ?>
<section>
<h1>Some heading</h1>
<section>
<p>normal paragaraph</p>
<p class="list">list 1</p>
<p class="list">list 1</p>
<p>normal paragaraph</p>
<p class="list">list 2</p>
<p class="list">list 2</p>
</section>
<section> ... </section>
</section>
使用的 XSLT:
<xsl:template match="section">
<xsl:for-each-group select="node()" group-by="if (@class='list') then 'list' else 'nolist'">
<xsl:for-each select="current-grouping-key()">
<xsl:choose>
<xsl:when test="current-grouping-key() = 'list'">
<list>
<xsl:apply-templates select="current-group()" />
</list>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates select="current-group()" />
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</xsl:for-each-group>
</xsl:template>
当前输出:
<h1>Some heading</h1>
<p>normal paragaraph</p>
<p>normal paragaraph</p>
<list>
<p class="list">list 1</p>
<p class="list">list 1</p>
<p class="list">list 2</p>
<p class="list">list 2</p>
</list>
<p>normal paragaraph</p>
....
预期输出:
<section>
<h1>Some heading</h1>
<section>
<p>normal paragaraph</p>
<list>
<p class="list">list 1</p>
<p class="list">list 1</p>
</list>
<p>normal paragaraph</p>
<list>
<p class="list">list 2</p>
<p class="list">list 2</p>
</list>
</section>
<section>...</section>
</section>
这是我希望这对某人有帮助的答案,正如@marthin honnen 所评论的,我尝试使用 group-adjacent,得到了想要的输出,这很有帮助。
<xsl:template match="section">
<xsl:copy>
<xsl:for-each-group select="*" group-adjacent="if (@class='list') then 'list' else 'nolist'">
<xsl:for-each select="current-grouping-key()">
<xsl:choose>
<xsl:when test="current-grouping-key() = 'list'">
<list>
<xsl:apply-templates select="current-group()" />
</list>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates select="current-group()" />
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</xsl:for-each-group>
</xsl:copy>
</xsl:template>
<xsl:template match="section">
<section>
<h1>Some Heading</h1>
<section>
<xsl:for-each-group select="section/p" group-by=".">
<xsl:choose>
<xsl:when test="current-grouping-key() != 'normal paragaraph'">
<p>normal paragaraph</p>
<list>
<xsl:for-each select="current-group()">
<p class="list"><xsl:value-of select="current-grouping-key()"/></p>
</xsl:for-each>
</list>
</xsl:when>
</xsl:choose>
</xsl:for-each-group>
</section>
</section>
</xsl:template>
使用 for-each-group 我试图通过每个 para 元素的 class 值添加父节点。我尝试应用分组但结果不好,我没有得到所需的输出。我对在这种情况下使用分组感到困惑。在这种情况下有没有更好的添加父节点的方法?
当前 XML:
<?xml version="1.0" encoding="utf-8" ?>
<section>
<h1>Some heading</h1>
<section>
<p>normal paragaraph</p>
<p class="list">list 1</p>
<p class="list">list 1</p>
<p>normal paragaraph</p>
<p class="list">list 2</p>
<p class="list">list 2</p>
</section>
<section> ... </section>
</section>
使用的 XSLT:
<xsl:template match="section">
<xsl:for-each-group select="node()" group-by="if (@class='list') then 'list' else 'nolist'">
<xsl:for-each select="current-grouping-key()">
<xsl:choose>
<xsl:when test="current-grouping-key() = 'list'">
<list>
<xsl:apply-templates select="current-group()" />
</list>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates select="current-group()" />
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</xsl:for-each-group>
</xsl:template>
当前输出:
<h1>Some heading</h1>
<p>normal paragaraph</p>
<p>normal paragaraph</p>
<list>
<p class="list">list 1</p>
<p class="list">list 1</p>
<p class="list">list 2</p>
<p class="list">list 2</p>
</list>
<p>normal paragaraph</p>
....
预期输出:
<section>
<h1>Some heading</h1>
<section>
<p>normal paragaraph</p>
<list>
<p class="list">list 1</p>
<p class="list">list 1</p>
</list>
<p>normal paragaraph</p>
<list>
<p class="list">list 2</p>
<p class="list">list 2</p>
</list>
</section>
<section>...</section>
</section>
这是我希望这对某人有帮助的答案,正如@marthin honnen 所评论的,我尝试使用 group-adjacent,得到了想要的输出,这很有帮助。
<xsl:template match="section">
<xsl:copy>
<xsl:for-each-group select="*" group-adjacent="if (@class='list') then 'list' else 'nolist'">
<xsl:for-each select="current-grouping-key()">
<xsl:choose>
<xsl:when test="current-grouping-key() = 'list'">
<list>
<xsl:apply-templates select="current-group()" />
</list>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates select="current-group()" />
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</xsl:for-each-group>
</xsl:copy>
</xsl:template>
<xsl:template match="section">
<section>
<h1>Some Heading</h1>
<section>
<xsl:for-each-group select="section/p" group-by=".">
<xsl:choose>
<xsl:when test="current-grouping-key() != 'normal paragaraph'">
<p>normal paragaraph</p>
<list>
<xsl:for-each select="current-group()">
<p class="list"><xsl:value-of select="current-grouping-key()"/></p>
</xsl:for-each>
</list>
</xsl:when>
</xsl:choose>
</xsl:for-each-group>
</section>
</section>
</xsl:template>