...

View Full Version : XSL Param or Attribute?



TechGlider
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:



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







<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 -->

</xsl:for-each>



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?

TechGlider
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.

TechGlider
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)" />

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

xml:


<?xml version="1.0"?>
<base>
<item>
<amount>1</amount>
<check>yes</check>
</item>
<item>
<amount>2</amount>
<check>yes</check>
</item>
<item>
<amount>3</amount>
<check>yes</check>
</item>
<item>
<amount>4</amount>
<check>yes</check>
</item>
<item>
<amount>5</amount>
<check>yes</check>
</item>
<item>
<amount>6</amount>
<check>yes</check>
</item>
<item>
<amount>7</amount>
<check>yes</check>
</item>
<item>
<amount>8</amount>
<check>yes</check>
</item>

</base>


xslt:


<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:f="http://exslt.org/functions"
extension-element-prefixes="f">

<xsl:output method="xml"
indent="yes"/>

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

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

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

</xsl:stylesheet>


see also:

http://exslt.org/func/index.html

best regards



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum