xsl:choose, xsl:when and xsl:otherwise : choose « XSLT stylesheet « XML Tutorial






File: Data.xml

<?xml version = "1.0" encoding = "UTF-8"?>
<planner>

  <year value="2002">

    <date month="7" day="15">
      <note time="1430">meeting</note>
      <note time="1620">course</note>
    </date>

    <date month="7" day="4">
      <note>Independence Day</note>
    </date>

    <date month="7" day="9">
      <note />
    </date>
  </year>

</planner>

File: Transform.xslt

<?xml version = "1.0" encoding = "UTF-8"?>
<xsl:stylesheet version = "1.0" 
   xmlns:xsl = "http://www.w3.org/1999/XSL/Transform"
      xmlns = "http://www.w3.org/1999/xhtml">
   <xsl:output method = "xml" omit-xml-declaration = "no" 
       doctype-system = 
          "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"
       doctype-public = 
          "-//W3C//DTD XHTML 1.0 Strict//EN" />

   <xsl:template match = "/">
      <html>
         <head><title>Conditional Processing</title></head>
         <body>
            <paragraph>Appointments
               <br />
               <xsl:apply-templates select = "planner/year" />
            </p>
         </body>
      </html>
   </xsl:template>
   
   <xsl:template match = "year">
      <strong>Year:</strong>

      <xsl:value-of select = "@value" />
      
      <br />
      
      <xsl:for-each select = "date/note">

         <!-- sort by date's day attribute value -->
         <xsl:sort select = "../@day" order = "ascending"
            data-type = "number" />
         
         <br />

         <strong>
            Day: 
            <xsl:value-of select = "../@month"/>/
            <xsl:value-of select = "../@day"/>
         </strong>
         
         <br />

         <xsl:choose>

            <xsl:when test = 
               "@time &gt; '0500' and @time &lt; '1200'">

               Morning (<xsl:value-of select = "@time" />):
            </xsl:when>

            <xsl:when test =
               "@time &gt; '1200' and @time &lt; '1700'">
               
               Afternoon (<xsl:value-of select = "@time" />):
            </xsl:when>

            <xsl:when test = 
               "@time &gt; '1200' and @time &lt; '2200'">
               
               Evening (<xsl:value-of select = "@time" />):
            </xsl:when>

            <xsl:when test = 
               "@time &gt; '2200' and @time &lt; '500'">
               
               Night (<xsl:value-of select = "@time" />):
            </xsl:when>

            <xsl:otherwise>
               Entire day:
            </xsl:otherwise>

         </xsl:choose>

         <xsl:value-of select = "." />

         <xsl:if test = ". = ''">
            n/a
         </xsl:if>

         <br />
      </xsl:for-each>

   </xsl:template>

</xsl:stylesheet>

Output:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html
  PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><head><title>Conditional Processing</title></head><body><paragraph>Appointments
               <br/><strong>Year:</strong>2002<br/><br/><strong>
            Day: 
            7/
            4</strong><br/>
               Entire day:
            Independence Day<br/><br/><strong>
            Day: 
            7/
            9</strong><br/>
               Entire day:
            
            n/a
         <br/><br/><strong>
            Day: 
            7/
            15</strong><br/>
               
               Afternoon (1430):
            Doctor's appointment<br/><br/><strong>
            Day: 
            7/
            15</strong><br/>
               
               Afternoon (1620):
            course<br/></p></body></html>








5.43.choose
5.43.1.choose statement
5.43.2.xsl:choose, xsl:when and xsl:otherwise
5.43.3.xsl:choose: check the value of an attribute
5.43.4.choose with otherwise statement
5.43.5.when test="position() mod 4 = 0"
5.43.6.Change style for even and odd
5.43.7.xsl:choose element is used for selection between several possibilities