[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

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


Subject: RE: [xsl] XSLT: Are XML Comments parseable?
From: "Khorasani, Houman" <houman_khorasani@xxxxxxxxxxxxxx>
Date: Tue, 20 Sep 2005 16:20:52 +0100

Hello Andrew,

Thank you for your response and also everyone else for your responses.

I do understand what guys are trying to say, yet it doesn't work.

XML file:

<?xml version="1.0" encoding="UTF-8"?>
<!--pear-->
<fruit>apple</fruit>



XSL:

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
	<xsl:output method="html" encoding="ISO-8859-1"/>
	<xsl:template name="GetComment" match="/">
		<xsl:apply-templates select="comment()"/>
		<c>
			<xsl:value-of select="."/>
		</c>
	</xsl:template>
</xsl:stylesheet>


Output:
<c>apple</c>


It should be though:
<c>pear</c>

What am I missing?

Many thanks,
Houman


-----Original Message-----
From: andrew welch [mailto:andrew.j.welch@xxxxxxxxx]
Sent: 20 September 2005 14:03
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject: Re: [xsl] XSLT: Are XML Comments parseable?

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
Keywords
xml