XSLT `xsl:choose` Element: Implementing Conditional Logic in XML Transformations

Master conditional processing in XSLT using the powerful `xsl:choose` element. This tutorial demonstrates how to create flexible and dynamic transformations by evaluating multiple conditions (`xsl:when`) and providing alternative processing paths (`xsl:otherwise`), enabling sophisticated data manipulation based on XML content.



Using the XSLT `xsl:choose` Element for Conditional Processing

Understanding `xsl:choose`

The XSLT `xsl:choose` element allows you to perform conditional processing based on the content of XML nodes. It's like an `if-then-else` statement but specifically designed for working with XML data. It evaluates a series of `xsl:when` elements; if a `xsl:when`'s `test` attribute evaluates to true, its content is processed; otherwise, the process continues. If none of the `xsl:when` conditions are true, the content of the `xsl:otherwise` element (if present) is processed.

`xsl:choose` Parameters

The `xsl:choose` element doesn't have parameters in the same sense as a function. Instead, it uses nested elements to define conditional logic:

  • xsl:when test="condition": Executes its content if the `condition` (an XPath expression) is true.
  • xsl:otherwise (optional): Executes its content if none of the `xsl:when` conditions are true.

Example: Assigning Grades Based on Salary

This example demonstrates using `xsl:choose` to assign letter grades to employees based on their salaries. The XML input includes employee information, and the XSLT stylesheet uses `xsl:choose` to determine each employee's grade.

1. Sample XML Data (`Employee.xml`)

Employee.xml

<Employees>
  <Employee id="1">
    <FirstName>Aryan</FirstName>
    <LastName>Gupta</LastName>
    <NickName>Raju</NickName>
    <Salary>30000</Salary>
  </Employee>
  <Employee id="2">
    <FirstName>Sara</FirstName>
    <LastName>Khan</LastName>
    <NickName>Zoya</NickName>
    <Salary>25000</Salary>
  </Employee>
  <Employee id="3">
    <FirstName>Peter</FirstName>
    <LastName>Symon</LastName>
    <NickName>John</NickName>
    <Salary>10000</Salary>
  </Employee>
</Employees>

2. XSLT Stylesheet (`Employee.xsl`)

Employee.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="/">
    <table border="1">
      <tr>
        <th>ID</th>
        <th>First Name</th>
        <th>Last Name</th>
        <th>Nick Name</th>
        <th>Salary</th>
        <th>Grade</th>
      </tr>
      <xsl:for-each select="//Employee">
        <tr>
          <td><xsl:value-of select="@id"/></td>
          <td><xsl:value-of select="FirstName"/></td>
          <td><xsl:value-of select="LastName"/></td>
          <td><xsl:value-of select="NickName"/></td>
          <td><xsl:value-of select="Salary"/></td>
          <td>
            <xsl:choose>
              <xsl:when test="Salary >= 25000">High</xsl:when>
              <xsl:when test="Salary >= 15000">Medium</xsl:when>
              <xsl:otherwise>Low</xsl:otherwise>
            </xsl:choose>
          </td>
        </tr>
      </xsl:for-each>
    </table>
  </xsl:template>
</xsl:stylesheet>

Conclusion

The `xsl:choose` element is a very important tool for implementing conditional logic in XSLT transformations. It enables the creation of flexible and dynamic transformations, adapting the output based on the input data.