View Full Version : XSL Param or Attribute?

02-20-2009, 09:31 PM
I need some help creating a dynamic variable..

I need to set up a variable that you can add to:

Can something like this be done --> This will be a for-each clause which will be looping through each set of node finding a child node, lets just say named = "amount"

so for example:

<name />
<account_number />
<amount> x </amount>

<xsl:variable name="current_value">0</xsl:variable>

<xsl:for-each select="/account">

<xsl:value-of select="$current_value + amount" /> <!-- i need this amount to be set to the current_value of this value -->


What i need to is set up a dynamic variable like this " current_value = current_value + amount "

So every time it goes through and hits the node amount it adds it to the current_value?

02-23-2009, 02:29 PM
Ok so i found out you cannot alter the value of global values, so that theory is shot.. But no i am wondering if i can do something like this...

<xsl:value-of select="sum(AMOUNT[position() &lt;= current()]/AMOUNT)" />

Where i get the position of the previous node and make add it to the current node.. and i will just do a null check since the first node may get a null value?

What i am looking to do here is a running Total.

02-23-2009, 03:12 PM
Figured it out.. For those of you interested on how to do a running total.... Here it is

//Declare the variable
<xsl:variable name="position" select="position()"/>

<xsl:value-of select="sum(//ACCOUNT[position() &lt;= $position]/AMOUNT)" />

02-23-2009, 03:59 PM
this is a general implementation using exslt.


<?xml version="1.0"?>



<?xml version="1.0"?>
<xsl:stylesheet version="1.0"

<xsl:output method="xml"

<xsl:template match="/">
<xsl:apply-templates select="*//amount"/>
<xsl:value-of select="f:sum(*//amount)"/>

<xsl:template match="amount">
<xsl:value-of select="."/>

<f:function name="f:sum">
<xsl:param name="mynodeset"/>
<xsl:variable name="first"
<xsl:variable name="rest"
select="$mynodeset[position() > 1]"/>
<xsl:when test="$rest">
<f:result select="$first + f:sum($rest)"/>
<f:result select="$first"/>


see also:


best regards