Open In App

XSLT <choose> Element

The XSLT <choose> Element is used to define the choice among the values of the node. It is the same as the Switch keyword of the CPP language.

Syntax:



<xsl:choose>
<xsl:when test="comparator"></xsl:when>
<xsl:otherwise></xsl:otherwise> [It is case when test failes]
</xsl:choose>

 

Attributes:



Example 1: In this example, we will use the “choose” element and color the node with age less than 18 by red and greater by blue color. Save Both files as mentioned and open xsl file on the Browser, it will show the output as shown below:




<!--Test.xml-->
  
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl "href="Rule.xsl" ?>
<student>
   <s>
      <name> Divyank Singh Sikarwar </name>
      <branch>CSE</branch>
      <age>18</age>
      <city> Agra </city>
   </s>
   <s>
      <name> Aniket Chauhan </name>
      <branch> CSE</branch>
      <age> 20</age>
      <city> Shahjahanpur </city>
   </s>
   <s>
      <name> Simran Agarwal</name>
      <branch> CSE</branch>
      <age> 23</age>
      <city> Buland Shar</city>
   </s>
   <s>
      <name> Abhay Chauhan</name>
      <branch> CSE</branch>
      <age> 17</age>
      <city> Shahjahanpur</city>
   </s>
   <s>
      <name> Himanshu Bhatia</name>
      <branch> IT</branch>
      <age>25</age>
      <city> Indore</city>
   </s>
</student>




<!--Rule.xsl-->
  
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
   <xsl:template match="/">
      <html>
         <body>
            <h1 align="center">Students' Basic Details</h1>
            <table border="3" align="center" >
               <tr>
                  <th>Name</th>
                  <th>Branch</th>
                  <th>Age</th>
               </tr>
               <xsl:for-each select="student/s">
                  <tr>
                     <xsl:choose>
                        <xsl:when test="age>18">
                           <td>
                              <span style="color:#0000ff">
                                 <xsl:value-of select="name"/>
                              </span>
                           </td>
                           <td>
                              <span style="color:#0000ff">
                                 <xsl:value-of select="branch"/>
                              </span>
                           </td>
                           <td>
                              <span style="color:#0000ff">
                                 <xsl:value-of select="age"/>
                              </span>
                           </td>
                        </xsl:when>
                        <xsl:otherwise>
                           <td>
                              <span style="color:#ff0000">
                                 <xsl:value-of select="name"/>
                              </span>
                           </td>
                           <td>
                              <span style="color:#ff0000">
                                 <xsl:value-of select="branch"/>
                              </span>
                           </td>
                           <td>
                              <span style="color:#ff0000">
                                 <xsl:value-of select="age"/>
                              </span>
                           </td>
                        </xsl:otherwise>
                     </xsl:choose>
                  </tr>
               </xsl:for-each>
            </table>
         </body>
      </html>
   </xsl:template>
</xsl:stylesheet>

Output:

choose

Example 2: In this example, we will choose templates for those who have branches specified in their details or not. Save Both files as mentioned and open xsl file on the Browser, it will show the output as shown below:




<!--Test.xml-->
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl "href="Rule.xsl" ?>
<student>
   <s>
      <name> Divyank Singh Sikarwar </name>
      <branch></branch>
      <age>18</age>
      <city> Agra </city>
   </s>
   <s>
      <name> Aniket Chauhan </name>
      <branch> CSE</branch>
      <age> 20</age>
      <city> Shahjahanpur </city>
   </s>
   <s>
      <name> Simran Agarwal</name>
      <branch> CSE</branch>
      <age> 23</age>
      <city> Buland Shar</city>
   </s>
   <s>
      <name> Abhay Chauhan</name>
      <branch></branch>
      <age> 17</age>
      <city> Shahjahanpur</city>
   </s>
   <s>
      <name> Himanshu Bhatia</name>
      <branch> IT</branch>
      <age>25</age>
      <city> Indore</city>
   </s>
</student>




<!--Rule.xsl-->
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
   <xsl:template match="/">
      <html>
         <body>
            <h1 align="center">Students' Basic Details</h1>
            <table border="3" align="center" >
               <tr>
                  <th>Details</th>
               </tr>
               <xsl:for-each select="student/s">
                  <tr>
                     <xsl:choose>
                        <xsl:when test="branch=''">
                           <td>
                              <xsl:apply-templates select="name"/>
                              <xsl:apply-templates select="age"/>
                           </td>
                        </xsl:when>
                        <xsl:otherwise>
                           <td>
                              <xsl:apply-templates select="name"/>
                              <xsl:apply-templates select="branch"/>
                              <xsl:apply-templates select="age"/>
                           </td>
                        </xsl:otherwise>
                     </xsl:choose>
                  </tr>
               </xsl:for-each>
            </table>
         </body>
      </html>
   </xsl:template>
   <xsl:template match="name">
      Name:
      <span style="font-family:cursive;color:#ff0000">
         <xsl:value-of select="."/>
      </span>
      <br />
   </xsl:template>
   <xsl:template match="branch">
      Branch:
      <span style="font-family:serif;color:#0ff000">
         <xsl:value-of select="."/>
      </span>
      <br />
   </xsl:template>
   <xsl:template match="age">
      Age: 
      <span style="font-family:fantsy;color:#0000ff">
         <xsl:value-of select="."/>
      </span>
      <br />
   </xsl:template>
</xsl:stylesheet>

Output:

choose


Article Tags :