还行,我只是看你刚才所需要的数字,所以下面剥离XSLT
<xsl:stylesheet
version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" indent="yes" />
<xsl:variable name="elements" select="/people/man"/>
<xsl:variable name="count" select="count($elements)"/>
<!-- Main Entry point -->
<xsl:template match="/">
<xsl:call-template name="addthem">
<xsl:with-param name="pos" select="1"/>
<xsl:with-param name="sum" select="$elements[1]/@age"/>
</xsl:call-template>
</xsl:template>
<!-- recursive calling template to sum up the ages -->
<xsl:template name="addthem">
<xsl:param name="pos"/>
<xsl:param name="sum"/>
<xsl:value-of select="$sum"/>
<xsl:text>
</xsl:text>
<!-- recursive call to sum up the ages -->
<xsl:if test="$pos lt number($count)">
<xsl:call-template name="addthem">
<xsl:with-param name="pos" select="$pos + 1"/>
<xsl:with-param name="sum" select="number($sum) + number($elements[$pos + 1]/@age)"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
产生你的样品以下输入 -
20
60
90
170
模板(原始一个带有标签和东西):
<xsl:stylesheet
version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" indent="yes" />
<xsl:variable name="txtlabels" select="tokenize('first,second,third,fourth,fifth,sixth,seventh,eights,ninth,tenth,eleventh,twelveth,thirteenth,fourteenth,fifteenth', ',')"/>
<!-- Util template to generate labels -->
<xsl:template name="getlabel">
<xsl:param name="startat" select="1"/>
<xsl:param name="idx"/>
<xsl:if test="number($startat) lt number($idx)">
<xsl:value-of select="$txtlabels[$startat]"/>
<xsl:text> </xsl:text>
<xsl:call-template name="getlabel">
<xsl:with-param name="startat" select="$startat + 1"/>
<xsl:with-param name="idx" select="$idx"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
<!-- Main Entry point -->
<xsl:template match="/">
<xsl:variable name="count">
<xsl:value-of select="count(/people/man)"/>
</xsl:variable>
<xsl:call-template name="addthem">
<xsl:with-param name="count" select="count(/people/man)"/>
<xsl:with-param name="pos" select="1"/>
<xsl:with-param name="sum" select="/people/man[1]/@age"/>
<xsl:with-param name="elements" select="/people/man"/>
</xsl:call-template>
</xsl:template>
<!-- recursive calling template to sum up the ages -->
<xsl:template name="addthem">
<xsl:param name="count"/>
<xsl:param name="pos"/>
<xsl:param name="sum"/>
<xsl:param name="elements"/>
<!-- get the label prefix, without the 'and' clause -->
<xsl:variable name="thelabelprefix">
<xsl:call-template name="getlabel">
<xsl:with-param name="startat" select="1"/>
<xsl:with-param name="idx" select="$pos"/>
</xsl:call-template>
</xsl:variable>
<!-- Now append the 'and' clause, if required, to the labels!!! -->
<xsl:variable name="thelabel">
<xsl:choose>
<xsl:when test="number($pos) eq 1">
<xsl:value-of select="$txtlabels[$pos]"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of
select="concat($thelabelprefix, ' and ', $txtlabels[$pos])"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:value-of select="$thelabel"/>
<xsl:text> : </xsl:text> <xsl:value-of select="$sum"/>
<xsl:text>
</xsl:text>
<!-- recursive call to sum up the ages -->
<xsl:if test="$pos lt number($count)">
<xsl:call-template name="addthem">
<xsl:with-param name="count" select="$count"/>
<xsl:with-param name="pos" select="$pos + 1"/>
<xsl:with-param name="sum" select="number($sum) + number($elements[$pos + 1]/@age)"/>
<xsl:with-param name="elements" select="$elements"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
产生以下为您输入XML输出:
first : 20
first and second : 60
first second and third : 90
first second third and fourth : 170
我已经添加评论里面,让我知道如果你需要进一步的帮助。 它基本上使用了两个递归模板,一个用于'标签',另一个用于添加。
而且,你的样本输出应改为90和170,而不是110和190或您的样品输入应该说年龄= 50代替年龄= 30
是 “第一时代”, “第一和第二年龄(组合)” 所需输出的部分等等?如果是的话,我认为这会很困难。仅仅一小部分的款项应该是可行的(尽管我现在没有一个方便的解决方案)。 – musiKk 2011-05-21 10:31:38
@musiKk:我只需要值,而不是文本。 – 2011-05-21 11:43:29
好问题,+1。查看我的答案,获取完整的,非常短的非递归解决方案。 :) – 2011-05-21 17:09:09