Tip: Make choices at runtime with XSLT parameters

Use parameters and conditionals in your style sheets

Extensible Stylesheet Langauage Transformations provide the ability to perform sophisticated manipulation of data as it is transformed from one form to another. You can increase their capabilites even further through the use of parameters that can be specified at runtime. This tip takes a basic look at using parameters and conditional statements in an XSLT style sheet.

Nicholas Chase (nicholas@nicholaschase.com), President, Chase and Chase, Inc.

Nicholas Chase has been involved in Web site development for companies such as Lucent Technologies, Sun Microsystems, Oracle, and the Tampa Bay Buccaneers. Nick has been a high school physics teacher, a low-level radioactive waste facility manager, an online science fiction magazine editor, a multimedia engineer, and an Oracle instructor. More recently, he was the Chief Technology Officer of Site Dynamics Interactive Communications in Clearwater, Florida, USA, and is the author of three books on Web development, including Java and XML From Scratch (Que) and the upcoming Primer Plus XML Programming (Sams). He loves to hear from readers and can be reached at nicholas@nicholaschase.com.



01 August 2002

Also available in Japanese

Note:This tip uses the Xalan XSL Transformation engine, but any XSLT processor will do. It assumes that you are familiar with XSL transformations.

The style sheet

In this tip, I take a single style sheet and repurpose it to provide different results depending on the parameter values entered by the user when the document is actually transformed. The style sheet takes an XML document and transforms it into an HTML document displaying the results of a dog show. The basic style sheet creates a page with information in tables:

Listing 1: The basic style sheet
<?xml version="1.0"?> 
<xsl:stylesheet 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    version="1.0">

<xsl:template match="/">
  <xsl:apply-templates/>
</xsl:template>

<xsl:template match="results">
<html>
<head>
<title><xsl:value-of select="@showName" /></title>
<style>
    * { font-family: Verdana }
    td { font-size: 10pt }
    .groupName { font-weight: bold; }
</style>
</head>
<body>
  <h2 align="center">
    <xsl:value-of select="@showName" />
  </h2>
  <h4 align="center">
      Show Date: <xsl:value-of select="@showDate" />
  </h4>
  <h4 align="center">
    <xsl:value-of select="@location" />
  </h4>
  <xsl:apply-templates/>
</body>
</html>
</xsl:template>

<xsl:template match="group">

<p class="groupName"><xsl:value-of select="@name"/></p>

<table align="center" width="75%">
<tr><td width="10%">
    1st:</td><xsl:apply-templates select="first"/></tr>
<tr><td width="10%">
    2nd:</td><xsl:apply-templates select="second"/></tr>
<tr><td width="10%">
    3rd:</td><xsl:apply-templates select="third"/></tr>
<tr><td width="10%">
    4th:</td><xsl:apply-templates select="fourth"/></tr>
</table> 

</xsl:template>

<xsl:template match="first|second|third|fourth">
    <td width="40%"><xsl:value-of select="breed"/></td>
    <td width="50%"><xsl:value-of select="dog"/></td>
</xsl:template>

<xsl:template match="group[@name='Best In Show']">
    <p align="center">
        <b>Best In Show:</b>
        <xsl:text>  </xsl:text>
        <xsl:value-of select="first/dog" />
        (<xsl:value-of select="first/breed" />)
    </p>
</xsl:template>
</xsl:stylesheet>

The result is a Web page like the one shown in Figure 1.

Figure 1. An HTML document displaying the results of a dog show
HTML document displaying results of a dog show

You can add a parameter to an XSLT style sheet inside or outside a template. In this case, you're adding a single winnersOnly parameter, with an initial value of no.

Listing 2: Adding the parameter
<?xml version="1.0"?> 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

<xsl:param name="winnersOnly">no</xsl:param>

<xsl:template match="/">
        <xsl:apply-templates/>
</xsl:template>

<xsl:template match="results">
...
<h4 align="center"><xsl:value-of select="@location" /></h4>
<h1>The value of winnersOnly is "<xsl:value-of
select="$winnersOnly"/>"</h1>

<xsl:apply-templates/>
 
</body>
</html>
</xsl:template>

You specify the parameter with an initial value of no, but you can specify a different value at runtime. Whatever value you choose, it can be accessed by using the $winnersOnly notation in a value-of element just as though it were a typical XPath expression.

Using Xalan, you could run this transformation using the command:

java org.apache.xalan.xslt.Process -in scores.xml -xsl scores.xsl 
-out results.html -param winnersOnly yes

The result is that the value gets set and you can output it to the page, as in Figure 2.

Figure 2. Result output with parameter value of yes
Result output with parameter value of yes

Using the xsl:if element

Now that you have the parameter, what can you do with it? Besides outputting it as part of the result document, you can use it to make choices. For example, you can use a simple if element to specify that you want specific text output if the winnersOnly parameter is set to yes.

Listing 3: Using the xsl:if element
... 
<h4 align="center"><xsl:value-of select="@location" /></h4>
<xsl:if test="$winnersOnly='yes'">
    <h2 align="center">WINNERS</h2>
</xsl:if>
<xsl:apply-templates/>
...

You're checking to determine whether a particular condition, or test, is true. It just so happens that the condition depends on the value of the parameter. If $winnersOnly='yes' is true, then the content will be evaluated, or in this case, displayed.


Using the xsl:choose element

The if element is handy for simple choices, but there's no else element, which makes it difficult to use in complex situations. That's not to say, however, that you can't perform an if-then-else type of test. You simply have to use the xsl:choose element instead:

Listing 4: Using the xsl:choose element
_
<xsl:template match="group">
<xsl:choose>
<xsl:when test="$winnersOnly='yes'"> <p align="center"> <b><xsl:value-of select="@name"/>:</b> <xsl:text> </text> <xsl:value-of select="first/dog" /> (<xsl:value-of select="first/breed" />) </p> </xsl:when>
<xsl:otherwise> <p class="groupName"><xsl:value-of select="@name"/></p> <table align="center" width="75%"> <tr><td width="10%">1st:</td><xsl:apply-templates select="first"/></tr> <tr><td width="10%">2nd:</td><xsl:apply-templates select="second"/></tr> <tr><td width="10%">3rd:</td><xsl:apply-templates select="third"/></tr> <tr><td width="10%">4th:</td><xsl:apply-templates select="fourth"/></tr> </table> </xsl:otherwise>
</xsl:choose> </xsl:template> _

In this case, if the test evaluates to true, then the when block of content is evaluated and displayed. If not, the otherwise block comes into play instead.

This time, if you change the winnersOnly parameter to yes, you can see the results, as in Figure 3.

Figure 3. Result output with use of choose element
Result output with use of choose element

Summary

In this tip, you've taken a very basic look at using parameters and conditional statements in an XSLT style sheet. The advantage of parameters is that you can specify them at runtime, but this capability isn't limited to transformations performed from the command line. With most engines, you can specify transformations when performing them programmatically, as well.

Resources

Comments

developerWorks: Sign in

Required fields are indicated with an asterisk (*).


Need an IBM ID?
Forgot your IBM ID?


Forgot your password?
Change your password

By clicking Submit, you agree to the developerWorks terms of use.

 


The first time you sign into developerWorks, a profile is created for you. Information in your profile (your name, country/region, and company name) is displayed to the public and will accompany any content you post, unless you opt to hide your company name. You may update your IBM account at any time.

All information submitted is secure.

Choose your display name



The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerWorks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

Required fields are indicated with an asterisk (*).

(Must be between 3 – 31 characters.)

By clicking Submit, you agree to the developerWorks terms of use.

 


All information submitted is secure.

Dig deeper into XML on developerWorks


static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=XML
ArticleID=12141
ArticleTitle=Tip: Make choices at runtime with XSLT parameters
publish-date=08012002