Preserving or Stripping White Space
XSLT 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 determine 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 whose white space must be preserved on output. The <xsl:preserve-space>
element 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...elemN" />
In this syntax, elem1 through elemN are the names (without the enclosing < and > delimiters) of all elements whose white space must be preserved.
You can specify all of the elements in the source document with the asterisk operator ("*"), as follows:
<xsl:preserve-space elements="*" />
Because all of the content of an XML document is, by default, preserved, <xsl:preserve-space>
is only useful when 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 How the MSXML Processor Parses White Space.
Removing White Space with <xsl:strip-space>
The <xsl:strip-space>
element provides a list of elements in the source document that have content which must be removed from the output tree. The <xsl:strip-space>
element is an empty, top-level element.
The following is the general syntax.
<xsl:strip-space elements="elem1 elem2...elemN" />
In this syntax, elem1 through elemN are the names (without the enclosing < and > delimiters) of all elements whose white space must be removed.
You can specify all of the elements in the source document with the asterisk operator ("*"), as follows:
<xsl:strip-space elements="*" />
If an element appears in both an <xsl:strip-space>
and <xsl:preserve-space>
list, the specification that appears last 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...elemN" />
If this order were reversed, the explicit preserve-space
settings for elem1, elem2, and so on would be overridden by the global strip-space
setting.