Solved

XSLT: modifying a variable

Posted on 2004-10-29
298 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
Question by:blackfrancis75
    4 Comments
     
    LVL 8

    Expert Comment

    by:a_twixt_in_the_tale
    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
    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
    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:
    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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone. Privacy Policy Terms of Use

    Featured Post

    Prepare to Pass the CompTIA A+ 900 Series Exam

    CompTIA aims to adapt its A+ Certification to reflect the most current knowledge and skills needed by today's IT professionals--and this year's 2016 exam is harder than ever. This certification is one of the most highly-respected and sought after in IT.

    Suggested Solutions

    Title # Comments Views Activity
    Merge XML into DOM 5 24
    Issue exporting to XML with Excel 2016 3 52
    XML template problem 4 49
    XML file fails to process correctly 3 32
    Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
    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.
    With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…
    This video Micro Tutorial is the second in a two-part series that shows how to create and use custom scanning profiles in Nuance's PaperPort 14.5 (http://www.experts-exchange.com/articles/17490/). But the ability to create custom scanning profiles a…

    877 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

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now