Link to home
Start Free TrialLog in
Avatar of blackfrancis75
blackfrancis75

asked on

XSLT: modifying a variable

Hello,
I have a little template which is meant to iterate over all the /records of my XML document and add ./field[7] to a running total.
I know what you're going to say - use sum() instead, but the problem is I need the summing to be conditional (so later on I'll put <xsl:if> processing
in this template).
Anyway, is it possible to do something like the following;

    <xsl:template name="calc_vat">
        <xsl:variable name="bab" select="0"/>
        <xsl:for-each select="/tabledata/record"><xsl:variable name="bab" select="number($bab)+${./field[7]}"/></xsl:for-each>
    <xsl:value-of select="$bab"/></xsl:template>

it isn't working at the moment which I presume is because the variable "bab" is being redeclared within my <for-each> processing loop,
but what I really want to do is add the field[7] value to the "bab" figure on each iteration.


Can someone give me some pointers or an alternative solution?
Thanks
Avatar of a_twixt_in_the_tale
a_twixt_in_the_tale

apply a template to filter out the nodes u need and then sum() across the transformed o/p?

:)
Don
Avatar of blackfrancis75

ASKER

ah, forgive me for being slow but could you provide a short example?
that'd really help.  ta
Im guessing you mean something like this:

    <xsl:template match="/tabledata/record" name="total">
        <xsl:for-each select="/tabledata/record">
             <xsl:if test="./@include">
                 <xsl:value-of select="format-number(sum(./field[7]),'#0.00')"/>
             </xsl:if>
        </xsl:for-each>
    </xsl:template>  

But im not sure how to make this template return ONLY the sum of all the filtered nodes, if you could help out that'd be great.
ASKER CERTIFIED SOLUTION
Avatar of rdcpro
rdcpro
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial