How Does xsl:text Control Whitespace in XSLT?
In Extensible Stylesheet Language Transformations (XSLT), the
<xsl:text> element serves as a precise mechanism for
managing output whitespace, preserving literal character data, and
preventing unwanted text formatting side effects. Because XSLT
processors automatically strip white-space-only text nodes from
stylesheets during parsing, developers frequently face formatting
challenges where spaces, tabs, and line breaks are discarded or
unintentionally introduced. Using <xsl:text> provides
explicit control over text node emission, ensuring predictable character
rendering across XML, HTML, and plain-text serialization.
Default Whitespace Handling in XSLT
XSLT processors operate according to strict whitespace preservation and stripping rules outlined in the W3C specifications. When an XSLT processor reads a stylesheet, it differentiates between elements in the XSLT namespace and literal result elements.
During the initial stylesheet compilation:
- Any text node containing exclusively whitespace characters (spaces, carriage returns, line feeds, and tabs) located within an XSLT element is stripped out by default.
- Whitespace adjacent to literal XML tags within templates is frequently collapsed or eliminated during output tree construction.
- Formatting applied to enhance stylesheet readability, such as indents and new lines, can inadvertently bleed into output text if plain character blocks are used outside of structured tags.
Without a targeted directive, generating exact whitespace—such as a single space separating two dynamic values or specific line breaks in plain-text output formats like CSV or Markdown—becomes unreliable across different processors.
The Core Functionality of
<xsl:text>
The <xsl:text> element instructs the XSLT
processor to treat enclosed content as a literal text node that must be
written directly to the result tree.
Key behaviors of <xsl:text> include:
1. Protection Against Whitespace Stripping
Unlike general XSLT container elements, an
<xsl:text> element that contains only whitespace
characters will not have its content stripped during stylesheet parsing.
Enclosing spaces, line feeds, or tabs inside
<xsl:text> guarantees that those exact characters are
emitted into the result document.
2. Isolation from Stylesheet Indentation
Developers can format and indent their XSLT stylesheets for visual
clarity without risking that the indentation logic corrupts the target
output. Wrapping desired text strings or spaces within
<xsl:text> isolates the output content from the
formatting whitespace of the stylesheet itself.
3. Disabling Output Escaping
The <xsl:text> element supports the optional
attribute disable-output-escaping="yes". When enabled, this
attribute allows the emission of characters like < and
& without converting them into standard XML entities
(< and &), facilitating raw
text injection when required for specific non-XML outputs.
Common Practical Use Cases
Adding Controlled Spacing Between Dynamically Generated Elements
When outputting sibling values such as a first name and a last name,
placing a space between two <xsl:value-of> elements
directly in the stylesheet can result in the space being discarded by
the parser.
<xsl:value-of select="FirstName"/>
<xsl:text> </xsl:text>
<xsl:value-of select="LastName"/>The enclosed space inside
<xsl:text> </xsl:text> is preserved as a
literal character node, guaranteeing consistent separation between the
output tokens.
Enforcing Line Breaks in Plain Text Formats
When generating delimited data or plain text formats, standard template line breaks may be parsed ambiguously. Developers can define explicit newlines using entity-encoded text elements:
<xsl:value-of select="Record/Field"/>
<xsl:text> </xsl:text>This ensures an exact line feed (ASCII code 10) is added after each record, independent of operating system formatting or engine-specific indentation rules.
Preventing Unwanted Concatenation or Gaps
In mixed-content scenarios where inline text meets markup (such as
producing inline punctuation or formatted HTML spans),
<xsl:text> eliminates accidental spaces introduced by
editor auto-formatting tools.
<a href="{@url}">
<xsl:value-of select="@title"/>
</a>
<xsl:text>.</xsl:text>Placing the period inside <xsl:text> attaches it
directly to the link text without intervening whitespace or line wrap
artifacts.
Summary of Best Practices
To maintain predictable transformations:
- Use
<xsl:text> </xsl:text>whenever single-character spaces are mandatory between dynamic expressions. - Use numeric character references (such as
for line feeds or	for tabs) inside<xsl:text>to make whitespace intent explicit and immune to automated code formatters. - Avoid relying on stylesheet whitespace formatting for output structure, as parsers optimize and discard unstructured whitespace nodes during execution.