Re: [xsl] XSLT: Are XML Comments parseable?

Subject: Re: [xsl] XSLT: Are XML Comments parseable?
From: andrew welch <andrew.j.welch@xxxxxxxxx>
Date: Tue, 20 Sep 2005 14:03:01 +0100
It's because of a thing called the 'default template' which kicks in
when you haven't specified a template to match the current node.

When the XML is transformed, the root matching template is the first
to be applied.  If you haven't provided one, the default one is used,
which looks like this:

<xsl:template match="/">
   <xsl:apply-templates/>
</xsl:template>

The <xsl:apply-templates/> instruction applies templates to the
comment and to the <fruit> element.  You have a template that matches
the comment, but not one for the <fruit> element, so the default
template is used again.  The default template for a text node copies
it to the output, which is why you see 'apple'.

The two choices here are to provide a root matching template and only
apply-templates to the comment, eg:

<xsl:template match="/">
   <xsl:apply-templates select="comment()/>
</xsl:template>

This way the <fruit> element never gets processed and so it wont
appear in your output.

The other way is to provide a template for <fruit> that doesn't ouput
anything:

<xsl:template match="fruit"/>

This means this template will be used instead of the default template,
and as it does nothing you won't get anything in the output.

The first way only selects the things you want to process.  The second
way processes everything but uses 'no-op' templates to suppress
unwanted output.

More info can be found at Dave Pawson's FAQ:
http://www.dpawson.co.uk/xsl/sect2/defaultrule.html

cheers
andrew

Current Thread