spacer

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

home / experts / javascript / column100


Web Services, Part V: XML and XSLT Programming

Formatting Numbers with XSLT

Subject Matter Expert - Managed Services (PA)
Next Step Systems
US-PA-Wayne

Justtechjobs.com Post A Job | Post A Resume
Developer News
News Flash: Adobe Has iPhone Workaround
Adobe's Flash 10.1 Goes Mobile (Minus iPhone)
A Salute to Visionary CEOs


When you want to convert numeric data in XML into formatted strings in HTML, there are several ways to do it. First, you can just copy the data as is. If, for example, the XML file includes this line:

<week number="3" dvds_rented="18000"/>

you can extract the value of the number attribute as follows:

<xsl:value-of select="@number"/>

Another way to convert numeric data is by specifying the target format. You have to use the format-number() method. The first parameter is the numeric data, while the second parameter is the format specification. You put a "#" sign for each digit, and commas to separate groups of digits. The browser will count how many digits you entered to the right of the last comma and will take it as your directive for all commas. If you put a comma to the left of three right most digits, you'll get a comma every three digits. If you put a comma to the left of two right most digits, you'll get a comma every two digits, and so on. You can allocate printing space for a six-digit number, and a comma every three digits, starting from the right hand side, as follows:

format-number(@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>
  </tr>
</xsl:for-each>
</TABLE>
</BODY>
</HTML>
</xsl:template>
</xsl:stylesheet>

Here is the output (ignore the Total column, we'll show you how to generate it on the next page):


Next: How to add up XML data entries

http://www.internet.com

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs

webref The latest from WebReference.com Browse >
Building a Banking Application Home Page with OOP · Mixing Scripting Languages · Review: phpFox, a Social Networking CMS with all the Bells and Whistles
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Enterprise 2.0: Social Networking in the Cloud · BroadSoft Marketplace Hastens Pace of Telephony Innovation · Review: HTC Hero for Sprint


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/6.html