Generating Comments with xsl:comment

Generating Comments with xsl:comment

You can also create comments on the fly with the <xsl:comment> element. Here's an example. In this case, I'm creating comments that will replace <PLANET> elements. I'll include the name of the planet in the text of the comment:

Listing ch13_20.xsl
 <?xml version="1.0"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="PLANETS">     <HTML>         <HEAD>             <TITLE>                 Planets             </TITLE>         </HEAD>         <BODY>             <xsl:apply-templates select="PLANET"/>         </BODY>     </HTML> </xsl:template> <xsl:template match="PLANET">  <xsl:comment>This was the <xsl:value-of select="NAME"/> element</xsl:comment>  </xsl:template> </xsl:stylesheet> 

Here's the result:

 <HTML>  <HEAD> <TITLE>                 Planets             </TITLE> </HEAD> <BODY> <!--This was the Mercury element--> <!--This was the Venus element--> <!--This was the Earth element--> </BODY> </HTML> 

Generating Text with xsl:text

You can create text nodes with the <xsl:text> element, allowing you to do things such as replace whole elements with text on the fly. One reason you can use <xsl:text> is to preserve whitespace, as in this example from earlier in the chapter, where I used <xsl:text> to insert spaces:

Listing ch13_21.xsl
 <?xml version="1.0"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">     <xsl:template match="/PLANETS">         <HTML>             <HEAD>                 <TITLE>                     The Planets Table                 </TITLE>             </HEAD>             <BODY>                 <H1>                     The Planets Table                 </H1>                 <TABLE BORDER="1">                     <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:apply-templates select="MASS"/></TD>         <TD><xsl:apply-templates select="RADIUS"/></TD>       </TR>    </xsl:template>     <xsl:template match="MASS">       <xsl:value-of select="."/>  <xsl:text> </xsl:text>  <xsl:value-of select="@UNITS"/>     </xsl:template>     <xsl:template match="RADIUS">       <xsl:value-of select="."/>  <xsl:text> </xsl:text>  <xsl:value-of select="@UNITS"/>     </xsl:template>     <xsl:template match="DAY">       <xsl:value-of select="."/>  <xsl:text> </xsl:text>  <xsl:value-of select="@UNITS"/>     </xsl:template> </xsl:stylesheet> 

Another situation to use <xsl:text> is when you want characters such as < and & , not &lt; and &amp; , to appear in your output document. To do that, you set the <xsl:text> element's disable-output-escaping attribute to "yes" :

Listing ch13_22.xsl
 <?xml version="1.0"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="PLANETS">     <HTML>         <HEAD>             <TITLE>                 Planets             </TITLE>         </HEAD>         <BODY>             <xsl:apply-templates select="PLANET"/>         </BODY>     </HTML> </xsl:template> <xsl:template match="PLANET">  <xsl:text disable-output-escaping = "yes">   &lt;PLANET&gt;   </xsl:text>  </xsl:template> </xsl:stylesheet> 

Here is the result:

 <HTML>  <HEAD> <TITLE>                 Planets             </TITLE> </HEAD> <BODY>       <PLANET>       <PLANET>       <PLANET>   </BODY> </HTML> 


Real World XML
Real World XML (2nd Edition)
ISBN: 0735712867
EAN: 2147483647
Year: 2005
Pages: 440
Authors: Steve Holzner

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