Partager via


Preserving and Stripping White Space

A version of this page is also available for

Windows Embedded CE 6.0 R3

4/8/2010

XSL Transformations (XSLTs) can distinguish nodes that contain white space intermingled with other characters. The white space is considered inseparable from the other text in the node.

For nodes that contain nothing but white space, the <xsl:preserve-space> and <xsl:strip-space> elements handle how the nodes are output.

Preserving White Space with <xsl:preserve-space>

The <xsl:preserve-space> element provides a list of those elements in the source document where white space must be preserved on output. <xsl:preserve-space> is always an empty, top-level element, for example, a child of the <xsl:stylesheet> element in an XSLT style sheet.

The following is the general syntax.

<xsl:preserve-space elements="elem1 elem2..." />

elem1 and elem2 are the names of all elements, without the enclosing < and > delimiters, whose white space must be preserved.

You can specify all of the elements in the source document with the asterisk operator (*).

<xsl:preserve-space elements="*" />

Because all of the content of an XML document is, by default, preserved, <xsl:preserve-space> is useful only in cases in which you have used <xsl:strip-space> to override the default behavior; in such cases, use <xsl:preserve-space> to identify exceptions to the explicit stripping of white space.

Note

The Microsoft XSLT processor will process all of a document's white space only if the preserveWhiteSpace property has been set to TRUE prior to loading the document into the DOM. For more information, see Handling White Space.

Removing White Space with <xsl:strip-space>

The <xsl:strip-space> element provides a list of those elements in the source document in which content must be removed from the output tree. <xsl:strip-space> is an empty, top-level element.

The following is the general syntax.

<xsl:strip-space elements="elem1 elem2..." />

elem1 and elem2 are the names of all elements (without the enclosing < and > delimiters) whose white space must be removed.

You can specify all of the elements in the source document with the asterisk operator (*).

<xsl:strip-space elements="*" />

If an element appears in both an <xsl:strip-space> and <xsl:preserve-space> list, the last specification applies. Therefore, a typical sequence of these two elements in an XSLT style sheet is as follows.

<xsl:strip-space elements="*" />
<xsl:preserve-space elements="elem1 elem2..." />

If this order were reversed, the explicit "preserve" settings for elem1, elem2, and so on would be overridden by the global "strip" setting.

See Also

Concepts

Controlling White Space with XSLT