The XSLT <xsl:choose> element is used to specify a multiple conditional test against the content of nodes with the <xsl:otherwise> and <xsl:when> elements.
<xsl:choose> <xsl:when test="expression"> ... some output ... </xsl:when> <xsl:otherwise> ... some output .... </xsl:otherwise> </xsl:choose>
test: It specifies a condition in xml data to test.
Let's take an example to create a table of
Employee.xml
<?xml version = "1.0"?> <?xml-stylesheet type = "text/xsl" href = "employee.xsl"?> <class> <employee id = "001"> <firstname>Aryan</firstname> <lastname>Gupta</lastname> <nickname>Raju</nickname> <salary>30000</salary> </employee> <employee id = "024"> <firstname>Sara</firstname> <lastname>Khan</lastname> <nickname>Zoya</nickname> <salary>25000</salary> </employee> <employee id = "056"> <firstname>Peter</firstname> <lastname>Symon</lastname> <nickname>John</nickname> <salary>10000</salary> </employee> </class>
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 = "/"> <html> <body> <h2>Employee</h2> <table border = "1"> <tr bgcolor = "pink"> <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 = "class/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 > 50000"> High </xsl:when> <xsl:when test = "salary > 40000"> Medium </xsl:when> <xsl:otherwise> Low </xsl:otherwise> </xsl:choose> </td> </tr> </xsl:for-each> </table> </body> </html> </xsl:template> </xsl:stylesheet>
Output: