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="http://www.w3.org/1999/XSL/Transform">      <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:

 <?xml version="1.0" encoding="utf-8"?><PLANETS><PLANET><NAME>Mercury</NAME>  <MASS>.0553  </MASS><DAY>58.65</DAY><RADIUS>1516</RADIUS><DENSITY>.983</DENSITY>  <DISTANCE>43.4</DISTANCE></PLANET><PLANET><NAME>Venus</NAME><MASS>.815</MASS>  <DAY>116.75</DAY><RADIUS>3716</RADIUS><DENSITY>.943</DENSITY><DISTANCE>66.8</DISTANCE>  </PLANET><PLANET><NAME>Earth</NAME><MASS>1</MASS><DAY>1</DAY><RADIUS>2107</RADIUS>  <DENSITY>1</DENSITY><DISTANCE>128.4</DISTANCE></PLANET></PLANETS> 

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="http://www.w3.org/1999/XSL/Transform">      <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
EAN: N/A
Year: 2005
Pages: 196

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net