Conditional Processing

One of the more powerful features of XSL is the ability to do conditional processing. If you are familiar with any programming or scripting languages, then you are probably already familiar with conditional processing.

The idea is that you establish a condition, a test, and if that test is met, you continue with the remainder of the processing. The most common structure for conditional processing is an “if…then” structure.

XSLT does have some structures for conditional processing: if, choose, when, and otherwise.

xsl:if

The xsl:if element allows you to perform a test, and if that test turns out to be true, you can then perform some transformation. The element accepts one attribute:

  • test

The test attribute must be a boolean XPath expression. XPath will be covered in greater detail in Chapter 11. However, there are a number of functions that can be used to perform tests. For now, it's just important to know that the test either returns true, and the contents of the if element are executed, or it returns false and they are not.

For example, if we wanted to generate a list of the people in our address book, and insert a dashed line between entries; however, we did not want to have the line after the last entry, we could use:

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

<xsl:template match="contact">
  <xsl:value-of select="."/>
  <xsl:if test="position() != last()">--------</xsl:if>
</xsl:template>



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

<xsl:template match="contact">
  <xsl:value-of select="."/>
  <xsl:if test="position() != last()">--------</xsl:if>
</xsl:template>
						

xsl:choose, xsl:when, xsl:otherwise

If you are familiar with conditionals from other areas, you might also be familiar with a mechanism that allows you to choose between different cases, such as “case…switch.” The idea here is that you have a few different tests you want to perform, with a different template or transformation to perform based on which test is true.

To perform this type of conditional processing, you need to make use of three structures:

  • xsl:choose

  • xsl:when

  • xsl:otherwise

The first element, choose, is the parent element for both the when and the otherwise elements. The tests are specified in the when element using a test attribute. You can use any number of when elements as children of the choose.

The when element functions similarly to an if element, allowing you to perform a test, and if the test is true, then perform the transformation. If the test is false, then the processor moves on to evaluate the next when.

The final element in the structure is the otherwise element, which acts as a catchall. If none of the when tests turns out to be true, the transformation specified in the otherwise is performed instead. Here's what the structure looks like:

<xsl:choose> 
 <xsl:when test="Some test">
  <!-- Transformation One -->
  </xsl:when>
  <xsl:when test="Another test">
  <!-- Transformation Two -->
  </xsl:when>
  <xsl:otherwise>
  <!-- Transformation Three -->
  </xsl:otherwise>
</xsl:choose>

Just as with the if element, the tests are expressed using XPath, which is covered in more detail in Chapter 11.

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

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