spacer
Yehuda Shiran January 3, 2002
Conditional Statements in XSLT
Tips: January 2002

Yehuda Shiran, Ph.D.
Doc JavaScript

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

XSLT supports conditional statements. It is based on a very special construct which includes three XSL tags: xsl:choose, xsl:where, and xsl:otherwise. The <xsl:choose> and </xsl:choose> are the outer tag pair. They embrace the where tags, <xsl:where> and </xsl:where>, followed by the pair <xsl:otherwise> and </xsl:otherwise>.

Let's see an example. Suppose we want to color numbers in a report according to their magnitude. We want them red if they are below a certain limit, green otherwise. The following section chooses the style attribute for one <td> cell in a table. The XML data we compare is dvds_rented, and the limit is weekly_quota:

<td>
  <xsl:attribute name="style">
    <xsl:choose>
      <xsl:when test="number(@dvds_rented <= $weekly_quota)">color:red;</xsl:when>
      <xsl:otherwise>color:green;</xsl:otherwise>
    </xsl:choose>
    text-align:right;
  </xsl:attribute>
  <xsl:value-of select="format-number(@dvds_rented, '###,###')"/> 
</td>
The following XSLT file converts 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:param name="weekly_quota" select="12000"/>
<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>
         <xsl:attribute name="style">
           <xsl:choose>
             <xsl:when test="number(@dvds_rented <= $weekly_quota)">color:red;</xsl:when>
             <xsl:otherwise>color:green;</xsl:otherwise>
           </xsl:choose>
           text-align:right;
         </xsl:attribute>
         <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 colorful report:

Here is the XML file:

<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="020103.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>

People who read this tip also read these tips:

Look for similar tips by subject:


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