How xsl:strip-space Controls Whitespace in XSLT
The xsl:strip-space element is a top-level XSLT
instruction used to remove whitespace-only text nodes from specified
elements in a source XML document before template processing begins. By
defining which elements should have their extraneous spaces, tabs, and
line breaks removed, developers can prevent unwanted whitespace in the
output, ensure accurate node counting, and simplify XPath
navigation.
Understanding Whitespace-Only Text Nodes
In XML, text between tags that contains only formatting
characters—such as carriage returns, line feeds, tabs, and standard
spaces—is parsed as a text node. By default, XSLT processors preserve
these whitespace-only nodes. This default behavior can introduce
unexpected empty lines in the output or alter the results of XPath
expressions like count(*) or position().
Syntax and Operation
The xsl:strip-space element is placed directly under the
root <xsl:stylesheet> or
<xsl:transform> element. Its primary attribute is
elements, which takes a whitespace-separated list of
element names or wildcards.
<xsl:strip-space elements="book author description" />To strip whitespace from all elements in the source document, use the wildcard selector:
<xsl:strip-space elements="*" />How the Stripping Process Works
During the transformation process, the XSLT processor applies whitespace handling in a specific sequence:
- Source Tree Construction: The XML document is parsed into an abstract node tree.
- Identification of Whitespace Nodes: The processor identifies text nodes that consist entirely of whitespace characters (ASCII space, tab, line feed, carriage return). Nodes containing any non-whitespace character are left untouched.
- Rule Matching: The processor checks the parent
element of the whitespace-only text node against the rules defined in
xsl:strip-spaceandxsl:preserve-space. - Node Removal: If the parent element matches an
xsl:strip-spacepattern, the text node is stripped from the source tree before templates are matched or applied.
Interaction
with xsl:preserve-space and xml:space
Whitespace stripping is subject to specific precedence rules:
- Conflict Resolution: If an element matches both an
xsl:strip-spaceand anxsl:preserve-spacedeclaration, standard XSLT import precedence and priority rules apply. If the conflicting declarations have the same import precedence, the one declared last in the stylesheet takes priority. - The
xml:spaceAttribute: If an element in the source XML document includes the attributexml:space="preserve", that element and all of its descendants retain their whitespace-only text nodes regardless of anyxsl:strip-spacedeclarations in the stylesheet, unless explicitly overridden down the hierarchy withxml:space="default".
Practical Benefits
Using xsl:strip-space streamlines XML transformations
by:
- Eliminating unintended line breaks and indentation carried over from pretty-printed source XML.
- Allowing accurate positional testing (such as
node()[1]orfollowing-sibling::node()[1]) without inadvertently selecting empty text nodes. - Reducing memory footprint and improving transformation performance on large XML datasets containing heavy indentation.