Sponsored

XSLT Reference

xsl:preserve-space

XSLT 1.0 element

Explicitly preserves whitespace-only text nodes in the named source elements, counteracting xsl:strip-space rules.

Syntax
<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

AttributeTypeRequiredDescription
elementswhitespace-separated NameTestsYesElements 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-space only 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 of xsl:strip-space directives.
  • 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.

See also