XML technologies / XSLT / Introduction to XSLT 2.0 / XSLT and XPath – round three / Modular stylesheets / Import precedence – resolving import conflicts

(Excerpt from “XSLT 2.0 & XPath 2.0” by Frank Bongers, chapter 3.)

Even CSS stylesheets import styling rules. See Frank Bongers, “XHTML, HTML & CSS”, Galileo Press. Imported CSS rules for the same element tend to accumulate. Conflicting individual rules favor the importing stylesheet.

Imported XSLT has similar precedence, but a different starting point: simple accumulation of rules occurs only in certain cases .

Importing versus including

XSLT need not fit everything in one stylesheet. Two similar instructions combine external modules with a principal stylesheet:

  • xsl:import

  • This declaration imports external rules and declarations:

    <xsl:import href="pfad/externes_modul.xsl" />

  • xsl:include

  • This declaration includes external rules and declarations:

    <xsl:include href="pfad/externes_modul.xsl" />

Both are always empty and use an href attribute to load an external module. They look identical but differ in resolving conflicts.

Importing with xsl:import

The first instruction, xsl:import, partly resembles CSS imports, but matching template rules with equal match attributes do not accumulate. Import precedence immediately applies: lower-ranked external template rules competing with local ones are hidden. More on this shortly.

Inserting with xsl:include

Unlike xsl:import, which tracks external rules’ origin and rank, xsl:include is a physical copy. External rules act as if written at the inclusion position.

The position of xsl:include is unrestricted except that it is top-level. The inclusion position matters because the last duplicate template rule in document order is used. Putting inclusion at the end favors included rules; putting it first favors local rules.

Included rules have exactly the same import precedence as local rules. This matters when an including module is itself imported. Rules behave as physically inserted into the principal stylesheet, as if the parser replaced the xsl:include instruction with all external rules at that point, at the stylesheet’s beginning.

Importing external stylesheets with xsl:import

An xsl:import statement loads an external module whose rules have lower precedence than local ones.

<xsl:import href="pfad/dateiname.xsl" />

Element xsl:import is top-level and cannot occur inside template bodies. It must precede any xsl:include elements and all other top-level elements. Multiple xsl:import instructions may occur consecutively.

If a stylesheet included with xsl:include contains xsl:import instructions, they are treated as following the principal stylesheet’s last actual xsl:import instructions, regardless of inclusion position, effectively moving them forward.

A compact stylesheet combines standard-article formatting with table formatting.

...
<!-- artikel-mit-tabelle.xsl -->
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:import href="artikel.xsl"/>
  <xsl:import href="tabelle.xsl"/>
</xsl:stylesheet>

Stylesheet artikel.xsl can itself import bil­der.xsl containing image templates.

These cascading imports form an import branch (import tree). Each stylesheet has an import child for every xsl:import it contains. Those xsl:import instructions run before the completed tree is added to the principal stylesheet.

Thus the indirectly imported bilder.xsl is added before tabelle.xsl is imported.

Import precedence example

Here is a more complex scenario; usually things are simpler.

  • Module 1 (master.xsl) imports modules 4 and 2, in that order.

  • Module 2 imports module 3.

  • Module 4 imports modules 6 and 5, in that order.

Complex import precedence scenario

Figure: Complex import precedence scenario.

Increasing precedence is: module 6, then 5, 4, 3, 2 and finally 1 (master.xsl, highest precedence).

  • Module 1 rules override all other modules’ rules.
    The order of xsl:import instructions matters, shown by gray rectangles. The last instruction has highest import precedence: the third exceeds the second, which exceeds the first, if that many exist.

  • The entire last-imported branch ranks higher.
    If an imported module imports others, those have lower precedence: modules 5 and 6 rank below module 4.
    However, its whole import branch ranks above modules imported before the branch root. Module 3 therefore exceeds module 4 despite being indirect, because it comes through the higher-ranked module 2.

  • Repeated imports are harmless.
    Repeated imports are allowed though not very useful. Rules then occur with different precedences, as can happen in complex configurations, without especially helpful effects.

   

back to top