DigitalMC DigitalMC - 2 months ago 14
HTML Question

Maintain HTML comments in XSLT

Basically I have HTML comments in my XSLT that I want to preserve in the output but XSLT keeps dropping them. They are totally removed. Any way to keep these comments?

Below is a code example:

<xsl:template match="/">

<!-- Preserve This HTML Comment In Output -->
<div><xsl:value-of select="xmlnode" /></div>

</xsl:template>


I've tried wrapping in
<xsl:text>
and
<xsl:comment>
tags but no help.

Answer

You can't preserve comment but you can generate them in output with:

  <xsl:comment>This is a comment!</xsl:comment> 

Therefor try

<xsl:template match="/">
   <!-- Preserve This HTML Comment In Output -->
  <xsl:comment>Preserve This HTML Comment In Output</xsl:comment> 
  <div><xsl:value-of select="xmlnode" /></div>
</xsl:template>