Skip to main content

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

The first time you sign into developerWorks, a profile is created for you. Select information in your developerWorks profile is displayed to the public, but you may edit the information at any time. Your first name, last name (unless you choose to hide them), and display name will accompany the content that you post.

All information submitted is secure.

  • Close [x]

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.

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

All information submitted is secure.

  • Close [x]

Tip: Make choices at runtime with XSLT parameters

Use parameters and conditionals in your style sheets

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.

Summary:  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.

View more content in this series

Date:  01 Aug 2002
Level:  Introductory
Also available in:   Japanese

Activity:  35129 views
Comments:  

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

About the author

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.

Report abuse help

Report abuse

Thank you. This entry has been flagged for moderator attention.


Report abuse help

Report abuse

Report abuse submission failed. Please try again later.


developerWorks: Sign in


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. Select information in your developerWorks profile is displayed to the public, but you may edit the information at any time. Your first name, last name (unless you choose to hide them), and display name will accompany the content that you post.

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.

(Must be between 3 – 31 characters.)

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

 


Rate this article

Comments

Help: Update or add to My dW interests

What's this?

This little timesaver lets you update your My developerWorks profile with just one click! The general subject of this content (AIX and UNIX, Information Management, Lotus, Rational, Tivoli, WebSphere, Java, Linux, Open source, SOA and Web services, Web development, or XML) will be added to the interests section of your profile, if it's not there already. You only need to be logged in to My developerWorks.

And what's the point of adding your interests to your profile? That's how you find other users with the same interests as yours, and see what they're reading and contributing to the community. Your interests also help us recommend relevant developerWorks content to you.

View your My developerWorks profile

Return from help

Help: Remove from My dW interests

What's this?

Removing this interest does not alter your profile, but rather removes this piece of content from a list of all content for which you've indicated interest. In a future enhancement to My developerWorks, you'll be able to see a record of that content.

View your My developerWorks profile

Return from help

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
author1-email=nicholas@nicholaschase.com
author1-email-cc=

Tags

Help
Use the search field to find all types of content in My developerWorks with that tag.

Use the slider bar to see more or fewer tags.

For articles in technology zones (such as Java technology, Linux, Open source, XML), Popular tags shows the top tags for all technology zones. For articles in product zones (such as Info Mgmt, Rational, WebSphere), Popular tags shows the top tags for just that product zone.

For articles in technology zones (such as Java technology, Linux, Open source, XML), My tags shows your tags for all technology zones. For articles in product zones (such as Info Mgmt, Rational, WebSphere), My tags shows your tags for just that product zone.

Use the search field to find all types of content in My developerWorks with that tag. Popular tags shows the top tags for this particular content zone (for example, Java technology, Linux, WebSphere). My tags shows your tags for this particular content zone (for example, Java technology, Linux, WebSphere).

Special offers