Re: [xsl] Preference rules for /something and /*

Subject: Re: [xsl] Preference rules for /something and /*
From: Wendell Piez <wapiez@xxxxxxxxxxxxxxxx>
Date: Mon, 18 Jun 2007 16:45:04 -0400

At 04:07 PM 6/18/2007, you wrote:
I'm not sure if I get this right, so I'm asking here. If I have the following two rules:

<xsl:template match="/root" />
<xsl:template match="/*" />

I receive an "ambiguous rule match" error if the input matches the first. I figured, '*' is not very selective, so it should've conceived lowest priority (I know that a single '*' has prio -.5). What I'd guess is that I am having a compound rule here, similar to 'node/child' or 'node/*' which both receive prio +.5, with the parent node being the document node. Am I correct?

Yes. Both of these fall into the "anything else" category. There are three categories:

1. Simple tests by name, on the child or attribute axis in a single step
   e.g. "root" or "@root" -- priority 0
2. "Wildcard" matches on the child or attribute axis in a single step
   e.g. "@*" or "comment()" -- priority -0.5
3. Anything else
   e.g. "//root", "/root", "/*/root" or "/*" -- priority 0.5

(There's also a category between rules 1 and 2: a match on a namespace-and-type, with a single step, such as "html:*" or "svg:node()", gets priority 0.25.)

Or should a processor distinguish and select the first as having a higher prio (which intuitively seems more correct)?

Early drafts of XSLT attempted to specify such a cascade, but either it proved difficult to specify completely, or it appeared unlikely to work transparently enough, so the rules were simplified.

This does mean that you can get edge cases like yours, where matches using wildcards (node tests by type, that is, as opposed to those by name) fall into the "anything else" category by virtue of extra steps in the XPath.

Of course I can use priority='x', but I'm curious about the default prio here.

Given the rough granularity of the discriminations, it's generally wise to make priorities explicit as soon as there's any reasonable chance for confusion.


PS: the same happens when I over-specify the first rule, i.e.: /root[foo[bar='y']]

Yes, that's also an "anything else".

Wendell Piez                            mailto:wapiez@xxxxxxxxxxxxxxxx
Mulberry Technologies, Inc.      
17 West Jefferson Street                    Direct Phone: 301/315-9635
Suite 207                                          Phone: 301/315-9631
Rockville, MD  20850                                 Fax: 301/315-8285
  Mulberry Technologies: A Consultancy Specializing in SGML and XML

Current Thread