<xsl:comment> | |
Allows you to create a comment in the output document. Comments are sometimes used to add legal notices, disclaimers, or information about when the output document was created. Another useful application of the <xsl:comment> element is the generation of CSS definitions or JavaScript code in an HTML document. | |
Category | |
Instruction |
|
Required Attributes | |
None. |
|
Optional Attributes | |
None. |
|
Content | |
An XSLT template. |
|
Appears in | |
<xsl:comment> appears in a template. |
|
Defined in | |
XSLT section 7.4, Creating Comments. |
|
Example | |
Here's a stylesheet that generates a comment to define CSS styles in an HTML document: <?xml version="1.0"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="html"/> <xsl:template match="/"> <html> <head> <title>XSLT and CSS Demo</title> <style> <xsl:comment> p.big {font-size: 125%; font-weight: bold} p.green {color: green; font-weight: bold} p.red {color: red; font-style: italic} </xsl:comment> </style> </head> <body> <xsl:apply-templates select="list/title"/> <xsl:apply-templates select="list/listitem"/> </body> </html> </xsl:template> <xsl:template match="title"> <p class="big"><xsl:value-of select="."/></p> </xsl:template> <xsl:template match="listitem"> <xsl:choose> <xsl:when test="position() mod 2"> <p class="green"><xsl:value-of select="."/></p> </xsl:when> <xsl:otherwise> <p class="red"><xsl:value-of select="."/></p> </xsl:otherwise> </xsl:choose> </xsl:template> </xsl:stylesheet> This stylesheet creates three CSS styles inside an HTML comment. We'll apply the stylesheet to this document: <?xml version="1.0"?> <list xml:lang="en"> <title>Albums I've bought recently:</title> <listitem>The Sacred Art of Dub</listitem> <listitem>Only the Poor Man Feel It</listitem> <listitem>Excitable Boy</listitem> <listitem xml:lang="sw">Aki Special</listitem> <listitem xml:lang="en-gb">Combat Rock</listitem> <listitem xml:lang="zu">Talking Timbuktu</listitem> <listitem xml:lang="jz">The Birth of the Cool</listitem> </list> The stylesheet will apply one CSS style to the <title> element and will alternate between two CSS styles for the <listitem>s. Here's the generated HTML: <html> <head> <META http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>XSLT and CSS Demo</title> <style> <!-- p.big {font-size: 125%; font-weight: bold} p.green {color: green; font-weight: bold} p.red {color: red; font-style: italic} --> </style> </head> <body> <p class="big">Albums I've bought recently:</p> <p class="green">The Sacred Art of Dub</p> <p class="red">Only the Poor Man Feel It</p> <p class="green">Excitable Boy</p> <p class="red">Aki Special</p> <p class="green">Combat Rock</p> <p class="red">Talking Timbuktu</p> <p class="green">The Birth of the Cool</p> </body> </html> When rendered, the document looks like Figure A-6. Document with generated comment nodes |