Click to return to the XML (Extensible Markup Language) home page    
xsl:for-each Element     xsl:otherwise Element     XSL Elements    
Web Workshop  |  XML (Extensible Markup Language)

xsl:if Element


Allows simple conditional template fragments.

Syntax

<xsl:if
    expr="script-expression"
    language="language-name"
    test="pattern"  >

Attributes

expr
Script expression evaluating to a Boolean. If this expression returns "TRUE", and the test pattern succeeds, the contents of xsl:if are placed in the output.
language
Active Scripting language used for the expression in the expr attribute. If left unspecified, the current scripting language is used, as specified by a language attribute on an ancestor. If no such attribute exists, JScript® (compatible with ECMA 262 language specification) is used. This attribute accepts the same values as the language attribute on the HTML SCRIPT element.
test
Condition in the source data to test. If the select pattern in this attribute identifies at least one node in the current source document, the contents of xsl:if are placed in the output.

Element Information

Number of occurrences Unlimited
Parent elements xsl:attribute, xsl:comment, xsl:copy, xsl:element, xsl:for-each, xsl:if, xsl:otherwise, xsl:pi, xsl:template, xsl:when, output elements
Child elements xsl:apply-templates, xsl:attribute, xsl:choose, xsl:comment, xsl:copy, xsl:element, xsl:eval, xsl:for-each, xsl:if, xsl:pi, xsl:value-of, output elements
Requires closing tag Yes. XSL is an XML grammar and, like all XML grammars, all tags must have closing tags to satisfy the definition of well-formed.

Remarks

The XSL processor also supports multi-part conditional testing, using the xsl:choose, xsl:when, and xsl:otherwise elements.

Examples

This example inserts the text "International Stock" when the "stock" element has an "international" attribute.

<xsl:template match="stock">
  <xsl:if test="@international">International Stock</xsl:if>
  <xsl:apply-templates />
</xsl:template>

This example inserts an HR before the first "item" element in a set, and another HR element after the last "item" element in the set. The "context()" method refers to the set of "item" elements selected by the xsl:apply-templates that caused this template to be invoked.

<xsl:template match="item">
  <xsl:if test="context()[0]"><HR/></xsl:if>
  <xsl:apply-templates />
  <xsl:if test="context()[end()]"><HR/></xsl:if>
</xsl:template>

See Also

Conditional Templates, Influencing Transformations Through Script



Back to topBack to top

Did you find this topic useful? Suggestions for other topics? Write us!

© 1999 Microsoft Corporation. All rights reserved. Terms of use.