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

Subject: RE: Re: [xsl] apply-templates and predicates
From: "Michael Kay" <mike@xxxxxxxxxxxx>
Date: Thu, 28 Apr 2005 14:49:31 +0100
If you want to process the descendants of an unknown element, then do so:

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

Michael Kay
http://www.saxonica.com/
 

> -----Original Message-----
> From: cknell@xxxxxxxxxx [mailto:cknell@xxxxxxxxxx] 
> Sent: 28 April 2005 14:45
> To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> Subject: RE: Re: [xsl] apply-templates and predicates
> 
> 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="http://www.w3.org/1999/XSL/Transform";>
>   <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>
> 
>   <xsl:template match="/">
>     <xsl:apply-templates />
>   </xsl:template>
> 
> </xsl:stylesheet>
> 
> 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>
> <xsl:template>
> 
> when I think I have finished and added sufficient templates that 
> <xsl:apply-templates/>
> 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.
> 
> David
> 
> ______________________________________________________________
> __________
> 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:
> http://www.star.net.uk
> ______________________________________________________________
> __________

Current Thread