Re: [xsl] Question about template priority

Subject: Re: [xsl] Question about template priority
From: David Carlisle <davidc@xxxxxxxxx>
Date: Tue, 09 Feb 2010 16:50:42 +0000
On 09/02/2010 16:39, Houghton,Andrew wrote:
I'm a little perplexed by a transform I have. Basically the input file looks like:

<ns:foo />

The transform file looks like:

<!--(1)--><xsl:template match="node()[generate-id(.) = generate-id(root(.)/*[1])]" />
<!--(2)--><xsl:template match="ns:foo" />
<!--(3)--><xsl:template match="/">
             <xsl:apply-templates select="*[1]" />

What template (1) is suppose to do is catch document elements that are not ns:foo, e.g., someone gave the wrong XML document to the transform. However, template (1) is always invoked regardless of whether the correct XML input document is given or not.

Looking at Michael Kay's book, it says that priority of a template is based on:

1. select all templates that have a match attribute; selected templates (1,2,3)
2. select all templates that have the same mode as the apply-template; selected templates (1,2,3)
3. select all templates whose pattern matches the node; selected templates (1,2)
4. select the one with the highest import precedence; selected templates (1,2)
5. select the one with the highest priority; selected templates (?)

Neither template (1 or 2) have a priority so in order to determine the template with the highest priority it depends upon the match pattern syntax:

1. Pattern1 | Pattern2 = treat Pattern1 and Pattern2 separately and determine their default priority
2. QName, @QName, child::QName, attribute::QName, processing-instruction, (literal) = 0.0 priority
3. NCName:*, @NCName:*, child::NCName:*, attribute::NCName:* = -0.25 priority
4. NodeTest, @NodeTest, child::NodeTest, attribute::NodeTest = -0.5 priority
5. otherwise = 0.5 priority

Template (2) looks to me to be a QName and should therefore get a 0.0 priority.

Template (1) looks to me to be a NodeTest and should therefore get a -0.5 priority.

However, when I run Saxon, it always selects template (1).

So I added a priority="0" to template (1) and Saxon gives me XTRE0504 ambigious rule match indicating that template (2) priority is in fact zero. Changing the priority for template (1) to -0.1 and template (2) is executed which indicates that Saxon thought that the default priority pattern for template (1) was "otherwise" and selected 0.5 for the priority which is why it was executed without a priority attribute.

So why isn't template (1) considered a NodeTest? When I lookup NodeTest in Michael Kay's book in the examples it clearly shows:

because of following [] expression: it's not _just_ a nodetest.

It doesn't affect your question, but

<!--(1)--><xsl:template match="node()[generate-id(.) = generate-id(root(.)/*[1])]" />

is the same as

<!--(1)--><xsl:template match="/*"/>


The Numerical Algorithms Group Ltd is a company registered in England
and Wales with company number 1249803. The registered office is:
Wilkinson House, Jordan Hill Road, Oxford OX2 8DR, United Kingdom.

This e-mail has been scanned for all viruses by Star. The service is
powered by MessageLabs. ________________________________________________________________________

Current Thread