?
Solved

XSLT: modifying a variable

Posted on 2004-10-29
4
Medium Priority
?
306 Views
Last Modified: 2006-11-17
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
0
Comment
Question by:blackfrancis75
  • 2
4 Comments
 
LVL 8

Expert Comment

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

:)
Don
0
 
LVL 5

Author Comment

by:blackfrancis75
ID: 12444223
ah, forgive me for being slow but could you provide a short example?
that'd really help.  ta
0
 
LVL 5

Author Comment

by:blackfrancis75
ID: 12444336
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
 
LVL 26

Accepted Solution

by:
rdcpro earned 2000 total points
ID: 12446300
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

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
Kernel Data Recovery is a renowned Data Recovery solution provider which offers wide range of softwares for both enterprise and home users with its cost-effective solutions. Let's have a quick overview of the journey and data recovery tools range he…
Watch the video to learn how one can deal with PST file corruption issue with an outstanding Kernel for Outlook PST Repair Tool easily. Using this tool, non-technical users can swiftly perform the repair process to restore their essential data witho…
Suggested Courses

599 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question