1
0
mirror of https://github.com/alexandrev/xslt-lab.git synced 2026-09-18 19:43:16 +00:00
Files
xslt-lab/site/content/xslt/functions/xsl-if.md
T
alexandrev-tibco 53e90ef86e feat(blog): complete XSLT/XPath reference — 229 function pages
Full coverage of XSLT 1.0, 2.0 and 3.0 elements and XPath functions:
- 59 XSLT elements (xsl:stylesheet → xsl:use-accumulators)
- 170 XPath functions (1.0 node/string/numeric/boolean, 2.0 sequence/
  date/QName/string, 3.0 HOF/map/array/JSON/streaming)
Each page: description, parameters table, return value, 2 runnable
Saxon examples with input XML + stylesheet + output, notes, cross-links.
xsltCompletions.js: all 229 entries now have blogSlug for hover links.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-19 09:28:05 +02:00

3.7 KiB

title, description, date, version, versionLabel, category, syntax, tags
title description date version versionLabel category syntax tags
xsl:if Conditionally outputs content when a boolean XPath expression evaluates to true; has no else branch. 2026-04-18T00:00:00Z 1.0 XSLT 1.0 element <xsl:if test="boolean-expression">
xslt
reference
xslt1

Description

xsl:if evaluates the XPath expression in its test attribute and instantiates its content only when the result is true (or converts to boolean true). When the expression is false, the element and all its children are silently skipped — no output is produced.

Because xsl:if has no else branch, use xsl:choose with xsl:when and xsl:otherwise when you need to handle two or more mutually exclusive cases. For a simple true/false toggle, xsl:if is more concise and readable.

The test expression is evaluated in the current context and can reference context-node content, positional functions like position() and last(), variables, and any XPath 1.0 expression. Non-boolean results are coerced to boolean according to XPath rules: a non-empty node-set is true, a non-zero number is true, and a non-empty string is true.

Attributes

Attribute Type Required Description
test Boolean XPath expression Yes Expression that controls whether the content is output.

Examples

Conditional row class

Input XML:

<?xml version="1.0" encoding="UTF-8"?>
<items>
  <item id="1" stock="5">Widget</item>
  <item id="2" stock="0">Gadget</item>
  <item id="3" stock="12">Doohickey</item>
</items>

Stylesheet:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes"/>

  <xsl:template match="/items">
    <table>
      <xsl:apply-templates select="item"/>
    </table>
  </xsl:template>

  <xsl:template match="item">
    <row>
      <xsl:if test="@stock = 0">
        <xsl:attribute name="class">out-of-stock</xsl:attribute>
      </xsl:if>
      <name><xsl:value-of select="."/></name>
      <stock><xsl:value-of select="@stock"/></stock>
    </row>
  </xsl:template>
</xsl:stylesheet>

Output:

<table>
  <row><name>Widget</name><stock>5</stock></row>
  <row class="out-of-stock"><name>Gadget</name><stock>0</stock></row>
  <row><name>Doohickey</name><stock>12</stock></row>
</table>

Alternate row separator

Input XML:

<?xml version="1.0" encoding="UTF-8"?>
<colors>
  <color>Red</color>
  <color>Green</color>
  <color>Blue</color>
</colors>

Stylesheet:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="text"/>

  <xsl:template match="/colors">
    <xsl:for-each select="color">
      <xsl:if test="position() > 1">
        <xsl:text>, </xsl:text>
      </xsl:if>
      <xsl:value-of select="."/>
    </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>

Output:

Red, Green, Blue

Notes

  • xsl:if cannot appear at the top level of a stylesheet; it is only valid inside a template body.
  • An empty node-set converts to boolean false, so <xsl:if test="element-name"> is a safe existence check.
  • Comparing a missing attribute to a value (e.g., @attr = 'x') returns false rather than raising an error, because the attribute node-set is empty.
  • For multiple exclusive branches, use xsl:choose instead of chaining several xsl:if elements — the latter evaluates all conditions independently, which is rarely the desired behaviour.

See also