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>
3.5 KiB
title, description, date, version, versionLabel, category, syntax, tags
| title | description | date | version | versionLabel | category | syntax | tags | ||||
|---|---|---|---|---|---|---|---|---|---|---|---|
| true() | Returns the boolean value true. Used in XPath expressions and xsl:if tests where an unconditional true value is needed. | 2026-04-18T00:00:00Z | 1.0 | XSLT 1.0 | boolean function | true() |
|
Description
true() returns the boolean literal true. It takes no arguments and exists because XPath 1.0 has no boolean literal syntax — unlike languages such as Java or Python, you cannot write the bare word true in an XPath expression and expect it to be interpreted as a boolean.
The most common use is comparing attribute values that represent boolean flags (e.g. @enabled = 'true'), or setting a default condition in xsl:when using <xsl:when test="true()"> as a fallback branch that is always matched.
true() is also useful when passing a boolean parameter to a template or evaluating a condition in a variable: <xsl:variable name="always" select="true()"/>.
Parameters
This function takes no parameters.
Return value
xs:boolean — always returns true.
Examples
Use true() as a catch-all xsl:when branch
Input XML:
<?xml version="1.0" encoding="UTF-8"?>
<items>
<item type="A">Alpha</item>
<item type="B">Beta</item>
<item type="C">Gamma</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">
<result>
<xsl:for-each select="item">
<xsl:choose>
<xsl:when test="@type = 'A'">
<primary><xsl:value-of select="."/></primary>
</xsl:when>
<xsl:when test="@type = 'B'">
<secondary><xsl:value-of select="."/></secondary>
</xsl:when>
<xsl:when test="true()">
<other><xsl:value-of select="."/></other>
</xsl:when>
</xsl:choose>
</xsl:for-each>
</result>
</xsl:template>
</xsl:stylesheet>
Output:
<result>
<primary>Alpha</primary>
<secondary>Beta</secondary>
<other>Gamma</other>
</result>
Assign a boolean variable
Input XML:
<?xml version="1.0" encoding="UTF-8"?>
<report>
<title>Sales Q1</title>
</report>
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:variable name="includeHeader" select="true()"/>
<xsl:template match="/report">
<output>
<xsl:if test="$includeHeader">
<header><xsl:value-of select="title"/></header>
</xsl:if>
<body>Report body here.</body>
</output>
</xsl:template>
</xsl:stylesheet>
Output:
<output>
<header>Sales Q1</header>
<body>Report body here.</body>
</output>
Notes
- XPath 1.0 has no bare boolean literals. You must use
true()andfalse()as function calls. - Using
<xsl:when test="true()">is a valid (though verbose) alternative to<xsl:otherwise>. Prefer<xsl:otherwise>for readability in most cases. - Comparing a string attribute like
@flag = 'true'performs string comparison, not a boolean conversion. If you need the attribute value interpreted as boolean, useboolean(@flag = 'true')explicitly. - In XSLT 2.0+ you can use
xs:boolean('true')for schema-aware processing, buttrue()remains valid.