"choose" - The If...Else Element

This section describes the 'choose' element, which is used in the content of a 'template' element. The 'choose' element is a conditional statement that be used to put different output contents under different logical conditions.

choose: A child element used as part of the content of a "template" element. A "choose" element serves as a conditional selection statement to do different things under different conditions. A "choose" element is a like "if...else" statement in many other computer languages. The syntax of the "choose" element is:

<xsl:choose>
 <xsl:when test="condition1">
  content
 </xsl:when>
 <xsl:when test="condition2">
  content
 </xsl:when>
 ...
 <xsl:otherwise>
  content
 </xsl:otherwise>
</xsl:choose>

where "condition1" and "condition2" are XPath expressions to return Boolean values.

If one of test conditions returns true, the content associated with that condition will be used. All other contents will be ignored.

Here are some examples of "if" elements:

<xsl:choose>
<!-- for expired elements, output a message -->
<xsl:when test="@status='expired'">
 This <xsl:value-of select="name(.)"/> record has expired!
</xsl:when>

<!-- for active elements, apply transformations -->
<xsl:when test="@status='active'">
 <xsl:apply-templates/>
</xsl:when>

<xsl:otherwise>
 Error: Unkown status!
</xsl:otherwise>
</xsl:choose>

Last update: 2014.

Table of Contents

 About This Book

 Introduction of XML (eXtensible Markup Language)

 XML File Syntax

 XML File Browsers

 DOM (Document Object Model) Programming Interface

 SAX (Simple API for XML) Programming Interface

 DTD (Document Type Definition) Introduction

 Syntaxes of DTD Statements

 Validating an XML Document against the Specified DTD Document Type

 XSD (XML Schema Definition) Introduction

 Syntaxes of XSD Statements

 Validating XML Documents Against Specified XML Schemas

 XSL (Extensible Stylesheet Language) Introduction

 XSLT (XSL Transformations) Introduction

 Java Implementation of XSLT

 XPath (XML Path) Language

XSLT Elements as Programming Statements

 "value-of" - Evaluating XPath String Expressions

 Examples of Using "value-of" Elements

 "{expression}" - Shorthand of "value-of" Elements

 "variable" - Declaring Variables

 "for-each" - Looping through a Node Set

 Examples of Using "for-each" Elements

 "if" - The Conditional Element

"choose" - The If...Else Element

 Control and Generate XML Element in the Result

 XML Notepad - XML Editor

 XML Tools Plugin for Notepad++

 XML 1.1 Changes and Parsing Examples

 Outdated Tutorials

 References

 PDF Printing Version