NickChase
NickChase

Reputation: 1512

XSLT comment tags don't output content?

I'm trying to carry over some XML from one document to another, but I want it to be commented out when it gets there. So I want to do something like this:

<xsl:template name="process">
    <figure>
        <xsl:comment>
            <xsl:copy />
        </xsl:comment>
    </figure>
</xsl:template>

When I run it using a straight Java XSLT processor, I get something along the lines of:

<figure>
<!--







  -->
  </figure>

The weird thing is that if I run it WITHHOUT the comment object, as in:

<xsl:template name="process">
    <figure>
            <xsl:copy />
    </figure>
</xsl:template>

The content comes through just fine.

Any ideas? I thought maybe you can't use a element in a comment but I looked it up and it's supposed to be fine.

Upvotes: 3

Views: 501

Answers (1)

Tomalak
Tomalak

Reputation: 338406

Right from the spec (Section 7.4, Creating Comments), emphasis mine:

It is an error if instantiating the content of xsl:comment creates nodes other than text nodes. An XSLT processor may signal the error; if it does not signal the error, it must recover by ignoring the offending nodes together with their content.

So wherever you've looked it up, your information is wrong. It is not supposed to work.

You would have to create text that looks like XML inside a comment. This is either difficult or easy, depending on the XSLT processor you are using.

Saxon for example has a serialize() extension function, which makes the task trivial:

<xsl:comment>
    <xsl:value-of select="saxon:serialize($node-set, 'default')" />
</xsl:comment>

This requires the saxon namespace declaration in your XSLT:

<xsl:stylesheet version="2.0" 
   xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
   xmlns:saxon="http://saxon.sf.net/"
>

Upvotes: 5

Related Questions