views:

35

answers:

1

I have a weird problem. Using XSLT transformations with PHP and for some reason, the compiled template file that is printed to the user strips all comments from the code. This never occurred before and have been unable to debug this problem at all. Even at the source $xslt->transformToXML($xml), it is stripped comments now, when it wasn't before.

This is particularly annoying with JS blocks that are wrapped in <!-- -->.

Any ideas?

+2  A: 

As far as I know, unless you tell it otherwise, an XSLT transform will strip comments and processing instructions.

If you want to keep comments you can add something like

<xsl:template match="comment()">
  <xsl:comment><xsl:value-of select="."/></xsl:comment>
</xsl:template>

to your xslt file.

Peter Tillemans
@Peter, your "AFAIK" is right. "The built-in template rule for processing instructions and comments is to do nothing." http://www.w3.org/TR/xslt#built-in-rule +1 for supplying the needed template.
LarsH
@Peter even simpler would be <xsl:template match="comment()"/><xsl:copy/></xsl:template>
Nick Jones
@Nick when I do xhtml to xhtml transformation I usually add a copy everything template in the beginning and override what I want. But it is dangerous and surprising for unsuspected users.
Peter Tillemans