3

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.

0

1 Answer 1

5

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/"
>
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks @Dimitre. The same to you!
That definitely solves the mystery. Thanks so much, Tomalak!
@NickChase Beware that XML comments cannot be nested. If your input XML contains comment nodes and you pass it through serialize(), putting the result into another comment, you might end up with illegal XML.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.