XSLT choose

XSLT <xsl:choose> Element

To express multiple conditional tests, we can use the <xsl:choose> element in conjunction with <xsl:when> and <xsl:otherwise>.

Syntax:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<xsl:choose>
<xsl:when test="expression">
... Code ...
</xsl:when>
<xsl:otherwise>
... Code ....
</xsl:otherwise>
</xsl:choose>
<xsl:choose> <xsl:when test="expression"> ... Code ... </xsl:when> <xsl:otherwise> ... Code .... </xsl:otherwise> </xsl:choose>

  
   ... Code ...
  
  
   ... Code ....
  

Where to use the Choose Condition:

In the XSL file, we can add the <xsl:choose>, <xsl:when>, and <xsl:otherwise> elements, to insert a multiple conditional test against the XML file.

Example:

Books.xml:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<!--?xml version="1.0" encoding="UTF-8"?-->
<bookstore>
<book category="Child">
<title lang="en">ABC</title>
<author>Author Name</author>
<year>2020</year>
<price>100.00</price>
</book>
<book category="IT">
<title lang="en">XQuery Book</title>
<author>Author 1</author>
<author>Author 2</author>
<year>2005</year>
<price>300.00</price>
</book>
<book category="Sociology">
<title lang="en">Sociology 1</title>
<author>Author Name</author>
<year>2010</year>
<price>250.00</price>
</book>
<book category="GK">
<title lang="en">Current Affairs</title>
<author>Author Name</author>
<year>2004</year>
<price>500.00</price>
</book>
<book category="Science">
<title lang="en">Science Book</title>
<author>Author 1</author>
<author>Author 2</author>
<author>Author 3</author>
<year>2011</year>
<price>150.00</price>
</book>
</bookstore>
<!--?xml version="1.0" encoding="UTF-8"?--> <bookstore> <book category="Child"> <title lang="en">ABC</title> <author>Author Name</author> <year>2020</year> <price>100.00</price> </book> <book category="IT"> <title lang="en">XQuery Book</title> <author>Author 1</author> <author>Author 2</author> <year>2005</year> <price>300.00</price> </book> <book category="Sociology"> <title lang="en">Sociology 1</title> <author>Author Name</author> <year>2010</year> <price>250.00</price> </book> <book category="GK"> <title lang="en">Current Affairs</title> <author>Author Name</author> <year>2004</year> <price>500.00</price> </book> <book category="Science"> <title lang="en">Science Book</title> <author>Author 1</author> <author>Author 2</author> <author>Author 3</author> <year>2011</year> <price>150.00</price> </book> </bookstore>



  
    ABC
    Author Name
    2020
    100.00
  

  
    XQuery Book
    Author 1
    Author 2
    2005
    300.00
  

  
    Sociology 1
    Author Name
    2010
    250.00
   

  
    Current Affairs
    Author Name
    2004
    500.00
  

  
    Science Book
    Author 1
    Author 2
    Author 3
    2011
    150.00
  


XSLT Code:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<!--?xml version="1.0" encoding="UTF-8"?-->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="">
<h2>List of Books</h2>
<xsl:for-each select="bookstore/book">
</xsl:for-each><xsl:choose>
<xsl:when test="year > 2005">
</xsl:when></xsl:choose><xsl:otherwise>
</xsl:otherwise><table border="1">
<tbody><tr bgcolor="#FF0000">
<th>Title</th>
<th>Price</th>
</tr>
<tr>
<td><xsl:value-of select="title"></xsl:value-of></td>
<td bgcolor="#DC143C">
<xsl:value-of select="price">
</xsl:value-of></td>
<td><xsl:value-of select="price"></xsl:value-of></td>
</tr>
</tbody></table>
</xsl:template>
</xsl:stylesheet>
<!--?xml version="1.0" encoding="UTF-8"?--> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match=""> <h2>List of Books</h2> <xsl:for-each select="bookstore/book"> </xsl:for-each><xsl:choose> <xsl:when test="year > 2005"> </xsl:when></xsl:choose><xsl:otherwise> </xsl:otherwise><table border="1"> <tbody><tr bgcolor="#FF0000"> <th>Title</th> <th>Price</th> </tr> <tr> <td><xsl:value-of select="title"></xsl:value-of></td> <td bgcolor="#DC143C"> <xsl:value-of select="price"> </xsl:value-of></td> <td><xsl:value-of select="price"></xsl:value-of></td> </tr> </tbody></table> </xsl:template> </xsl:stylesheet>





  

List of Books

Title Price

Output:

Explanation:

In the above example, we are adding a pink background-colour to the “Price” column if the year of the book has a value higher than 2005.

Example 2:

Books.xml:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<!--?xml version="1.0" encoding="UTF-8"?-->
<bookstore>
<book category="Child">
<title lang="en">ABC</title>
<author>Author Name</author>
<year>2020</year>
<price>100.00</price>
</book>
<book category="IT">
<title lang="en">XQuery Book</title>
<author>Author 1</author>
<author>Author 2</author>
<year>2005</year>
<price>300.00</price>
</book>
<book category="Sociology">
<title lang="en">Sociology 1</title>
<author>Author Name</author>
<year>2010</year>
<price>250.00</price>
</book>
<book category="GK">
<title lang="en">Current Affairs</title>
<author>Author Name</author>
<year>2004</year>
<price>500.00</price>
</book>
<book category="Science">
<title lang="en">Science Book</title>
<author>Author 1</author>
<author>Author 2</author>
<author>Author 3</author>
<year>2011</year>
<price>150.00</price>
</book>
</bookstore>
<!--?xml version="1.0" encoding="UTF-8"?--> <bookstore> <book category="Child"> <title lang="en">ABC</title> <author>Author Name</author> <year>2020</year> <price>100.00</price> </book> <book category="IT"> <title lang="en">XQuery Book</title> <author>Author 1</author> <author>Author 2</author> <year>2005</year> <price>300.00</price> </book> <book category="Sociology"> <title lang="en">Sociology 1</title> <author>Author Name</author> <year>2010</year> <price>250.00</price> </book> <book category="GK"> <title lang="en">Current Affairs</title> <author>Author Name</author> <year>2004</year> <price>500.00</price> </book> <book category="Science"> <title lang="en">Science Book</title> <author>Author 1</author> <author>Author 2</author> <author>Author 3</author> <year>2011</year> <price>150.00</price> </book> </bookstore>



  
    ABC
    Author Name
    2020
    100.00
  

  
    XQuery Book
    Author 1
    Author 2
    2005
    300.00
  

  
    Sociology 1
    Author Name
    2010
    250.00
  

  
    Current Affairs
    Author Name
    2004
    500.00
  

  
    Science Book
    Author 1
    Author 2
    Author 3
    2011
    150.00
  


XSLT Code:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<!--?xml version="1.0" encoding="UTF-8"?-->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="">
<h2>List of Books</h2>
<xsl:for-each select="bookstore/book">
</xsl:for-each><xsl:choose>
<xsl:when test="year > 2005">
</xsl:when></xsl:choose><xsl:when test="year > 2004">
</xsl:when><xsl:otherwise>
</xsl:otherwise><table border="1">
<tbody><tr bgcolor="#FF0000">
<th>Title</th>
<th>Artist</th>
</tr>
<tr>
<td><xsl:value-of select="title"></xsl:value-of></td>
<td bgcolor="#DC143C">
<xsl:value-of select="price">
</xsl:value-of></td>
<td bgcolor="#00FFFF">
<xsl:value-of select="price"></xsl:value-of></td>
<td><xsl:value-of select="price"></xsl:value-of></td>
</tr>
</tbody></table>
</xsl:template>
</xsl:stylesheet>
<!--?xml version="1.0" encoding="UTF-8"?--> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match=""> <h2>List of Books</h2> <xsl:for-each select="bookstore/book"> </xsl:for-each><xsl:choose> <xsl:when test="year > 2005"> </xsl:when></xsl:choose><xsl:when test="year > 2004"> </xsl:when><xsl:otherwise> </xsl:otherwise><table border="1"> <tbody><tr bgcolor="#FF0000"> <th>Title</th> <th>Artist</th> </tr> <tr> <td><xsl:value-of select="title"></xsl:value-of></td> <td bgcolor="#DC143C"> <xsl:value-of select="price"> </xsl:value-of></td> <td bgcolor="#00FFFF"> <xsl:value-of select="price"></xsl:value-of></td> <td><xsl:value-of select="price"></xsl:value-of></td> </tr> </tbody></table> </xsl:template> </xsl:stylesheet>





  

List of Books

Title Artist

Output:

Explanation:

In the above example, we have placed two <xsl:when> elements. Here, we are adding a pink background-colour to the “Price” column if the year of the book has a value higher than 2005, and if the year of the book has a value higher than 2004 and lower or equal to 2005, then we are adding a grey background-colour.