Re: [xsl] accumulate a it possible?

Subject: Re: [xsl] accumulate a it possible?
From: Gregory Murphy <Gregory.Murphy@xxxxxxxxxxx>
Date: Wed, 9 Oct 2002 11:39:08 -0700 (PDT)

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=""; version="1.0">
<xsl:template match="/">
  <table border="1">
    <xsl:apply-templates select="numbers/num"/>

<xsl:template match="num">
    <td>num=<xsl:value-of select="."/></td>
      <xsl:apply-templates select="." mode="accumulate"/>

<xsl:template match="num" mode="accumulate">
    <xsl:when test="preceding-sibling::num">
      <xsl:variable name="previous">
        <xsl:apply-templates select="preceding-sibling::num[1]"
      <xsl:value-of select="number(.) + $previous"/>
      <xsl:value-of select="."/>


 XSL-List info and archive:

Current Thread