Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
| Search | Today's Posts | Mark Forums Read
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
Location: , , .
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"?>
<tropicalStorm>
    <name>Maria</name>
    <rainfall>
        <station>
            <location>Hong Kong Observatory</location>
            <amount date="2006-05-16">1.6</amount>
            <amount date="2006-05-17">15.0</amount>
        </station>
        <station>
            <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>
        </station>
        <station>
            <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>
        </station>
    </rainfall>
</tropicalStorm>

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
Points: 18,487, Level: 59
Points: 18,487, Level: 59 Points: 18,487, Level: 59 Points: 18,487, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

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
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old October 28th, 2006, 03:47 AM
joefawcett's Avatar
Wrox Author
Points: 9,763, Level: 42
Points: 9,763, Level: 42 Points: 9,763, Level: 42 Points: 9,763, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

You need to output one row for each station so for each station you need sum(amount):
Code:
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:template match="/">
    <html>
      <body>
        <table>
          <caption>
            Rainfall by Station during '<xsl:value-of select="*/name"/>'
          </caption>
          <thead>
            <tr>
              <th>Station</th>
              <th>Rainfall</th>
            </tr>
          </thead>
          <tbody>
            <xsl:apply-templates select="*/rainfall/station"/>
            <tr>
              <td>ALL</td>
              <td>
                <xsl:value-of select="sum(*/rainfall/station/amount)"/>
              </td>
            </tr>
          </tbody>
        </table>
      </body>
    </html>
  </xsl:template>

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

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.