December 31, 2001 - Adding Numbers in XSLT | WebReference

December 31, 2001 - Adding Numbers in XSLT

Yehuda Shiran December 31, 2001
Adding Numbers in XSLT
Tips: December 2001

Yehuda Shiran, Ph.D.
Doc JavaScript

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="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="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 an XSL file that converts the XML file at the bottom of this tip (try it):

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="https://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:

Here is the XML file:

<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="011231.xsl"?>
  <sales>
    <summary>
      <heading>MyDVD Rental Store</heading>
      <subhead>Periodical Sales Report</subhead>
      <description>Sales Report for January, February, and March of 2001</description>
    </summary>
	<data>
      <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>
      <month>
        <name>February 2001</name>
        <week number="1" dvds_rented="11500" />
        <week number="2" dvds_rented="12390" />
        <week number="3" dvds_rented="19050" />
        <week number="4" dvds_rented="11200" />		  
      </month>
      <month>
        <name>March 2001</name>
        <week number="1" dvds_rented="15300" />
        <week number="2" dvds_rented="12390" />
        <week number="3" dvds_rented="10050" />
        <week number="4" dvds_rented="11230" />		  
      </month>
    </data>
  </sales>