Hi,
The actual rules run something like this. To determine which template is
applied when a node is selected, all matching templates are considered.
When more than one template matches:
1. The one with highest "import precedence" is selected
(Import precedence is determined by your import hierarchy when you use
xsl:import to bring in stylesheet modules, enabling you to override
imported templates transparently in an importing stylesheet. If you
want modularity without this behavior, use xsl:include.)
2. If two or more templates have the same import precedence, then each
candidate template is assigned a priority as follows, and the one with the
highest priority is used:
2a. If an explicit priority is given, this is the priority assigned.
2b. If no explicit priority is given, priority is assigned as follows:
match with just a node test by name - priority is 0
(e.g. match="div" or match="@type")
match with a node test only by type - priority is -0.5
(e.g. match="*" or match="comment()")
match with a node test by type, except namespace-qualified - priority is -0.25
(e.g. match="html:*")
anything else - priority 0.5
(e.g. match="div/p" or match="p[not(preceding-sibling::*)]" or
match="//div")
Matches on either side of a "|" operator are considered separately, so if
you have match="div/div | p" the priority will be 0.5 or 0 depending on
whether it's a div or a p that's doing the matching.
As you can see, this provides for a rough, but not too super-complicated,
assignment of higher priority to "better matches". (Early drafts of XSL had
more complex methods but this is what they settled on.)
But it's not enough to help you pick between "div/div" and "div/div/div"
when you have a div that matches both. Those are cases where you have to
provide a priority explicitly.
(NB: match="div" and match="//div" both match the same nodes, but with
different priorities, 0 and 0.5 respectively. It's because of this that we
frown on match="//div" since this is almost always unintentional but almost
never useful.)
All this is described in the XSLT Rec, at
http://www.w3.org/TR/xslt#conflict "5.5 Conflict Resolution for Template Rules"
Now, you ask, okay, what if priority is assigned and I still have a conflict?
In that case, the behavior depends on your implementation. (It's one of
relatively few occasions where implementations are allowed to differ.)
One reason many of us like Saxon for development is that Saxon will signal
a warning before it uses the last best template. MSXML simply uses that
one. Other processors signal an error. YMMV.
When teaching this stuff, I always recommend to students that they test
their processor on this so they know what to expect.
Cheers,
Wendell
At 04:32 PM 5/4/2005, Jay wrote:
"As a general rule, the more specific the pattern, the higher its
priority."
A misperception on my part. Rules with context (something more than just
the element name) get a priority of .5, regardless of how specific the
context is.
======================================================================
Wendell Piez mailto:wapiez@xxxxxxxxxxxxxxxx
Mulberry Technologies, Inc. http://www.mulberrytech.com
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
======================================================================