Updated April 10, 2023
Definition of XSLT choose
XSLT choose is defined as the if-else construct that contains a sequence of conditions with multiple variables. To go with multiple options, we are happened to use the <choose> element and tests a Boolean Expression. They assist conditional tests of complex types to use against the XML source file.
Syntax:
The Syntax declaration of XSLT choose element is given as
<xsl:choose>
<when test=” exp1”> // evaluates the true expression Statement
<otherwise> // evaluates a False Statement
</xsl:choose>
These elements have two child elements that resemble the if-else part. <xsl: when> constructs can have one or more child elements and meanwhile <xsl: otherwise> has zero or one.
How choose function works in XSLT?
The choose function adds <xsl: when >and <xsl: otherwise> to them to define multiple conditions or when we need to use a sequence of alternatives. The <choose >element works by taking the first declared <xsl: when> element that is matched is evaluated in the output. If None of the test matches then the contents of the <otherwise> is given for the output.
Even the <choose> element handles nested <when> elements with a single <otherwise> element. In the below examples we have used an input XML with various cases and XSL transform uses <choose> element to evaluate the true and false from the expressions. It falls within the template with none of the attributes.
Examples
Example #1
XML
<?xml version = "1.0"?>
<?xml-stylesheet type = "text/xsl" href = "students.xsl"?>
<Online>
<Academy enroll = "255">
<University>Neustach omnegres</University>
<Location>Germany</Location>
<Accrediation>WHCCC</Accrediation>
<Score>85</Score>
</Academy>
<Academy enroll = "255">
<University>Neustach omnegres</University>
<Location>Germany</Location>
<Accrediation>WHCCC</Accrediation>
<Score>85</Score>
</Academy>
<Academy enroll = "255">
<University>Neustach omnegres</University>
<Location>Germany</Location>
<Accrediation>WHCCC</Accrediation>
<Score>85</Score>
</Academy>
</Online>
XSL
<?xml version = "1.0" encoding = "UTF-8"?>
<xsl:stylesheet version = "1.0"
xmlns:xsl = "http://www.w3.org/1999/XSL/Transform">
<xsl:template match = "/">
<html>
<body>
<h2>Education Academy</h2>
<table border = "1.2">
<tr bgcolor = "#9acd32">
<th>enroll</th>
<th>University</th>
<th>Location</th>
<th>Accrediation</th>
<th>Score</th>
<th>Grade</th>
</tr>
<xsl:for-each select = "class/Academy">
<tr>
<td><xsl:value-of select = "@enroll"/></td>
<td><xsl:value-of select = "University"/></td>
<td><xsl:value-of select = "Location"/></td>
<td><xsl:value-of select = "Accrediation"/></td>
<td><xsl:value-of select = "Score"/></td>
<td>
<xsl:choose>
<xsl:when test = "Score > 95">
A+ Grade
</xsl:when>
<xsl:when test = "Score > 80">
B Grade
</xsl:when>
<xsl:otherwise>
Low
</xsl:otherwise>
</xsl:choose>
</td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Explanation
The above code adds a background color to the column when the test case of a score is greater than 85. And the stylesheet has two when the statement under an <choose> element.
Output:
Example #2
XML
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="sschoose.xsl" ?>
<Woods>
<Wood>
<lengthitem> Drywalll Screws
</lengthitem>
<prize>900</prize>
</Wood>
<Wood>
<lengthitem> Fastening Screws
</lengthitem>
<prize>600</prize>
</Wood>
<Wood>
<lengthitem>Hammer
</lengthitem>
<prize>400</prize>
</Wood>
</Woods>
XSL
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
<xsl:template match="Wood">
<xsl:choose>
<xsl:when test="prize < 400">
(small)
</xsl:when>
<xsl:when test="prize > 600">
(medium)
</xsl:when>
<xsl:otherwise>
(large)
</xsl:otherwise>
</xsl:choose>
<xsl:apply-templates />
<BR/>
</xsl:template>
</xsl:stylesheet>
Explanation
Here the multiple conditions I checked with the otherwise and when elements. The child elements are tested from top to bottom until a test condition met the source data or the else statement like <otherwise> is chosen.
Output:
Example #3
XML
<?xml version = "1.0"?>
<?xml-stylesheet type = "text/xsl" href = "ecommp.xsl"?>
<e-comm>
<product Category="Accessories">
<pid>154</pid>
<Description>Mobile Cell holder with sling bag</Description>
<fabric>Glass</fabric>
<price>36.99</price>
</product>
<product Category="Electronics">
<pid>164</pid>
<Description>Android Support graphics Drawing Battery-Stylus Free</Description>
<fabric> Tablets</fabric>
<price>56.99</price>
</product>
<product Category="Electronics">
<pid>154</pid>
<Description>iPhone 12 with 128GB</Description>
<fabric> Apple Store</fabric>
<price>6.99</price>
</product>
<product Category="Accessories">
<pid>574</pid>
<Description>MAC Support graphics Drawing Battery-Stylus Free</Description>
<fabric> Tablets</fabric>
<price>17.99</price>
</product>
<product Category="Fashions">
<pid>133</pid>
<Description>New Season Style Show Toppers</Description>
<fabric>Rayon</fabric>
<price>66.55</price>
</product>
<product Category="Footwear">
<pid>335</pid>
<Description>Casual shoes and sneakers</Description>
<fabric> Smart Casuals</fabric>
<price>14.99</price>
</product>
</e-comm>
XSL
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html"/>
<xsl:template match="/">
<html>
<head>
<title>XSLT multiple conditions in xslt</title>
</head>
<body>
<h2 style="color:DarkYellow; font-style:italic;">XSLT</h2>
<table border="1.5" cellpadding="6" cellspacing="0" bordercolor="Red">
<tr bgcolor="Aqua" style="color:White; font-weight:bold">
<td>Product ID</td>
<td>Name of product</td>
<td>Fabric Type</td>
<td> Price</td>
</tr>
<xsl:for-each select="e-comm/product">
<tr bgcolor="DarkSalmon" style="color:Slate Blue; font-weight:normal">
<td height="7">
<xsl:value-of select="pid"/>
</td>
<td height="7">
<xsl:value-of select="Description"/>
</td>
<td height="7">
<xsl:value-of select="fabric"/>
</td>
<xsl:choose>
<xsl:when test="price < 20">
<td height="8" style="color:Brown; font-weight:bold; font-style:italic;">
<xsl:value-of select="price"/>
</td>
</xsl:when>
<xsl:when test="price > 100 and price < 42">
<td height="8" style="color:Crimson; font-weight:bold; font-style:italic;">
<xsl:value-of select="price"/>
</td>
</xsl:when>
<xsl:otherwise>
<td height="8">
<xsl:value-of select="price"/>
</td>
</xsl:otherwise>
</xsl:choose>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Explanation
The code adds the Background color to the test condition if the prize is lesser or greater. The result of the above is given below.
Output:
Example #4
pr.xml
<?xml version="1.0"?>
<Product_list>
<Pro type="Babymilk">
<pname>Dexolac</pname>
<vitamin>76</vitamin>
<carbohydrates>82</carbohydrates>
<protein>76</protein>
<minerals>568</minerals>
</Pro>
<Pro type="Babymilk">
<pname>Nano Pro</pname>
<vitamin>62</vitamin>
<carbohydrates>45</carbohydrates>
<protein>89</protein>
<minerals>558</minerals>
</Pro>
<Pro type="Babymilk_present">
<pname>SimMomIQ</pname>
<vitamin>66</vitamin>
<carbohydrates>86</carbohydrates>
<protein>72</protein>
<minerals>632</minerals>
</Pro>
<Pro type="Babymilk">
<pname>Enfagrow</pname>
<vitamin>75</vitamin>
<carbohydrates>70</carbohydrates>
<protein>89</protein>
<minerals>810</minerals>
</Pro>
<Pro type="Babymilk_new">
<pname>Nestle</pname>
<vitamin>54</vitamin>
<carbohydrates>85</carbohydrates>
<protein>77</protein>
<minerals>56</minerals>
</Pro>
</Product_list>
pr.xsl
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="Product_list">
<table>
<tr style="background-color:##a57c6c">
<th>Pro</th>
<th>carbo (g)</th>
<th>Fiber (g)</th>
<th>cholestrol (g)</th>
<th>Energy (kj)</th>
</tr>
<xsl:for-each select="Pro">
<xsl:choose>
<xsl:when test="@type = 'Babymilk_new'">
<tr style="background-color:#40e0d0">
<td>
<xsl:value-of select="pname"/>
</td>
<td>
<xsl:value-of select="vitamin"/>
</td>
<td>
<xsl:value-of select="carbohydrates"/>
</td>
<td>
<xsl:value-of select="protein"/>
</td>
<td>
<xsl:value-of select="minerals"/>
</td>
</tr>
</xsl:when>
<xsl:when test="@type = 'Babymilk_present'">
<tr style="background-color:#d27025">
<td>
<xsl:value-of select="pname"/>
</td>
<td>
<xsl:value-of select="carbohydrates"/>
</td>
<td>
<xsl:value-of select="vitamin"/>
</td>
<td>
<xsl:value-of select="protein"/>
</td>
<td>
<xsl:value-of select="minerals"/>
</td>
</tr>
</xsl:when>
<xsl:otherwise>
<tr style="background-color:#cccccc">
<td>
<xsl:value-of select="pname"/>
</td>
<td>
<xsl:value-of select="carbohydrates"/>
</td>
<td>
<xsl:value-of select="vitamin"/>
</td>
<td>
<xsl:value-of select="protein"/>
</td>
<td>
<xsl:value-of select="minerals"/>
</td>
</tr>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</table>
</xsl:template>
</xsl:stylesheet>
Explanation
The above highlights the content of the product table with the background colors and the output looks like something as below. So here in the XSL file, we have selected an attribute product list and the value is found using ’@’. And the respective pname is highlighted with different colors. And if no pname is selected it is automatically directed to <otherwise>.
Output:
Example #5
Book details checking the condition for a prize using a single choose attribute
book.xml
<?xml version="1.0" encoding="UTF-8"?>
<bookdb>
<dept>
<bname>Data Science</bname>
<author>Thomas Cerian</author>
<country>USA</country>
<bprice>20.90</bprice>
<pubyear>1975</pubyear>
</dept>
<dept>
<bname>Artificial Itelligence</bname>
<author>Aron amrk</author>
<country>Italy</country>
<bprice>22.90</bprice>
<pubyear>1965</pubyear>
</dept>
<dept>
<bname>R Languagr</bname>
<author>Evangeline lawrence</author>
<country>German</country>
<bprice>15.90</bprice>
<pubyear>1995</pubyear>
</dept>
</bookdb>
book.xsl
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2>My movie Collection</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th>bname</th>
<th>author</th>
</tr>
<xsl:for-each select="bookdb/dept">
<tr>
<td><xsl:value-of select="bname"/></td>
<xsl:choose>
<xsl:when test="bprice > 20">
<td bgcolor="#ff00ff">
<xsl:value-of select="author"/>
</td>
</xsl:when>
<xsl:otherwise>
<td><xsl:value-of select="author"/></td>
</xsl:otherwise>
</xsl:choose>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Explanation
A Typical simple rule with the <choose> is implied with the <when> element associated with the XSL file and XML file and the output looks like below:
Output:
Advantages
- The <choose> element avoids exit or breaks statements, Unlike Switch Statement. It behaves very similarly to the Switch Statement in other Middle-Level Language.
- And doesn’t have any attributes to associate with the element.
Conclusion
Coming to an end, the <choose> element in XSL is represented in a different construction compared to other languages because it is very similar to the if-else condition statement. In this article, we have gone different construct named choose and otherwise in which XSL has a Traverse descending making in searching the condition to be met.
Recommended Articles
This is a guide to XSLT choose. Here we discuss definition, syntax, and parameters, How choose function work in XSLT? examples with code implementation. You may also have a look at the following articles to learn more –