<xsl:preserve-space> Element 

Preserves white space only text nodes that may appear in those elements as specified by the elements attribute.

<xsl:preserve-space
  elements = tokens />

Attributes

  • elements
    This is a required attribute. The value is a white space separated list of name tokens of the nodes whose white space only text nodes must be preserved.

Element Information

Number of occurrences

Unlimited

Parent elements

xsl:stylesheet, xsl:transform

Child elements

(No child elements)

Remarks

The <xsl:preserve-space> element preserves white-space-only text nodes in the specified elements. It has no effect on the white space characters in text nodes with both white space and non-white-space characters. Here preservation of white-space-only text nodes means that the nodes from the source document will be kept in the result document. The <xsl:strip-space> does the opposite; it strips the white-space-only text nodes in the specified nodes.

By default, all white-space-only text nodes are preserved. If an element name matches a name test in an <xsl:strip-space> element, it is removed from the set of white-space-preserving element names. If an element name matches a name test in an <xsl:preserve-space> element, it is added back to the set of white-space-preserving element names.

For more information, see "Whitespace Stripping", Section 3.4 of the XSLT W3 Recommendation at www.w3.org/TR/xslt.

Example

The following example illustrates the effects of preserving and stripping white-space-only text nodes using <xsl:preserve-space> and <xsl:strip-space>.

XML File (source.xml)

<?xml version="1.0"?>
<document>
<text>   </text>
<text>  ;</text>
<text>
This is a   sample text 
    
</text>
<code>   </code>
<code>  ;</code>
<code>
This is a   sample code 
    
</code>
</document>

XSLT File (trans.xsl)

<?xml version='1.0'?>
<xsl:stylesheet version="1.0"
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="text"/>
  <!-- 
     The following xsl:preserve-space is not necessary.
     It is included to emphasize the fact that white-space-only
     text nodes are to be preserved on the <code> elements.
   -->
  <xsl:preserve-space elements="code"/>
  <xsl:strip-space elements="text"/>

  <xsl:template match="/">
    code elements:
    <xsl:apply-templates select="//code"/>

    text elements:
    <xsl:apply-templates select="//text"/>
  </xsl:template>

  <xsl:template match="text">
     text # <xsl:value-of select="position()"/>
     has <xsl:value-of select="count(text())"/> text(). 
     "<xsl:value-of select="translate(.,' &#10;&#13;&#9;', '-NRT')"/>"
  </xsl:template>
  

  <xsl:template match="code">
     code # <xsl:value-of select="position()"/>
     has <xsl:value-of select="count(text())"/> text(). 
     "<xsl:value-of select="translate(.,' &#10;&#13;&#9;', '-NRT')"/>"
  </xsl:template>

</xsl:stylesheet>

Try It!

  1. Copy the code above and save it in appropriate files on your local drive.

  2. Launch the XSLT transformation, using the Command Line Transformation Utility (msxsl.exe) from a command prompt, as follows:

    msxsl source.xml trans.xsl

    Important

    Do not start the transformation from Internet Explorer. The browser performs some space-stripping operations that are not compatible with the XSLT specifications. This can cause the XLST transformation to appear ill-behaved.

Output

This is the standard output:

code elements:

code # 1

has 1 text().

"---"

code # 2

has 1 text().

"--;"

code # 3

has 1 text().

"NThis-is-a-Tsample-codeTNTN"

text elements:

text # 1

has 0 text().

""

text # 2

has 1 text().

"--;"

text # 3

has 1 text().

"NThis-is-a-Tsample-textTNTN"

Notice that the transformation yields one text node for the first <code> element, but no text node for the first <text> element. This is because these elements both have a white-space-only text node that is preserved in <code> but stripped in <text>, according to the <xsl:preserve-space> and <xsl:strip-space> instructions listed at the beginning of the style sheet. The second and third elements of each kind show that the text nodes that are not white-space-only are not affected by those instructions.

See Also

Reference

<xsl:strip-space> Element