Developer

Use the power of conditional processing in XSLT

Many developers don't appreciate XSLT's built-in conditional processing capabilities. See how you can manipulate your XML document based on the attributes it contains with this potent technique.


Most developers I know don't regard XSLT as a programming language, but a quick perusal of its many features suggests otherwise. Although it is not as powerful as languages like Java and C#, XSLT does contain basic features for working with XML documents.

One element it shares with other languages is conditional processing. This encompasses performing repetitive tasks or choosing an action based upon data values. Let's take a closer look at this XSLT feature, which includes the for-each, if, and choose directives.

I'll use the XML in Listing A throughout the examples presented in this article. The XML includes Web sites with a title, URL, description, and an arbitrary type for each element. Each sample XSLT will be saved in a file called test.xsl, and I've referenced this in the second line of the XML document.

Processing multiple values
Looping allows an action to be performed multiple times. It is usually applied to multiple items with similar data types. One basic looping structure is the for statement. It processes each individual element in a collection of elements. This functionality is available in XSLT with the for-each element, which has the following syntax:
<xsl:for-each select=expression>
<!—do something with each element
</xsl:for-each>


The expression attribute utilizes XPath syntax to return a set of nodes to be processed in the for-each body. All nodes returned are the same type. The example in Listing B displays the contents of the sample XML document using the for-each element.

The stylesheet processes each site element within the root sites node. The title is displayed in bold and the URL is displayed below this title. This allows each node in a collection to be easily processed. You may want to add extra functionality by displaying only data that meets certain criteria.

Checking a value
One of the most fundamental decision-making functions in programming is the if statement. Basically, it says perform some action only if a condition is met. The basic syntax of the if statement is:
<xsl:if test=Boolean-expression>
<!—- peform an action
</xsl:if>


In Listing C, I've altered the previous example and used an if statement to display only those elements with a type element set to News.

The result of this code is the display of only one site. The test attribute of the xsl:if element uses an XPath Boolean expression to return a true or false value. The example tests the value of the corresponding type element, and the comparison value is enclosed in apostrophes since it's a string value. You can use any valid XPath expression in the test attribute. For example, the following xsl:if element displays an end-of-output marker if the current element is the last:
<xsl:if test="postion()=last()">
<p>—————the end——————-</p>
</xsl:if>


One curious omission is an else condition for the xsl:if element. This condition would allow an alternate operation to be performed if the if condition were false. This is a standard aspect of most programming languages, but it does not exist in the current XSL implementation. However, this functionality is available with the xsl:choose element.

Make a decision
The xsl:choose element lets you perform an operation based on an XPath expression and then perform another operation if the test condition is false. Here is the syntax:
<xsl:choose>
<xsl:when test=condition>
<!—do something
</xsl:when>
<xsl:otherwise>
<!—do something
</xsl:otherwise>
</xsl:choose>


The element contains two child node types: xsl:when and xsl:otherwise. The xsl:when resembles the xsl:if element; xsl:otherwise provides the functionality of an else statement. You can also have multiple xsl:when statements. This allows you to check multiple conditions and perform a default operation (via xsl:otherwise) if all xsl:when statements fail. This setup resembles the case statement found in most programming languages. In Listing D, I replaced the xsl:if statement from the previous example with an xsl:choose element and displayed elements in different colors depending on their type.

The result is that all records whose type is News are displayed in red. It is a basic example, but it does showcase the ability to make decisions using the data from the XML document.

Programming with XML
A lot of developers don't recognize the true programming aspects of XSLT, but it offers many of the base elements of a programming language, including flow control and decision making. The xsl:for-each, xsl:if, and xsl:choose elements allow XML data to be the basis for formulating the results of the transformation.

Editor's Picks

Free Newsletters, In your Inbox