The xsl:strip-space and xsl:preserve-space Elements

The <xsl: strip-space > and <xsl: preserve-space > Elements

The <xsl:stripspace> element causes the XSLT processor to strip all pure whitespace nodes (also called expendable whitespace nodes) from the input document. A pure whitespace node consists of only whitespace characters , without any other type of text. This element has only one attribute:

  • elements (mandatory). Specifies the elements from which to strip the whitespace. Set to a whitespace-separated list of NameTests (which are names or generic names with wildcards).

This element contains no content.

For example, to strip all whitespace nodes from planets.xml, I could use <xsl:strip-space elements="*"/> like this:

 <?xml version="1.0"?>  <xsl:stylesheet version="1.0"  xmlns:xsl="">      <xsl:strip-space elements="*"/>      <xsl:output method="xml"/>      <xsl:template match="*">          <xsl:copy>              <xsl:apply-templates/>          </xsl:copy>      </xsl:template>  </xsl:stylesheet> 

Heres the result document when I apply this stylesheet to planets.xml. Note that all whitespace has been stripped out, including all newline characters:


Notice that only pure whitespace nodes are removed this way. For example, the text content of the element <TITLE>Volcanoes for Dinner</TITLE> does not include any pure whitespace text nodes, so the text, Volcanoes for Dinner, would be preserved in the output document, including the spaces. This would be true even if the text contained multiple adjacent spaces, as in Volcanoes for Dinner.

There may be times you might not want to remove all the whitespace nodes throughout a document, and you can use the <xsl:preserve-space> element to indicate in which elements you want to preserve whitespace nodes. This element has the same attribute as <xsl:stripspace> :

  • elements (mandatory). Specifies the elements in which to preserve the whitespace. Set to a whitespace-separated list of NameTests (which are names or generic names with wildcards).

In fact, <xsl:preservespace> is the default for all elements in XSLT. If youve used <xsl:strip-space> , you can still indicate in which element or elements you want whitespace nodes preserved by setting the elements attribute in <xsl:preserve-space> to a list of that element or elements, as follows :

 <?xml version="1.0"?>  <xsl:stylesheet version="1.0"  xmlns:xsl="">      <xsl:strip-space elements="*"/>      <xsl:preserve-space elements="MASS RADIUS"/>      <xsl:output method="xml"/>      <xsl:template match="*">          <xsl:copy>              <xsl:apply-templates/>          </xsl:copy>      </xsl:template>  </xsl:stylesheet> 

All this discussion of stripping and preserving whitespace may make you a little nervous when it comes to formatting output documents with indentation spaces, but there is an easy way: You can use the indent attribute of the <xsl:output> element to automatically indent the result document.

Inside XSLT
Inside Xslt
ISBN: B0031W8M4K
Year: 2005
Pages: 196

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: