spacer

Webref WebRef   Sitemap · Experts · Tools · Services · Newsletters · About i.com

home / experts / javascript / column100


Web Services, Part V: XML and XSLT Programming

Adding Up XML Numbers

Market Data Analyst (IL)
Next Step Systems
US-IL-Chicago

Justtechjobs.com Post A Job | Post A Resume
Developer News
OpenOffice 3.2 Lands Amid Critical Changes
Red Hat, IBM Firmly in KVM Virtualization Camp
Red Hat Talks Up Open Source Cloud Plans


You can use XSLT to add up data within identical tags. Suppose you have the following XML section:

<month>
  <name>January 2001</name>
  <week number="1" dvds_rented="12000" />
  <week number="2" dvds_rented="15000" />
  <week number="3" dvds_rented="18000" />
  <week number="4" dvds_rented="11800" />
</month>

To add all dvds_rented during all four weeks, you will write:

<xsl:value-of select="sum(week/@dvds_rented)"/>

You need to insert this line inside a loop where the value of month is known. Something like this:

<xsl:for-each select="//data/month">
...
<xsl:value-of select="sum(week/@dvds_rented)"/>
...
</xsl:for-each>  

Use the format-number() method to pretty-print the sum:


<xsl:value-of select="format-number(sum(week/@dvds_rented),
  '###,###')"/>

Here is the XSLT file that converts the MyDVD XML file (try it)(view it):

<?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>
<HEAD>
<TITLE><xsl:value-of select="//summary/heading"/></TITLE>
</HEAD>
<BODY>
<H1><xsl:value-of select="//summary/heading"/></H1>
<H2><xsl:value-of select="//summary/subhead"/></H2>
<P><xsl:value-of select="//summary/description"/></P>
<TABLE>
<TR>
<TH>Month\Week</TH>
<xsl:for-each select="//data/month[1]/week">
<TH>W<xsl:value-of select="@number"/></TH>
</xsl:for-each>
<TH>Total</TH>
</TR>
<xsl:for-each select="//data/month">
 <tr>
   <th style="text-align:left"><xsl:value-of select="name"/></th>
   <xsl:for-each select="week">
     <td style="text-align:right">
       <xsl:value-of select="format-number(@dvds_rented, '###,###')"/>
     </td>
   </xsl:for-each>
	<td style="text-align:right;font-weight:bold">
	 <xsl:value-of select="format-number(sum(week/@dvds_rented),
     '###,###')"/>
	</td>
 </tr>
</xsl:for-each>
</TABLE>
</BODY>
</HTML>
</xsl:template>
</xsl:stylesheet>

Here is the output:


Next: How to define and use variables in XSLT

http://www.internet.com


The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers

webref The latest from WebReference.com Browse >
Search Engine Optimization: Selecting and Embedding Keywords · Are Google's Language Translation Web Services Ready for Prime Time? · Installing and Using Meeplace, the Business Review CMS
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
IBM DB2 10 for z/OS: Justifying the Upgrade · Living La Vida Colo: Choosing the Right Colocation Facility · FTC Concerns over Social Media Privacy Linger


Produced by Yehuda Shiran and Tomer Shiran
All Rights Reserved. Legal Notices.
Created: December 31, 2001
Revised: December 31, 2001

URL: http://www.webreference.com/js/column100/7.html