RE: Re: [xsl] apply-templates and predicates

Subject: RE: Re: [xsl] apply-templates and predicates
From: cknell@xxxxxxxxxx
Date: Thu, 28 Apr 2005 09:45:10 -0400
I like this idea and I'm thinking of incorporating it as a "best practice", but when trying it out it didn't do something I expected it to do. Specifically, with this stylesheet:

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="";>
  <xsl:output method="xml" indent="yes" encoding="UTF-8" />

  <xsl:template match="*">
    <xsl:message>unexpected element encountered: <xsl:value-of select="name()" /></xsl:message>

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


The only message output concerned the root element, and none of its descendants. I was expecting to see a message for each element in the document, but got only one. I tried changing the XPath expression from "*" to "//*", but to no effect.

Could you elaborate on this concept?
Charles Knell
cknell@xxxxxxxxxx - email

-----Original Message-----
From:     David Carlisle <davidc@xxxxxxxxx>
Sent:     Thu, 28 Apr 2005 10:45:05 +0100
To:       xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject:  Re: [xsl] apply-templates and predicates

  It also increases the chances that the stylesheet will do something unwanted 
  when the schema changes

Not necessarily.

As I've mentioned earlier in this thread I almost always start off with
a template
<xsl:template match="*">
  <xsl:message>unexpected element ...</xsl:message>

when I think I have finished and added sufficient templates that 
generates no more warning messages, then if the input changes on me and
new elements are added the stylesheet doesn't just silently accept the
input, it starts warning again. This mechanism is flexible in that
depending on the context it can be modified  to make the warnings fatal
errors, or no warnings at all, depending on circumstances.

If on the other hand instead of <xsl:apply-templates/> I just have
<xsl:apply-templates select="A|b[@foo]|C/>
because that's the only type of element that I expect to see, and I only
have templates for those elements, then if the schema changes and the
input has new elements, they will be silently ignored.

Similarly if instead of <xsl:apply-templates/> I just have
<xsl:apply-templates select="*[not(self::F)]/>
Because I believed that the only element type that I didn't need to
handle was F, and I didn't want to write an empty template for F, then
if the schema changes and  a new element type appears this will be
handled by the default template (whatever that does) without warning.

It's not a clear cut choice: most stylesheets use both forms to some
extent, but I think that it is definitely the case that in the face of
under specified or changing input, keeping select attributes simple and
putting more logic into the match templates is usually preferable.


This e-mail has been scanned for all viruses by Star. The
service is powered by MessageLabs. For more information on a proactive
anti-virus service working around the clock, around the globe, visit:

Current Thread