Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Dec 2002
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    XSLT: Using the sum function

    I have been reading about sum() to evaluate some nodes. I am very confused and cannot get anything working.

    Here is my (simplified) XML:

    <entity>
    <value>55</value>
    </entity>
    <entity>
    <value>7</value>
    </entity>
    <entity>
    <value>18</value>
    </entity>

    How do I write my XSL to add the <value> nodes to return the total? Do I use "sum()" with a "call-template" and what should that called template look like?

    Thanks!!

    P.S. Sorry if this has been covered in the forum, but you cannot search for strings less than 4 characters... like "sum", so I was unable to get any search results.

  • #2
    Regular Coder
    Join Date
    Jun 2002
    Location
    Newcastle, England
    Posts
    178
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Here's an xml file and an xsl stylesheet which performs the sum operation you are looking for:-

    patrick

    sum_me.xml (sourcefile)
    ------------------
    <?xml version="1.0"?>
    <?xml-stylesheet type="text/xsl" href="sum_it.xml"?>
    <main>
    <entity>
    <value>55</value>
    </entity>
    <entity>
    <value>7</value>
    </entity>
    <entity>
    <value>18</value>
    </entity>
    </main>

    sum_it.xml (stylesheet)
    ------------------
    <?xml version="1.0"?>
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="html"/>
    <xsl:template match="/">
    <html>
    <body>
    <xsl:apply-templates />
    </body>
    </html>

    </xsl:template>
    <xsl:template match="main">
    <p>
    <xsl:value-of select="sum(*)"/>
    </p>
    </xsl:template>
    </xsl:stylesheet>


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •