Introduction to XSLT if else
XSL provides conditional patterns capabilities statements which may be simple or complex. XSLT if-else elements contain a sequence of conditions with the expression values that work when a condition is true with the <xsl: when> statement. this <xsl:when> specifies unlimited instructions. XSL specifications implies a function<xsl: choose> to implement the functionality of if-else statements. To work with multiple options, we are supposed to use <choose> along with <otherwise> which is equivalent to anything else. The attribute is evaluated until the processor finds the condition that is met true. If the statement checks only one condition. <xsl: if> function doesn’t support else keyword on it.
Syntax:
if-else statement syntax comes with when and otherwise which is shown below:
<xsl: choose> // Executes if part
<xsl:when test="$Createdtime > $checkDate"> // executes then part
<h2>suppose </h2>
</xsl:when>
<xsl:otherwise> // Executes else part.
<h2>welcome</h2>
</xsl:otherwise>
</xsl: choose>
The above statement has two child types when and otherwise. when resembles if part and otherwise functions as an else-part.
How if-else statement work in XSLT?
XSLT has a potent technique when we happened to manipulate the XML document with conditional patterns. <if> Statement, one of the best decision-making parts of a programming language, and the test attribute gives Boolean value. There is no direct declaration on the if-else statement. Instead, we use to choose a keyword to process the statement. The operation is based on an Xpath expression if the respective condition is met false. In simple terms, we can use a choose-when-otherwise construct as shown previously. The above Syntax is equivalent to the if-else code here:
if ($Createdtime > $checkDate){output: <h2> Suppose</h2>}else if ($Createdtime = $checkDate){output: <h2>Welcome to my page</h2>}else{output: <h2>Exit the Page</h2>}
sample 2
<xsl:choose>
<xsl:when test="$tonum = '6'">
<xsl:variable tname="dType" select="Thursday"/>
</xsl:when>
<xsl:when test="$tonum = '7'">
<xsl:variable tname="dType" select="Friday"/>
</xsl:when>
<xsl:otherwise></xsl:otherwise>
</xsl:choose>
The other way of using the if-else condition with a single <if> statement is shown below:
<xsl:if match="employee[EName='Gabby john']">
</xsl:if>
An else would be followed as
<xsl:if match="employee[EName!='Gabby john']">
</xsl:if>
Even we can use nest xsl<if> inside xsl:when to enable further filter options.
<xsl:choose>
<xsl:when test="the given statement">
<xsl:when test="pub[@name='The third Race case'][@status='closed'][@labours in > 2]">
<xsl:if test="@exittime = 'final orders'">get to the engine as soon</xsl:if>
</xsl:when>
<xsl:otherwise...
</xsl:choose>
This is a great Performance booster with a conditional Structure.
Examples
In this section, we shall see a few examples of the if-else pattern which is the simplest function to implement using <choose> construct. The XSL specification is given below:
Example #1
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 Book 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 applied associated with the XSL file and XML file and the output looks like below:
Output:
Example #2
booky.xml
<?xml version="1.0" encoding="UTF-8"?>
<bookdb>
<dept>
<bname>Python-Basic Learning</bname>
<author>Gabby Kaate</author>
<country>USA</country>
<bprice>9.90</bprice>
<pubyear>1975</pubyear>
</dept>
<dept>
<bname>Xml and XSLT</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>
pr.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 Book database</h2>
<table border="2">
<tr bgcolor="red">
<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 >= 10">
<td bgcolor="blue">
<xsl:value-of select="author"/>
</td>
</xsl:when>
<xsl:when test="bprice > 16">
<td bgcolor="yellow">
<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
The above code uses the Book.xml file to select the conditions based on the price.
Output:
Example #3
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:
Conclusion
Coming to an end, an if-else statement in XSL is represented in a different construction compared to other languages. In this article, we have gone different construct named choose and otherwise in which XSL has a Hierarchical descending making in searching the position or the exact value. This mechanism leads to bad practice to deliver an exact output but it depends on the circumstances.
Recommended Articles
This is a guide to XSLT if else. Here we discuss How if-else statement work in XSLT along with the examples and outputs. You may also have a look at the following articles to learn more –