[xsl] Reliance on import precedence considered dangerous

Subject: [xsl] Reliance on import precedence considered dangerous
From: Jeni Tennison <mail@xxxxxxxxxxxxxxxx>
Date: Fri, 16 Feb 2001 10:25:12 +0000
I think I've discovered an odd behaviour concerning imported
stylesheets that I haven't seen mentioned anywhere. I'd be very glad
to be proved wrong (on both it's factual or it's status as a known
problem that David first identified 18 months ago).

When you import the same utility stylesheet (D) into two stylesheets
(B and C) that you then import into a main stylesheet (A), any
templates, attribute sets, global variables or parameters (and so on
for a few other things) in the first stylesheet that's imported (B)
that override the ones in the utility stylesheet (D) will no longer
work as they would if the stylesheet (B) were run standalone.

Here is an illustrative example.

In preparation for XSLT UK, I'm putting together a list of hostelries
that may be good locations for vehement arguments on the use of
xsl:script. I have a neat little utility function that uses the Piez
Technique to output the rating of a hostelry as a number of *s:

<xsl:template match="city[name = 'Oxford']/hostelry" mode="rating">
   <xsl:for-each select="$loadsanodes[position() &lt;= @rating]">

and this I have placed in a utility stylesheet unimaginatively called

I then have two stylesheets that can stand alone if necessary.  One
called 'pubs.xsl' and one called 'restaurants.xsl'.  These create
tables of the pubs and restaurants, including their rating.  In honour
of David Carlisle's penchant for OP, there's a special template of
pubs.xsl that overrides the one above:

<xsl:template match="city[name = 'Oxford']/
                        hostelry[drink = 'Old Peculier']"
   <xsl:for-each select="$loadsanodes[position() &lt;= @rating]">

pubs.xsl works hunkydory on its own - good places to get David that
pint you owe him are labelled appropriately. However, when I import
both pubs.xsl and restaurant.xsl into my main stylesheet:

<xsl:import href="pubs.xsl" />
<xsl:import href="restaurant.xsl" />

Suddenly they stop being labelled!  Why?  Well, looking at the import
tree, utilities.xsl imported by restaurant.xsl has a higher import
precedence than pubs.xsl.  Since the two templates have the same
priority, the one with the highest import precedence wins - the one in
utilities.xsl.  To make it work I have to add an explicit priority to
the template in pubs.xsl.

If this is the case, perhaps it is something that should be added in a
Note in Section 2.6.2 of the XSLT 1.1 WD?  It's not error-producing
like the equivalent with xsl:include, but it might be unexpected.


Jeni Tennison

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

Current Thread