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:

<xsl:choose>
  <xsl:when test="expression">
   ... Code ...
  </xsl:when>
  <xsl:otherwise>
   ... Code ....
  </xsl:otherwise>
</xsl:choose>

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:

<?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>

XSLT Code:

<?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>List of Books</h2>
  <table border="1">
    <tr bgcolor="#FF0000">
      <th>Title</th>
      <th>Price</th>
    </tr>
    <xsl:for-each select="bookstore/book">
    <tr>
      <td><xsl:value-of select="title"/></td>
      <xsl:choose>
      <xsl:when test="year > 2005">
         <td bgcolor="#DC143C">
         <xsl:value-of select="price"/>
         </td>
      </xsl:when>
      <xsl:otherwise>
         <td><xsl:value-of select="price"/></td>
      </xsl:otherwise>
      </xsl:choose>
    </tr>
    </xsl:for-each>
  </table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

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:

<?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>

XSLT Code:

<?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>List of Books</h2>
  <table border="1">
    <tr bgcolor="#FF0000">
      <th>Title</th>
      <th>Artist</th>
    </tr>
    <xsl:for-each select="bookstore/book">
    <tr>
      <td><xsl:value-of select="title"/></td>
      <xsl:choose>
      <xsl:when test="year > 2005">
         <td bgcolor="#DC143C">
         <xsl:value-of select="price"/>
         </td>
      </xsl:when>
      <xsl:when test="year > 2004">
         <td bgcolor="#00FFFF">
         <xsl:value-of select="price"/></td>
      </xsl:when>
      <xsl:otherwise>
         <td><xsl:value-of select="price"/></td>
      </xsl:otherwise>
      </xsl:choose>
    </tr>
    </xsl:for-each>
  </table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

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.

Please Share