[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: accumulate a variable...is it possible?

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

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


Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.