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
LVL 5
blackfrancis75Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

:)
Don
0
blackfrancis75Author Commented:
ah, forgive me for being slow but could you provide a short example?
that'd really help.  ta
0
blackfrancis75Author Commented:
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.
0
rdcproCommented:
It would help if you had posted a sample of the XML, along with the condition you're wanting to test for, but if the XML looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<tabledata>
      <record include="yes">
            <field>foo</field>
            <field>bar</field>
            <field>1</field>
            <field>snafu</field>
      </record>
      <record>
            <field>foo</field>
            <field>bar</field>
            <field>1</field>
            <field>snafu</field>
      </record>
      <record>
            <field>foo</field>
            <field>bar</field>
            <field>1</field>
            <field>snafu</field>
      </record>
      <record include="yes">
            <field>foo</field>
            <field>bar</field>
            <field>1</field>
            <field>snafu</field>
      </record>
      <record>
            <field>foo</field>
            <field>bar</field>
            <field>1</field>
            <field>snafu</field>
      </record>
      <record include="yes">
            <field>foo</field>
            <field>bar</field>
            <field>1</field>
            <field>snafu</field>
      </record>
      <record include="yes">
            <field>foo</field>
            <field>bar</field>
            <field>1</field>
            <field>snafu</field>
      </record>
</tabledata>

And  you only want to sum those nodes where include="yes", then this works:


<xsl:value-of select="sum(/tabledata/record[@include='yes']/field[3])"/>

and produces "4"

Regards,
Mike Sharp
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
XML

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.