Conditional Processing

XSLT provides two methods for processing based on a set of conditions. Those methods involve the following two elements:

  • <xsl:if>

  • <xsl:choose>

The <xsl:if> element operates like the if ... then construct typically found in programming and scripting languages. You specify the condition for the if statement using the test attribute. The condition results in a Boolean expression that returns either True or False. If the condition is True, the processing is carried out. For example:

<xsl:template match=”product/price”> 
  <xsl:if test=”number()”> 
      $ 
   </xsl:if> 
  <xsl:value-of /> 
</xsl:template> 

To see how this might work in a database application, consider the XML document in Listing 4.8, which represents the results of a database ...

Get Special Edition Using XSLT now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.