Adding Comments

 
xslt for dummies
Chapter 13 - Gimme Some Space and Other Output Issues
XSLT For Dummies
by Richard Wagner
Hungry Minds 2002
  

XSLT includes the xsl:comment instruction to create an XML/HTML-like comment and add it to the result document. After transformation, the comment text you provide becomes surrounded by a <!-- and --> . For example, suppose Id like to output the afifilms.xml document (refer to Listing 13-1) as an HTML-based numbered list. However, Id like to add a comment at the top of the document identifying the list along with comments both before and after the list denoting its start and end. The following stylesheet can be used to do this:

 <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:output method="html"/> <!-- Add HTML element, comments --> <xsl:template match="/topfilms"> <html> <xsl:comment>List created by <xsl:value-of select="@createdby"/></xsl:comment> <xsl:comment>***** Start List *****</xsl:comment> <ol> <xsl:apply-templates/> </ol> <xsl:comment>***** End List *****</xsl:comment> </html> </xsl:template> <!-- Apply to each film --> <xsl:template match="film"> <li><i><xsl:apply-templates/></i></li> </xsl:template> </xsl:stylesheet> 

The first template rule demonstrates the use of the xsl:comment . Any text placed between the start and end tags of the xsl:comment instruction are added as comments in the result tree. As you can see, you can include literal text or anything that evaluates to text, such as xsl:value-of . The second template rule adds the HTML code to create an italicized list (the li element creates a list item and i adds italics). The HTML code that is generated after the transformation is shown here:

 <html> <!--List created by AFI--> <!--***** Start List *****--> <ol>  <li><i>Citizen Kane</i></li> <li><i>Casablanca</i></li> <li><i>The Godfather</i></li> <li><i>Gone With The Wind</i></li> <li><i>Lawrence Of Arabia</i></li> <li><i>The Wizard Of Oz</i></li> <li><i>The Graduate</i></li> <li><i>On The Waterfront</i></li> <li><i>Schindler's List</i></li> <li><i>Singin' In The Rain</i></li> </ol> <!--***** End List *****--> </html> 

 Warning   Avoid putting -- in your comment text or ending your text with a - . These characters are used to denote an end of comment tag and so could potentially goof up the resulting XML document structure. Some XSLT processors handle this syntax problem by adding a space around the last dash to prevent an error, but other processors may generate a runtime error.

You may have noticed that the XML comments from the original source document werent copied to the result document. If you read the discussion on built-in templates in Chapter 4, you may recall that XSLT automatically strips out comments. If you want to carry over comments from the source to the result document, you need to add the following template rule to the stylesheet:

 <xsl:template match="comment()"> <xsl:copy/> </xsl:template> 

The <!-- American Film Institute Top 25 Films --> comment is then included in the output.

  
 
 
2000-2002    Feedback


XSLT For Dummies
XSLT for Dummies
ISBN: 0764536516
EAN: 2147483647
Year: 2002
Pages: 148

Similar book on Amazon

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