Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

XSLT: modifying a variable

Posted on 2004-10-29
4
Medium Priority
?
303 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

The Client Need Led Us to RSS I recently had an investment company ask me how they might notify their constituents about their newsworthy publications.  Probably you would think "Facebook" or "Twitter" but this is an interesting client.  Their cons…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

636 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