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
<xsl:output method="html"/>
<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.
<td style="text-align:right"> <xsl:value-of select="format-number(@books_sold,'###,###')"/> </td>
<td> <xsl:attribute name="style"> <xsl:choose> <xsl:when test="number(@books_sold <= $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>
To view the conditionalized data
Note To change how the page appears, you can change the value assigned to the low_sales
variable. For example, change the value from 21000 to 31000 and then save Transform.xls. Return to Internet Explorer and click Refresh to see how the page changes.
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 <= $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 <= $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.