spacer

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

home / experts / javascript / column100


Web Services, Part V: XML and XSLT Programming

Defining and Reusing Variables

Data Center Architect
The Computer Merchant, Ltd
US-MA-chelsea

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


XSLT supports variables. You can set a variable in one place and reuse it somewhere else. Use variables to make your code more flexible and well-organized. You define a variable with the xsl:param tag. You position this tag in the front section of your script, before the HEAD statament. Here is an example:

<xsl:param name="forecast" select="50000"/>

And here is an example for the whole front section:

<?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="/">
<HTML>
...
...
...

You use a variable by prefixing its name with a $ sign. For example:

$forecast

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:

<TH>Forecast</TH>

To add the forecast number for a single month can be done like this:

<td style="text-align:right;font-weight:bold">
  <xsl:value-of select="format-number($forecast, '###,###')"/>
</td>

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:param name="forecast" select="50000"/>
<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>
<TH>Forecast</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>
	<td style="text-align:right;font-weight:bold">
	  <xsl:value-of select="format-number($forecast, '###,###')"/>
	</td>
  </tr>
</xsl:for-each>
</TABLE>
</BODY>
</HTML>
</xsl:template>
</xsl:stylesheet>

Here is the output:


Next: How to implement conditional statements in XSLT

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