On Wed, 9 Oct 2002, Carter, Will wrote:
I am trying to accumulate a variable but not succesful... here is my xml:
--------------------------------
<numbers>
<num>3</num>
<num>7</num>
<num>11</num>
<num>6</num>
<num>3</num>
</numbers>
--------------------------------
here is the output I want:
--------------------------------
num=3 accumulated=3
num=7 accumulated=10
num=11 accumulated=21
num=6 accumulated=27
num=3 accumulated=30
--------------------------------
Try this - but watch out for the quadratic run-time!
// Gregory Murphy
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/">
<table border="1">
<xsl:apply-templates select="numbers/num"/>
</table>
</xsl:template>
<xsl:template match="num">
<tr>
<td>num=<xsl:value-of select="."/></td>
<td>
<xsl:text>accumulated=</xsl:text>
<xsl:apply-templates select="." mode="accumulate"/>
</td>
</tr>
</xsl:template>
<xsl:template match="num" mode="accumulate">
<xsl:choose>
<xsl:when test="preceding-sibling::num">
<xsl:variable name="previous">
<xsl:apply-templates select="preceding-sibling::num[1]"
mode="accumulate"/>
</xsl:variable>
<xsl:value-of select="number(.) + $previous"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="."/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list