Automatic Indenting

Automatic Indenting

The <xsl:output> element supports an attribute called indent , which you can set to yes or no, and which indicates to the XSLT processor whether you want the result document indented. Usually, indenting the result document doesnt matter very much, because that document is targeted to an application that doesnt care about indenting, as in the XML-to-XML and -HTML examples youve seen. However, there are times when youd like to view the result document as straight text, and in such cases, indenting that document to show its hierarchical structure can help.

How an XSLT processor uses the indent variable varies by processor, because its not specified by W3C, so you have to experiment to get the results you want. Say, for example, that you have a version of planets.xml without any indentation at all, like the following:

 <?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>  <PLANET>  <NAME>Venus</NAME>  <MASS UNITS="(Earth = 1)">.815</MASS>  <DAY UNITS="days">116.75</DAY>  <RADIUS UNITS="miles">3716</RADIUS>  <DENSITY UNITS="(Earth = 1)">.943</DENSITY>  <DISTANCE UNITS="million miles">66.8</DISTANCE><!--At perihelion-->  </PLANET>  <PLANET>  <NAME>Earth</NAME>  <MASS UNITS="(Earth = 1)">1</MASS>  <DAY UNITS="days">1</DAY>  <RADIUS UNITS="miles">2107</RADIUS>  <DENSITY UNITS="(Earth = 1)">1</DENSITY>  <DISTANCE UNITS="million miles">128.4</DISTANCE><!--At perihelion-->  </PLANET>  </PLANETS> 

You can use the <xsl:output indent="yes"/> element to instruct the XSLT processor to indent this document when you convert it to HTML:

Listing 3.6 Indenting Stylesheet
 <?xml version="1.0"?>  <xsl:stylesheet version="1.0"  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">      <xsl:output indent="yes"/>      <xsl:template match="/PLANETS">  <HTML>  <HEAD>  <TITLE>            The Planets Table        </TITLE>  </HEAD>  <BODY>  <H1>            The Planets Table        </H1>  <TABLE BORDER="2">  <TD>Name</TD>  <TD>Mass</TD>  <TD>Radius</TD>  <TD>Day</TD>  <xsl:apply-templates/>  </TABLE>  </BODY>  </HTML>  </xsl:template>  <xsl:template match="PLANET">  <TR>  <TD><xsl:value-of select="NAME"/></TD>  <TD><xsl:value-of select="MASS"/></TD>  <TD><xsl:value-of select="RADIUS"/></TD>  <TD><xsl:value-of select="DAY"/></TD>  </TR>  </xsl:template>  </xsl:stylesheet> 

Heres the result, using Saxon (which is particularly good at indenting), indented as desired:

 <HTML>     <HEAD>        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">        <TITLE>           The Planets Table         </TITLE>      </HEAD>      <BODY>         <H1>            The Planets Table         </H1>         <TABLE BORDER="2">            <TD>Name</TD>            <TD>Mass</TD>            <TD>Radius</TD>            <TD>Day</TD>           <TR>              <TD>Mercury</TD>              <TD>.0553</TD>              <TD>1516</TD>              <TD>58.65</TD>           </TR>           <TR>              <TD>Venus</TD>              <TD>.815</TD>              <TD>3716</TD>              <TvD>116.75</TD>           </TR>           <TR>              <TD>Earth</TD>              <TD>1</TD>              <TD>2107</TD>              <TD>1</TD>           </TR>        </TABLE>     </BODY>  </HTML> 

As you can see, handling whitespace takes a little bit of thought in XSLT, but its easier if you know whats going on.

Indenting Documents in this Book

How your XSLT processor indents documents is processor-dependent. When displaying documents in this book, Ill indent them for readability, even if the actual document has not been indented by the XSLT processor.



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