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-strip-space.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

4.2 KiB

title, description, date, version, versionLabel, category, syntax, tags
title description date version versionLabel category syntax tags
xsl:strip-space Removes whitespace-only text nodes from the specified source elements before the transformation begins processing them. 2026-04-18T00:00:00Z 1.0 XSLT 1.0 element <xsl:strip-space elements="names"/>
xslt
reference
xslt1

Description

xsl:strip-space instructs the XSLT processor to discard text nodes that consist entirely of whitespace characters from the specified source elements. This affects the source tree built before any templates run; stripped nodes are never seen by the transformation.

The most common form is <xsl:strip-space elements="*"/>, which removes all whitespace-only text nodes from every element in the source document. This is useful when the source XML was indented for human readability and the whitespace carries no meaning — the strip rule prevents these invisible text nodes from appearing in position() counts, triggering built-in text templates, or otherwise interfering with the transformation.

The elements attribute accepts a whitespace-separated list of element name tests or the wildcard *. xsl:preserve-space can override xsl:strip-space for specific elements where whitespace is significant.

Attributes

Attribute Type Required Description
elements whitespace-separated NameTests Yes Elements from which whitespace-only text nodes are removed. Use * for all.

Examples

Stripping indentation whitespace

Input XML:

<?xml version="1.0" encoding="UTF-8"?>
<menu>
  <section>
    <item>Soup</item>
    <item>Salad</item>
  </section>
</menu>

Stylesheet without strip-space (produces extra whitespace text nodes):

<?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="/menu">
    <xsl:for-each select="section/item">
      <xsl:value-of select="position()"/>
      <xsl:text>: </xsl:text>
      <xsl:value-of select="."/>
      <xsl:text>&#10;</xsl:text>
    </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>

Stylesheet with strip-space (clean numbering):

<?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:strip-space elements="*"/>

  <xsl:template match="/menu">
    <xsl:for-each select="section/item">
      <xsl:value-of select="position()"/>
      <xsl:text>: </xsl:text>
      <xsl:value-of select="."/>
      <xsl:text>&#10;</xsl:text>
    </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>

Output:

1: Soup
2: Salad

Selective stripping

Input XML:

<?xml version="1.0" encoding="UTF-8"?>
<report>
  <summary>  Total: 5  </summary>
  <items>
    <item>A</item>
    <item>B</item>
  </items>
</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:strip-space elements="report items"/>

  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>

Output:

<report>
  <summary>  Total: 5  </summary>
  <items>
    <item>A</item>
    <item>B</item>
  </items>
</report>

Notes

  • Stripping only removes text nodes that are entirely whitespace. Mixed-content text nodes containing non-whitespace characters are never affected.
  • The xml:space="preserve" attribute in the source document prevents stripping for that element and all its descendants, even if xsl:strip-space targets them.
  • Whitespace stripping is applied after the XML parser builds the source tree, before any template processing starts. It cannot be applied conditionally during transformation.
  • When xsl:strip-space elements="*" and xsl:preserve-space elements="pre" are both present, xsl:preserve-space wins for pre elements because it is more specific.

See also