XSLT Choose (if-else) Tutorial 🎯

beginner
5 min

XSLT Choose (if-else) Tutorial 🎯

Welcome to the XSLT Choose (if-else) tutorial! In this lesson, we'll dive into one of the most fundamental concepts in XSLT: using conditional statements to manipulate XML data. By the end of this tutorial, you'll be able to create powerful and dynamic XSLT transformations. Let's get started! 📝

Introduction 💡

XSLT (Extensible Stylesheet Language Transformations) is a language used for transforming XML documents into other formats such as HTML, JSON, or plain text. XSLT provides various features to manipulate and format XML data, and one of the most important features is the Choose (if-else) function.

Understanding the XSLT Choose (if-else) Function 📝

The XSLT Choose (if-else) function is used to evaluate conditions and perform different actions based on the results. It consists of one or more <xsl:choose> elements, each containing a condition and an action. If the condition evaluates to true, the corresponding action is executed.

Here's the basic structure of the Choose (if-else) function:

xml
<xsl:choose> <xsl:when test="condition1"> <!-- action for condition1 --> </xsl:when> <xsl:when test="condition2"> <!-- action for condition2 --> </xsl:when> ... <xsl:otherwise> <!-- action for all other conditions --> </xsl:otherwise> </xsl:choose>

The <xsl:when> Element 📝

The <xsl:when> element defines a condition and the corresponding action to be executed if the condition evaluates to true. The condition is specified using the test attribute.

xml
<xsl:when test="condition"> <!-- action for condition --> </xsl:when>

The <xsl:otherwise> Element 📝

The <xsl:otherwise> element defines the action to be executed if none of the <xsl:when> conditions evaluate to true.

xml
<xsl:otherwise> <!-- action for all other conditions --> </xsl:otherwise>

Example: Sorting Products Based on Price 🎯

Let's consider an XML document representing a list of products:

xml
<products> <product id="1"> <name>Product A</name> <price>10.99</price> </product> <product id="2"> <name>Product B</name> <price>7.99</price> </product> <product id="3"> <name>Product C</name> <price>15.99</price> </product> </products>

We want to create an XSLT transformation that sorts the products based on their price and outputs the result in an HTML table.

xml
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <html> <head> <title>Sorted Products</title> </head> <body> <table border="1"> <tr> <th>Name</th> <th>Price</th> </tr> <xsl:apply-templates select="products/product"/> </table> </body> </html> </xsl:template> <xsl:template match="product"> <xsl:variable name="price" select="price"/> <xsl:variable name="index" select="count(preceding-sibling::product) + 1"/> <xsl:choose> <xsl:when test="$price > 15"> <xsl:call-template name="high-priced-product"> <xsl:with-param name="index" select="$index"/> <xsl:with-param name="name" select="name"/> <xsl:with-param name="price" select="$price"/> </xsl:call-template> </xsl:when> <xsl:when test="$price > 10"> <xsl:call-template name="medium-priced-product"> <xsl:with-param name="index" select="$index"/> <xsl:with-param name="name" select="name"/> <xsl:with-param name="price" select="$price"/> </xsl:call-template> </xsl:when> <xsl:when test="$price <= 10"> <xsl:call-template name="low-priced-product"> <xsl:with-param name="index" select="$index"/> <xsl:with-param name="name" select="name"/> <xsl:with-param name="price" select="$price"/> </xsl:call-template> </xsl:when> </xsl:choose> </xsl:template> <xsl:template name="high-priced-product"> <tr> <td><xsl:value-of select="$name"/></td> <td><xsl:value-of select="$price"/></td> </tr> </xsl:template> <xsl:template name="medium-priced-product"> <tr style="background-color: #ffe6e6;"> <td><xsl:value-of select="$name"/></td> <td><xsl:value-of select="$price"/></td> </tr> </xsl:template> <xsl:template name="low-priced-product"> <tr style="background-color: #e6ffe6;"> <td><xsl:value-of select="$name"/></td> <td><xsl:value-of select="$price"/></td> </tr> </xsl:template> </xsl:stylesheet>

In this example, we define three templates (high-priced-product, medium-priced-product, and low-priced-product) to handle different price ranges and output the products accordingly.

Quiz 💡

Quick Quiz
Question 1 of 1

In the XSLT Choose (if-else) function, which element defines a condition and the corresponding action to be executed if the condition evaluates to `true`?

That's it for today! In the next lesson, we'll dive deeper into XSLT and explore more advanced concepts. Happy coding! 💡