Example of <xsl:preserve-space> and <xsl:strip-space>

MSXML 5.0 SDK

Microsoft XML Core Services (MSXML) 5.0 for Microsoft Office - XSLT Developer's Guide

Example of <xsl:preserve-space> and <xsl:strip-space>

You can use the <xsl:preserve-space> and <xsl:strip-space> elements to preserve and strip white space from XML documents.

Example

The strippreserve.xml document below contains <code> and <block> elements that have white space in the form of tabs, spaces, and newline characters.

The strippreserve.xsl style sheet below does the following:

  • Preserves the extraneous white space in the <code> elements.
  • Strips the extraneous white space from the <block> elements.
  • Encloses the content of each <code> and <block> element in square brackets ([ ]).
  • Uses the XSLT position() function to provide a numbered label for each <code> or <block>.
  • Uses the XPath translate() function to converts invisible white space to a visible character, one character at a time. This enables you to see the specific white characters in the <code> and <block> elements.

The results are as follows:

  • Each space character appears in the result tree as a hyphen (-).
  • Each newline character appears in the result tree as the letter N.
  • Each carriage return appears in the result tree as the letter R.
  • Each tab appears in the result tree as the letter T.
  • The first <code> element contains a single space, denoted by the hyphen in the [ ] characters. The first <block> element also contains a single space in the source document. However, the <xsl:strip-space> setting has caused this space to be removed from the result tree.
  • In the second <code> and <block> elements, no white space has been removed. If an element has any content that is not white space, white space will not be stripped, even if <xsl:strip-space> is set for that element.
Note   This example uses only XSLT to strip and preserve white space. You can also strip or preserve white space by manipulating the Document Object Model (DOM). For more information about this approach, see White Space and the DOM.

XML File (strippreserve.xml)

Copy this text to a file. Then, find each tabhere and replace it with a tab character (that is, press the TAB key).

<?xml-stylesheet type="text/xsl" href="strippreserve.xsl" ?>
<document>
<block> </block>
<block>
tabhere
Some texttabhere
tabhere
tabhere</block>
<code> </code>
<code>
tabhere
Some texttabhere
tabhere
tabhere</code>
</document>

XSLT File (strippreserve.xsl)

<?xml version='1.0'?>
<xsl:stylesheet version="1.0"
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<!-- Retain white space within all <code> elements -->
<xsl:preserve-space elements="code"/>
<!-- ... but strip it from all <block> elements -->
<xsl:strip-space elements="block"/>

<xsl:template match="/">
<html>
<head><title>Test: Stripping/Preserving White Space</title></head>
<body>
<h4>Code blocks:</h4>
<!-- <pre> element forces all output characters to be same width -->
<pre>
<!-- Use translate() XPath function to convert white-space
           characters to "visible" form. -->
<xsl:for-each select="//code">
"Code" #<xsl:value-of select="position()"/>: [<xsl:value-of select="translate(.,' &#10;&#13;&#9;','-NRT')"/>]<br/>
</xsl:for-each>
</pre>
<h4>Normalized blocks:</h4>
<pre>
<xsl:for-each select="//block">
"Block" #<xsl:value-of select="position()"/>: [<xsl:value-of select="translate(.,' &#10;&#13;&#9;','-NRT')"/>]<br/>
</xsl:for-each>
</pre>
</body>
</html>
</xsl:template>

</xsl:stylesheet>

Formatted Output

Code blocks:
"Code" #1: [-]
"Code" #2: [NTNSome-textTNTNT]
Normalized blocks:
"Block" #1: []
"Block" #2: [NTNSome-texttabhereNTNT]

Processor Output

<html>
<head>
<META http-equiv="Content-Type" content="text/html; charset=UTF-16">
<title>Test: Stripping/Preserving White Space</title></head>
<body>
<h4>Code blocks:</h4>
<pre>
"Code" #1: [-]<br>
"Code" #2: [NTNSome-textTNTNT]<br></pre>
<h4>Normalized blocks:</h4>
<pre>
"Block" #1: []<br>
"Block" #2: [NTNSome-textTNTNT]<br></pre>
</body>
</html>