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.6 KiB
title, description, date, version, versionLabel, category, syntax, tags
| title | description | date | version | versionLabel | category | syntax | tags | |||
|---|---|---|---|---|---|---|---|---|---|---|
| xsl:preserve-space | Explicitly preserves whitespace-only text nodes in the named source elements, counteracting xsl:strip-space rules. | 2026-04-18T00:00:00Z | 1.0 | XSLT 1.0 | element | <xsl:preserve-space elements="names"/> |
|
Description
xsl:preserve-space is a top-level declaration that instructs the XSLT processor to keep whitespace-only text nodes in the specified source elements when building the source tree. By default, without any whitespace directives, all whitespace-only text nodes in the source document are preserved.
Its primary use is as an exception to xsl:strip-space. When a stylesheet has a broad xsl:strip-space elements="*" rule that removes all whitespace-only text nodes, xsl:preserve-space can carve out specific elements where whitespace is significant and must not be touched.
The elements attribute takes a whitespace-separated list of element names or the wildcard *. Element names may be namespace-qualified. Import precedence applies: if xsl:strip-space and xsl:preserve-space both match the same element at the same import precedence, a conflict error is raised.
Attributes
| Attribute | Type | Required | Description |
|---|---|---|---|
elements |
whitespace-separated NameTests | Yes | Elements in which whitespace-only text nodes should be preserved. Use * for all elements. |
Examples
Preserving whitespace in code blocks
Input XML:
<?xml version="1.0" encoding="UTF-8"?>
<doc>
<para> This paragraph has leading spaces. </para>
<pre>
line one
line two
</pre>
</doc>
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="no"/>
<xsl:strip-space elements="*"/>
<xsl:preserve-space elements="pre"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Output:
<doc><para>This paragraph has leading spaces.</para><pre>
line one
line two
</pre></doc>
Selective preservation in a mixed document
Input XML:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<text> spaced </text>
<code> preserved </code>
</root>
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:strip-space elements="root text"/>
<xsl:preserve-space elements="code"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Output:
<root>
<text> spaced </text>
<code> preserved </code>
</root>
Notes
xsl:preserve-spaceonly affects whitespace-only text nodes in source elements. Text nodes that contain non-whitespace characters are never stripped regardless of any directive.- The
xml:space="preserve"attribute in the source document also prevents stripping, regardless ofxsl:strip-spacedirectives. - These directives affect the source tree built from the primary input document and from documents loaded with the
document()function. - Whitespace stripping happens before pattern matching; it does not remove whitespace-only text nodes from the result tree.