Conditional Processing

Controlling choices in XSLT as to how and whether a node is to be processed falls into two categories:

  • Choice of two processing alternatives, one of which is to do nothing

  • Choice of multiple (greater than two) options

The xsl:if Element

The xsl:if element in XSLT corresponds broadly to if...then... else type statements in other programming languages, but in XSLT there is no else option. If you want an else option, you must use xsl:choose, described later in this chapter.

An xsl:if element is always nested within an xsl:template element. XSLT elements that are nested within templates are termed instructions or instruction elements.

The general form is like this:

<xsl:template> 
<!-- Other content can go here. --> 
<xsl:if test="XPathExpression"> 
<!-- Anything in here is executed if the test attribute 
 returns true. --> 
</xsl:if> 
<!-- Other content can go here. --> 
</xsl:template> 

The value of the test attribute is converted to a Boolean value. If the result is true, the content of the xsl:if element is instantiated. If the result is false, the content of the xsl:if element is skipped.

The following list summarizes the rules for conversion to Boolean values:

  • If the expression is a node-set, the Boolean value true is returned if the node-set contains one or more nodes.

  • If the expression is a number, the Boolean value returned is true if the number is not zero.

  • If the expression is a string, the Boolean value returned is true if the string is not the empty string.

Some possible uses of xsl:if can be more succinctly expressed using a predicate.

For example, if you wanted to specify that a document was to be included in a results document only if a version attribute had the value final, you could use xsl:if inside a template that matched the Document element node:

<xsl:template match="Document"> 
<!-- All Document elements get to here. --> 
<xsl:if test="@version='final'"> 
<!-- Conditional processing of final version documents only 
     goes here. --> 
</xsl:if> 
<!-- Any other processing that applies to all Document element 
     nodes could go here. --> 
</xsl:template> 

However, you could just as easily control things using a predicate [@version="final"] in the location path in an xsl:apply-templates element’s select attribute.

However, when you want to process all Document element nodes but process them differently depending on whether they are final or draft, you can make use of the xsl:if element. Listing 12.1 shows an example XML source document.

Listing 12.1. Documents.xml: An XML Data Store Containing Final and Draft Documents
<?xml version='1.0'?> 
<Documents> 
 <Document version="outdated"> 
  <Title>XMML.com Training Courses</Title> 
  <Author>Karen Karenstein</Author> 
  <Date>1999/12/20</Date> 
  <Content>December 1999 content.</Content> 
 </Document> 
 <Document version="final"> 
  <Title>XMML.com Training Courses</Title> 
  <Author>Camilla Zukowski</Author> 
  <Date>2002/12/29</Date> 
  <Content>December 2002 content.</Content> 
 </Document> 
 <Document version="draft"> 
  <Title>XMML.com Training Courses</Title> 
  <Author>Camilla Zukowski</Author> 
  <Date>2002/07/31</Date> 
  <Content>July 2002 draft content.</Content> 
 </Document> 
 <Document version="final"> 
  <Title>XMML.com Consultancy Services</Title> 
  <Author>Paul Hartington</Author> 
  <Date>2003/04/29</Date> 
  <Content>April 2003 consultancy services 
   information.</Content> 
 </Document> 
</Documents> 

Listing 12.2 is an XSLT stylesheet that outputs the information about final version documents with a red h1 header and full information about the document author, together with the document content. For documents with a version attribute equal to draft or outdated, the document title is output in an h2 element in blue and only the document status is output for each such document.

Listing 12.2. Documents.xsl: An XSLT Stylesheet Using the xsl:if Element
<?xml version='1.0'?> 
<xsl:stylesheet 
 version="1.0"  
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
  > 
<xsl:output method="html" 
 indent="yes" /> 
<xsl:template match="/"> 
<html> 
<head> 
<title>XMML.com Documents</title> 
<style type="text/css"> 
h2{color:red} 
h3{color:blue} 
</style> 
</head> 
<body> 
<h1>All XMML.com documents.</h1> 
<xsl:apply-templates select="//Document" /> 

</body> 
</html> 
</xsl:template> 

<xsl:template match="Document"> 
<xsl:if test="@version='final'"> 
<h2>Document Title:<xsl:text> </xsl:text><xsl:value-of select= 
  "Title" /></h2> 
</xsl:if> 
<xsl:if test="not(@version='final')"> 
<h3>Document Title:<xsl:text> </xsl:text><xsl:value-of select= 
  "Title" /></h3> 
</xsl:if> 
<p>Document Status:<xsl:text> </xsl:text><xsl:value-of select= 
  "@version"/></p> 
<xsl:if test="@version='final'"> 
<p>Document Author:<xsl:text> </xsl:text><xsl:value-of select= 
  "Author"/></p> 
<p>Document Content:<xsl:text> </xsl:text><b><xsl:value-of 
select= 
  "Content"/></b></p> 
</xsl:if> 
</xsl:template> 

</xsl:stylesheet> 

Listing 12.3 contains the output from the XSLT transformation.

Listing 12.3. Documents.html: The Output Document After Applying Listing 12.2 to Listing 12.1
<html> 
   <head> 
      <meta http-equiv="Content-Type" content="text/html; 
       charset=utf-8"> 

      <title>XMML.com Documents</title><style type="text/css"> 
h2{color:red} 
h3{color:blue} 
</style></head> 
   <body> 
      <h1>All XMML.com documents.</h1> 
      <h3>Document Title: XMML.com Training Courses</h3> 
      <p>Document Status: outdated</p> 
      <h2>Document Title: XMML.com Training Courses</h2> 
      <p>Document Status: final</p> 
      <p>Document Author: Camilla Zukowski</p> 
      <p>Document Content: <b>December 2002 content.</b></p> 
      <h3>Document Title: XMML.com Training Courses</h3> 
      <p>Document Status: draft</p> 
      <h2>Document Title: XMML.com Consultancy Services</h2> 
      <p>Document Status: final</p> 
      <p>Document Author: Paul Hartington</p> 
      <p>Document Content: <b>April 2003 consultancy services 
        information. </b></p> 
   </body> 
</html> 

Figure 12.1 shows Listing 12.3 displayed in the Internet Explorer 5.5 browser.

Figure 12.1. Differential display of documents depending on the value of the version attribute.


To use only xsl:if elements to achieve output like this can be a little clumsy at times. Let’s modify the output by using the xsl:choose element in an XSLT transformation.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset