How to comment in XSLT and not HTML

Solution 1:

You use standard XML comments:

<!-- Comment -->

These are not processed by the XSLT transformer.

Solution 2:

Just make sure that you put your <!-- comments --> AFTER the opening XML declaration (if you use one, which you really don't need):

BREAKS:

<!-- a comment -->
<?xml version="1.0"?>

WORKS:

<?xml version="1.0"?>
<!-- a comment -->

I scratched my head on this same issue for a bit while debugging someone else's XSLT... seems obvious, but easily overlooked.

Solution 3:

Note that white space on either side of the comments can end up in the output stream, depending on your XSLT processor and its settings for handling white-space. If this is an issue for your output, make sure the comment is bracketed by xslt tags.

EG

<xsl:for-each select="someTag">
  <xsl:text>"</xsl:text>
    <!-- output the id -->
<xsl:value-of select="@id"/>
<xsl:text>"</xsl:text>
</xsl:for-each>

Will output " someTagID" (the indent tab/spaces in front of the comment tag are output). To remove, either unindent it flush with left margin, or bracket it like

<xsl:text>"</xsl:text><!-- output the id --><xsl:value-of select="@id"/>