Re: [xsl] Newbie question, commenting out an element

Subject: Re: [xsl] Newbie question, commenting out an element
From: Deborah Pickett <debbiep-list-xsl@xxxxxxxxxx>
Date: Sun, 21 Dec 2008 18:05:42 +1100
Hi Douglas,

Douglas Wade wrote:
> <applic><assert></assert></
> applic>
> to
> <!-- <applic><assert></assert></applic> -->

The reason this isn't working for you is that XSLT doesn't produce
serialized XML (at least, not directly).  It produces a result tree
(think of something like a DOM tree), which is serialized to XML by
another agent*.  In other words, XSLT works on elements and nodes, not
tags and text.

It's not allowed for an XML result tree to have comment nodes (made with
<xsl:comment>) with child nodes other than text.  If you try something like

<xsl:comment><xsl:element name="foo"/></xsl:comment>

you will get an error reminding you of this.

That said, there are a couple of ways you can do this.  One is to not
use XSLT at all, and treat this as a text-substitution problem to be
done in a general text-processing language like Perl.  The other is to
use an XPath extension function such as saxon:serialize(), which can
turn an XML result tree into a string that represents the tree, which
you can then stick into your output with <xsl:value-of>.

Whichever way you go, you will need to ensure that the comment never
contains the sequence of characters "--", which is forbidden in XML
comments to prevent the possibility of comments nesting.

* That agent is usually your XSLT processor, after it has finished
processing the stylesheet, but XSLT processors don't have to do this.

Current Thread