"if" - The Conditional Element

This section describes the 'if' element, which is used in the content of a 'template' element. The 'if' element is a conditional statement that be used to put a block of output content under a logical condition.

if: A child element used as part of the content of a "template element. A "if" element serves as a conditional statement to do things conditionally. The syntax of the "if" element is:

<xsl:if test="condition">
 content
</xsl:if>

where the "condition" is an XPath expression to return Boolean value, and "content" is a mixture literal data and XSLT elements to be used only if the condition is true.

If the specified condition returns true, the specified content will be used. If the specified condition returns false, the specified content will be ignored.

Here are some examples of "if" elements:

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

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

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