使用 XSLT 2.0 的解决方案可能是:
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" />
<xsl:variable name="count" select="4" />
<xsl:template match="Element">
<xsl:value-of select="for $i in 1 to $count return concat(Value[$i], '')"
separator="," />
<xsl:text>
</xsl:text>
</xsl:template>
<xsl:template match="text()" />
</xsl:stylesheet>
注意:您也可以使用 if 语句代替 concat 函数。
为了完整起见,使用 XSLT 1.0 编写的解决方案:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" />
<xsl:variable name="count" select="4" />
<!-- Ignore all text elements -->
<xsl:template match="text()" />
<xsl:template match="Element">
<xsl:if test="$count > 0">
<!-- Output existing values -->
<xsl:apply-templates select="Value[position() <= $count]" />
<!-- Output remaining commas -->
<xsl:call-template name="print-commas">
<xsl:with-param name="number"
select="$count - count(Value)" />
</xsl:call-template>
<!-- Line break -->
<xsl:text>
</xsl:text>
</xsl:if>
</xsl:template>
<!-- Print the first value without a comma preprended to the value -->
<xsl:template match="Value[1]">
<xsl:value-of select="." />
</xsl:template>
<!-- Print the reamaining value with a comma preprended to the value -->
<xsl:template match="Value">
<xsl:value-of select="concat(',', .)" />
</xsl:template>
<!-- Print the given amount of commas -->
<xsl:template name="print-commas">
<!-- Number of commas to be printed -->
<xsl:param name="number" />
<xsl:if test="$number > 0">
<xsl:text>,</xsl:text>
<!-- Recursive call decrementing the number of commas to
be printed -->
<xsl:call-template name="print-commas">
<xsl:with-param name="number"
select="$number - 1" />
</xsl:call-template>
</xsl:if>
</xsl:template>
</xsl:stylesheet>