Handling Whitespace

Handling Whitespace

Whitespace gives XSLT authors a lot of trouble at first. Chapter 2 explained that pure whitespace nodes are text nodes that contain only whitespace (spaces, carriage returns, line feeds, and tabs). These nodes are copied by default when they come from the source document.

Note that you can also have whitespace nodes in your stylesheets as well:

 <xsl:template match="PLANETS">      <xsl:copy>          <xsl:apply-templates select="PLANET"/>      </xsl:copy>  </xsl:template> 

In this case, Im using spaces to indent the stylesheet elements, as well as carriage returns to spread things out. Pure whitespace nodes such as these are not copied from the stylesheet to the output document. Note, however, that the whitespace in the following <TITLE> element is copied to the output, because its not a pure whitespace node (it also contains the text The Planets Table):

 <xsl:template match="/PLANETS">      <HTML>          <HEAD> 
 <TITLE>                  The Planets Table              </TITLE>              .              .              . 

If you want to eliminate this whitespace and retain the indented format, you can use empty <xsl:text> elements so the whitespace becomes pure whitespace nodes:

 <xsl:template match="/PLANETS">      <HTML>          <HEAD>              <TITLE>                  <xsl:text/>The Planets Table<xsl:text/>              </TITLE>              .              .              . 

Pure whitespace nodes are not copied from the stylesheet to the output document unless they are inside an <xsl:text> element, or an enclosing element has the xml:space attribute set to preserve (for more on xml:space , see Inside XML ).

On the other hand, by default, XSLT preserves whitespace text nodes in the source document and copies them to the result document. Use the copying stylesheet weve already seen, which copies all elements from the source document to the result document:

 <?xml version="1.0"?>  <xsl:stylesheet version="1.0"  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">      <xsl:output method="xml"/>      <xsl:template match="*">          <xsl:copy>              <xsl:apply-templates/>          </xsl:copy>      </xsl:template>  </xsl:stylesheet> 

and apply this stylesheet to planets.xml, all the whitespace Ive used in planets.xml is copied over to the result document as well:

 <?xml version="1.0"?>  <?xml-stylesheet type="text/xml" href="planets.xsl"?>  <PLANETS>      <PLANET>          <NAME>Mercury</NAME>          <MASS UNITS="(Earth = 1)">.0553</MASS>          <DAY UNITS="days">58.65</DAY>          <RADIUS UNITS="miles">1516</RADIUS>          <DENSITY UNITS="(Earth = 1)">.983</DENSITY>          <DISTANCE UNITS="million miles">43.4</DISTANCE><!--At perihelion-->      </PLANET>          .          .          . 

However, there are times you want to remove the whitespace used to format input documents, and you can do that with the <xsl:stripspace> element.

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

Similar book on Amazon

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