Re: [xsl] Confused with templates and XPath

Subject: Re: [xsl] Confused with templates and XPath
From: Frédéric Laurent <fl@xxxxxxxxxxxxx>
Date: Thu, 8 Jan 2004 12:58:39 +0100
David Carlisle wrote:
>>Doesnt the //para apply to any para in the document?
> 
> 
> yes a match of "//para" is exactly the same as a match of "para" and
> will match every para element.
> 
> Normally we tell people that starting a match with // does nothing
> useful, but does no harm either, but in this case it does do harm.
> 
> If the stylesheet had
> 
> <xsl:template match="para">
> <xsl:template match="chapter/para">
> 
> Then both templates would match para children of chapter.
> XSLT never applies too templates and if two match it uses the one of
> highest priority. As neither of these templates has a priority attribute
> the default priorities will be used, and the default priority for a
> match involving a "/" step is higher than that of a match with just a
> single element name, so the first template will be used if teh para is
> _not_ a child of chapter and the second will be used if it is.
> 
> However in your case
> 
> 
> <xsl:template match="//para">
> <xsl:template match="chapter/para">
> 
> both templates have the same priority, this is an error and the system
> could just fail and produce no output, but it is allowed to recover
> from the error and use the last specified template, in which case
> it will act as above, if they are really specified in this order.
> 
> You should report that as a bug to the author of the stylesheet, relying
> on a system's silent error recovery is bad practice.

Are you sure of that ?

The following stylesheet

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"   
       xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
<xsl:output method = "text" />
 <xsl:template match="chapter/para">
   chapter-para : <xsl:apply-templates />
 </xsl:template>
 <xsl:template match="//para">
   default-para : <xsl:apply-templates />
 </xsl:template>
</xsl:stylesheet>

with the following xml file
<doc>
	<para>para1</para>
	<chapter><para>para2</para></chapter>
</doc>

produces 

default-para : para1
chapter-para : para2

even if I change the template order (//para before chapter/para)

In the XSLT specification, I can read in the conflict paragraph*

"The next less specific kind of pattern (a pattern that tests for a node with a 
particular type and an expanded-name with a particular namespace URI) has 
priority -0.25. Patterns less specific than this (patterns that just tests for 
nodes with particular types) have priority -0.5. Patterns more specific than 
the most common kind of pattern have priority 0.5."

[*] http://www.w3.org/TR/xslt#conflict

>From my point of view, 
chapter/para is more specific, so its priority is 0.5
and //para is less specific, so its priority is negative.

Then the conclusion is that the more specific pattern is called, for
a given para.

Am I wrong ?

Cheers

Fred
-- 
XPath free testing software :  http://lantern.sourceforge.net
Frédéric Laurent                     http://www.opikanoba.org

 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread