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

XML
1. CSS Style
2. SVG
3. XML Schema
4. XQuery
5. XSLT stylesheet
Java
XML Tutorial
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
XML » XSLT stylesheet » Choose 
xsl:choose, xsl:when and xsl:otherwise

File: Data.xml

<?xml version = "1.0" encoding = "UTF-8"?>
<planner>
  <year value="2002">
    <date month="7" day="15">
      <note time="1430">Appointment</note>
      <note time="1620">Physics class</note>
    </date>
    <date month="7" day="4">
      <note>Independence Day</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>
        <p>
          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" />
    <xsl:for-each select="date/note">
      <xsl:sort select="../@day" order="ascending"
        data-type="number" />
      <strong>
        Day:
        <xsl:value-of select="../@month" />
        /
        <xsl:value-of select="../@day" />
      </strong>
      <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; '1700' 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><p>
          Appointments
          <br/><strong>Year:</strong>2002<strong>
        Day:
        7
        /
        4</strong>Entire day:Independence Day<br/><strong>
        Day:
        7
        /
        15</strong>

          Afternoon (
          1430
          ):
        Appointment<br/><strong>
        Day:
        7
        /
        15</strong>

          Afternoon (
          1620
          ):
        Physics class<br/></p></body></html>

 
Related examples in the same category
1. Choose when statement
2. when test="position()=last()"
www.java2java.com | Contact Us
Copyright 2010 - 2030 Java Source and Support. All rights reserved.
All other trademarks are property of their respective owners.