需要添加<more></more> <student> 元素



如果我们得到@more="1"属性,则<more>元素只应添加<student><following-sibling>。如果@more ="2";属性add<more>元素应添加到<following-sibling><student>元素

输入XML:

<kk>
<kita>
<student>
<roll>content here</roll>
<roll>content here</roll>
<roll>content here</roll>
<roll more="1">content here</roll>
</student>
<student>
<roll>content here</roll>
<roll>content here</roll>
<roll>content here</roll>
<roll>content here</roll>
</student>
<student>
<roll>content here</roll>
<roll>content here</roll>
<roll>content here</roll>
<roll>content here</roll>
</student>
</kita>
</kk>
XSL文件:

在xsl中有更多的代码,只是我复制在这里:

<xsl:if test="parent::student[parent::kita]/following-sibling::student/x:roll[@more]">
<more/>
</xsl:if>

预期输出:

<?xml version="1.0" encoding="UTF-8"?>
<kk>
<kita>
<student>
<roll>content here</roll>
<roll>content here</roll>
<roll>content here</roll>
<roll more="1">content here</roll>
</student>
<student>
<!-- if @more="1" or @more="2" add <more> element-->
<more>Content here</more>
<roll>content here</roll>
<roll>content here</roll>
<roll>content here</roll>
<roll>content here</roll>
</student>
<student>
<!-- If @more="2" add <more> element below-->
<more>Content here</more>
<roll>content here</roll>
<roll>content here</roll>
<roll>content here</roll>
<roll>content here</roll>
</student>
</kita>
</kk>

<xsl:template match="kita">
<xsl:copy>
<xsl:for-each-group select="student" group-starting-with="student[roll/@more]">
<xsl:apply-templates select="current-group()">
<xsl:with-param name="emit-more" select="xs:integer(roll/@more)"/>
<xsl:with-param name="more" select="roll[@more]"/>
</xsl:apply-templates>
</xsl:for-each-group>
</xsl:copy>
</xsl:template>

<xsl:template match="student">
<xsl:param name="emit-more"/>
<xsl:param name="more"/>
<xsl:copy>
<xsl:apply-templates select="@*"/>
<xsl:if test="position() - 1 = (1 to $emit-more)">
<more>
<xsl:apply-templates select="$more/node()"/>
</more>
</xsl:if>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>

最新更新