Step 4: Adding Conditional Statements
You can add a sales goal to the page, and use colored text to indicate which sales figures exceed, and which fall below, the sales goal. You do this by adding a conditional statement. A conditional statement checks a piece of data and decides how to format it based on its value. Here you will define a quarterly sales goal and then use a conditional statement to see if the sales amount is greater or less than the goal. Figures below the sales goal will appear in red; figures above the sales goal will appear in green.
To add conditional statements
Open Transform.xsl in your HTML editor.
Immediately after <xsl:output method="html"/>, add the following text.
<xsl:param name="low_sales" select="21000"/>
This line creates a variable parameter named low_sales, which represents the sales goal, and assigns it the value of 21000. Now you can use the name low_sales whenever you want to refer to the sales goal. Setting a variable in one place and reusing it makes for flexible, well-organized code.
Find and delete the following text.
<td style="text-align:right"> <xsl:value-of select="format-number(@books_sold,'###,###')"/> </td>
Replace with the following text.
<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>
Save and close Transform.xsl.
To view the page in Internet Explorer, double-click Sales.xml.
Your result should look like this:
Note You can change the value assigned to the low_sales parameter in Transform.xsl to change how the page appears. For example, you can change the sales goal from
31000. To display the result, save Transform.xls, return to Internet Explorer, and click Refresh.
How It Works
The key to this is the <xsl:choose> command, which tells the parser that a conditional statement is coming. If you look just after the <xsl:choose> command, you will see the line <xsl:when test="number(@books\_sold <= $low_sales)">color:red;</xsl:when>. The command xsl:when tells Internet Explorer that a choice is coming up. The text test="number(@books\_sold <= $low_sales)" tells Internet Explorer what the choice is. In this case, Internet Explorer is told to choose every case in which the value of books_sold in the <quarter> tag is less than the value in the low_sales parameter. The text color:red tells Internet Explorer to format the sales number as red text when the condition is met. The text </xsl:when> tells the parser that the xsl:when command is finished. The <xsl:otherwise>color:green;</xsl:otherwise> tag tells Internet Explorer what to do when the condition is not met.
Finally, notice that the style sheet property that controls the alignment of the column data has been moved into the definition for the style attribute, after the </xsl:choose> tag. This insures that all of the style attributes are contained in the same style attribute.
Last updated on Saturday, April 10, 2004
© 1992-2003 Microsoft Corporation. All rights reserved.