 | Level: Introductory Nicholas Chase (nicholas@nicholaschase.com), President, Chase and Chase, Inc.
01 Aug 2002 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.
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
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
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
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. |
Rate this page
|  |