Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old October 27th, 2006, 08:51 PM
Registered User
Join Date: Oct 2006
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Sum up value of all entity

Dear all,

I have a xml like this:

<?xml version="1.0" encoding="UTF-8"?>
            <location>Hong Kong Observatory</location>
            <amount date="2006-05-16">1.6</amount>
            <amount date="2006-05-17">15.0</amount>
            <location>Mid Levels</location>
            <amount date="2006-05-15">0.0</amount>
            <amount date="2006-05-16">2.0</amount>
            <amount date="2006-05-17">10.0</amount>
            <amount date="2006-05-18">0.0</amount>
            <location>Shau Kei Wan</location>
            <amount date="2006-05-15">0.0</amount>
            <amount date="2006-05-16">2.5</amount>
            <amount date="2006-05-17">10.0</amount>
            <amount date="2006-05-18">1.0</amount>

I want to sum up the total rainfall amount in html like this:

|Station |Total|
|Hong Kong Observatory | 16.6|
|Mid Levels | 12.0|
|Shau Kei Wan | 13.5|

So how to sum up the value of the "amount" entity? Please help!!! Thx a lot!

Old October 28th, 2006, 03:43 AM
mhkay's Avatar
Wrox Author
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts

First, get your terminology right. It's an element, not an entity. You won't be able to find the right entry in the index of a book, or the right information on Google, if you use the wrong terminology.

Second, use the sum function: sum(amount).

Michael Kay
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
Old October 28th, 2006, 03:47 AM
joefawcett's Avatar
Wrox Author
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts

You need to output one row for each station so for each station you need sum(amount):
<xsl:stylesheet version="1.0"

  <xsl:template match="/">
            Rainfall by Station during '<xsl:value-of select="*/name"/>'
            <xsl:apply-templates select="*/rainfall/station"/>
                <xsl:value-of select="sum(*/rainfall/station/amount)"/>

  <xsl:template match="station">
        <xsl:value-of select="location"/>
        <xsl:value-of select="sum(amount)"/>

Joe (Microsoft MVP - XML)

Similar Threads
Thread Thread Starter Forum Replies Last Post
Entity Conversion vengatatindia Word VBA 0 February 15th, 2008 05:52 AM
character entity into numeric character entity srkumar XSLT 1 November 22nd, 2007 04:53 AM
entity conversion orlyyefet XSLT 3 July 29th, 2007 03:14 PM
Help: Running Sum (or Cumulative Sum) timdasa VB Databases Basics 1 August 22nd, 2006 03:12 PM
Difference between Entity and Entity type arshad mahmood C++ Programming 0 May 8th, 2004 12:34 AM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.