Ok, I just read that you just need the numbers, so the next one without 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)"/> <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> <xsl:template name="addthem"> <xsl:param name="pos"/> <xsl:param name="sum"/> <xsl:value-of select="$sum"/> <xsl:text> </xsl:text> <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>
displays on your sample an input of the following results:
20 60 90 170
Template (original with inscriptions, etc.):
<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', ',')"/> <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> <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> <xsl:template name="addthem"> <xsl:param name="count"/> <xsl:param name="pos"/> <xsl:param name="sum"/> <xsl:param name="elements"/> <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> <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> <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>
outputs the following result for your xml input:
first : 20 first and second : 60 first second and third : 90 first second third and fourth : 170
I have added comments inside, let me know if you need more help. It mainly uses two recursive patterns, one for βlabelsβ and the other for adding.
And, your sample output should read 90 and 170 instead of 110 and 190, or your input sample should indicate age = 50 instead of age = 30