Microsoft XML Core Services (MSXML) 5.0 for Microsoft Office - XSLT Developer's Guide

Control the Flow of Transformation

Flow control allows your program, or transformation, to handle different tasks based on different conditions. To implement flow control in XSLT, you use xsl:if or xsl:choose. The analogous statements in Visual Basic are If ...Then ... End If and IF ... Then ... Else ... End If. The analogous statements in C/C++ and JScript are if (?) {...} and if (?) { ... } else { ... }.

In this exercise, you will define a quarterly sales goal. You will then add an <xsl:choose> conditional statement to determine whether a region meets this goal for a specified quarter. When a region meets its goal for a quarter, the sales figure will be displayed in green. Otherwise, the sales figure will be displayed in red. To accomplish this, you will add a style attribute to the <td> HTML tag. This attribute will be set to green if the sales goal is met, and set to red if it is not.

To add conditional statements

  1. In your HTML or text editor, open Transform.xsl and locate the following line:
    <xsl:output method="html"/>
  2. Immediately below this line, add the following code:
    <xsl:param name="low_sales" select="21000"/>

    This creates a parameter variable named low_sales, which represents the sales goal, and assigns it the value of 21000. To reference this parameter, use $low_sales in any ensuing XPath expressions. Now, you can insert the parameter variable low_sales whenever you want to refer to the sales goal. Setting a variable enables you to change the value of the variable in one place and update its value consistently throughout the file.

  3. Find and select the following code:
    <td style="text-align:right">
       <xsl:value-of select="format-number(@books_sold,'###,###')"/>
    </td>
  4. Replace it with the following code:
    <td>
       <xsl:attribute name="style">
          <xsl:choose>
             <xsl:when test="number(@books_sold &lt;= $low_sales)">
                   color:red;</xsl:when>
             <xsl:otherwise>color:green;</xsl:otherwise>
          </xsl:choose>
          text-align:right;
       </xsl:attribute>
       <xsl:value-of select="format-number(@books_sold,'###,###')"/>
    </td>
  5. Save Transform.xsl. You can leave the file open.

To view the conditionalized data

How the Conditional Statement Works

The code that you just added to Transform.xsl uses a combination of the <xsl:choose>, <xsl:when>, and <xsl:otherwise> elements to form a set of conditional statements.

      <xsl:choose>
         <xsl:when test="number(@books_sold &lt;= $low_sales)">
               color:red;</xsl:when>
         <xsl:otherwise>color:green;</xsl:otherwise>
      </xsl:choose>

The <xsl:choose> element declares that the enclosed child elements are conditional statements. The first child element, <xsl:when test="number(@books_sold &lt;= $low_sales)">color:red;</xsl:when>, states that if the value of the books_sold attribute in the <quarter> element is smaller than or equal to the value of the low_sales variable, color:red will be assigned to the style attribute of the <td> HTML element. This causes the number to be output in red. If the books_sold value is greater than the low_sales value, the next statement, <xsl:otherwise>color:green;</xsl:otherwise>, sets the output color to green instead of red.

The next exercise shows how to Process Multiple XML Documents.