January 2, 2002 - Avoiding Hard-Coded Numbers in XSLT | WebReference

January 2, 2002 - Avoiding Hard-Coded Numbers in XSLT

Yehuda Shiran January 2, 2002
Avoiding Hard-Coded Numbers in XSLT
Tips: January 2002

Yehuda Shiran, Ph.D.
Doc JavaScript

You define variables in XSLT in the upper portion of the file, before the HTML tag, and then you can reuse it anywhere in the file. Let's add a new column to our MyDVD Rental Store's report. This column will show the sales forecast for each month. Let's also fix the forecast at 50,000 per month, for all months. It makes sense to define a variable called forecast and set it to 50,000. During printing, we don't have to repeat the number three times -- we use the variable forecast.

To add the column header, Forecast, is straightforward:

To add the forecast number for a single month may be done like here:

<td style="text-align:right;font-weight:bold">
  <xsl:value-of select="format-number($forecast, '###,###')"/>
Here is an XSL file that will convert the XML file at the bottom of this tip (try it):

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="html"/>
<xsl:param name="forecast" select="50000"/>
<xsl:template match="/">
<TITLE><xsl:value-of select="//summary/heading"/></TITLE>
<H1><xsl:value-of select="//summary/heading"/></H1>
<H2><xsl:value-of select="//summary/subhead"/></H2>
<P><xsl:value-of select="//summary/description"/></P>
<xsl:for-each select="//data/month[1]/week">
<TH>W<xsl:value-of select="@number"/></TH>
<xsl:for-each select="//data/month">
    <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 style="text-align:right;font-weight:bold">
	  <xsl:value-of select="format-number(sum(week/@dvds_rented), '###,###')"/>
	<td style="text-align:right;font-weight:bold">
	  <xsl:value-of select="format-number($forecast, '###,###')"/>
Here is the output:
Here is the XML file:

<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="020102.xsl"?>
      <heading>MyDVD Rental Store</heading>
      <subhead>Periodical Sales Report</subhead>
      <description>Sales Report for January, February, and March of 2001</description>
        <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" />		  
        <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" />		  
        <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" />