如果我们在@more=元素中获得了“1”属性,那么<more>元素应该是add just <following-sibling> of <student>。如果添加“2”属性,则应在两个<following-sibling> <student>元素中添加<more>元素
输入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>发布于 2021-10-19 13:47:53
试着沿着这样的思路
<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>https://stackoverflow.com/questions/69629668
复制相似问题