還行,我只是看你剛纔所需要的數字,所以下面剝離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