XML technologies / XSLT / Reference: XSLT 1.0 and 2.0 elements / import
(Excerpt from “XSLT 2.0 & XPath 2.0” by Frank Bongers, chapter 6.)
A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z
Top-level element xsl:import imports an external XSL stylesheet module into the current stylesheet or module, adding external template rules and declarations to local ones.
Classification | Declaration |
Function group | Stylesheet structure |
Introduced in | XSLT 1.0 |
Stylesheet position and permitted contents:
Declaration xsl:import is top-level only and must precede every other top-level element, including any declarations of xsl:include. Any number of instructions xsl:import may follow one another. The element itself is always empty.
Attributes:
Standard attributes apply. In addition, xsl:import has mandatory attribute href.
href
Value | uri-reference |
Usage | Mandatory |
Introduced in | XSLT 1.0 |
Attribute href holds the external stylesheet URI. Relative URIs resolve against the importing module’s base URI. Fragment identifiers are optional for implementations, so importing embedded stylesheets by ID is unpredictable.
Purpose:
xsl:import imports external declarations and rules into a principal stylesheet (principal stylesheet module). Processing uses all local and imported rules. Common rules can thus be separate modules imported into any host stylesheet.
Imported rules and declarations retain their own base URIs and namespace declarations. The principal module’s namespaces likewise remain local and do not extend to imported modules.
Note – Compatible stylesheets
For both XSLT 1.0 and 2.0, write two principal modules (principal stylesheet modules) declaring versions version="1.0" and version="2.0" as entry points. Use xsl:import or xsl:include to load the desired modules, possibly different ones. Each can declare version 1.0 or 2.0 depending on its instructions.
Import ordering – Import precedence
Imported rules and declarations rank below local ones. When rules conflict over the match attribute’s pattern, local rules win. This is formally called import precedence: an imported rule has lower import precedence.
Cascading imports
An imported stylesheet may import others using declarations xsl:import. Indirectly imported rules rank below their importer.
Importing several stylesheets
With consecutive instructions xsl:import, the first imported module and its imports have the lowest precedence.
The last imported module and its imports have the highest imported precedence, still below local rules.
Repeated imports, even at different levels, are legal. Direct or indirect self-import is forbidden because it creates a cycle and stops processing, a static error ERR XTSE0210.
Template-rule behavior xsl:template
If several rules share attribute match or name, the highest import precedence wins. The importing stylesheet can reactivate an overridden imported rule with instruction xsl:apply-imports or, where appropriate, xsl:next-match in the local rule instead of xsl:apply-templates.
Other top-level elements
Attribute sets xsl:attribute-set
Attribute sets with the same expanded name combine. Conflicting attributes use the highest import precedence.
Number-format declarations xsl:decimal-format
Same-named declarations ofxsl:decimal-formatcombine across modules, as do unnamed declarations. Conflicting formatting properties use higher import precedence. Conflicts at equal precedence are static errors (ERR XTSE1290).
Warning – Difference from XSLT 1.0:
XSLT 1.0 forbids duplicate decimal formats through imports regardless of precedence, including multiple unnamed defaults.
Global parameters and variables xsl:param and xsl:variable
Same-named globals are allowed at different import precedences. References select the highest; lower-precedence variables are shadowed.
Namespace aliasing with xsl:namespace-alias
If several declarations ofxsl:namespace-aliasshare a namespace prefix, the highest import precedence applies. Equal precedence may be an error.
Whitespace handling with xsl:preserve-space and xsl:strip-space
Conflicts between declarationsxsl:preserve-spaceandxsl:strip-spaceuse higher import precedence. At equal precedence, the more specificNameTesthas higher priority. Normally, whitespace nodes are preserved in elements with equal-precedence instructionsxsl:preserve-spaceandxsl:strip-space.
Result-document properties xsl:output
Same-named and unnamed declarations ofxsl:outputcombine across modules. Conflicts use the highest import precedence.
Comparing: xsl:import versus xsl:include
Like xsl:import, instruction xsl:include combines stylesheets too. However, declaration xsl:include has no import precedence: it inserts content at the declaration’s position with equal rank. Unlike xsl:import, this need not be at the start. Duplicate-rule selection depends on final document order, generally favoring the last rule.
Examples:
Example 1 – Directly importing two stylesheets:
Principal stylesheet:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<!-- importiert Modul 1 ins Hauptstylesheet: -->
<xsl:import href="importbeispiel_1.xsl"/>
<!-- importiert Modul 2 ins Hauptstylesheet: -->
<xsl:import href="importbeispiel_2.xsl"/>
<xsl:template match="/">
<importbeispiel>
<xsl:apply-templates/>
</importbeispiel>
</xsl:template>
<xsl:template match="a">
<ausgabe>
<!-- Verarbeitung des Elements <a> -->
<xsl:apply-templates/>
</ausgabe>
</xsl:template>
</xsl:stylesheet>
The principal stylesheet imports two modules and defines a rule for element <a>.
First imported stylesheet (module 1):
<!-- importbeispiel_1.xsl -->
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:template match="a">
<extern1>
<!-- Verarbeitung des Elements <a> -->
<xsl:apply-templates/>
</extern1>
</xsl:template>
<xsl:template match="b">
<extern1>
<!-- Verarbeitung des Elements <b> -->
<xsl:apply-templates/>
</extern1>
</xsl:template>
<xsl:template match="c">
<extern1>
<!-- Verarbeitung des Elements <c> -->
<xsl:apply-templates/>
</extern1>
</xsl:template>
</xsl:stylesheet>
Module 1 also has a rule for element <a> and rules for <b> and <c>. Each wraps content with literal result element <extern1>. However, the rule for <a> is imported and lower-ranking than the principal rule, so is inactive.
Second imported stylesheet (module 2):
<!-- importbeispiel_2.xsl -->
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:template match="b">
<extern2>
<!-- Verarbeitung des Elements <b> -->
<xsl:apply-templates/>
</extern2>
</xsl:template>
<xsl:template match="d">
<extern2>
<!-- Verarbeitung des Elements <d> -->
<xsl:apply-templates/>
</extern2>
</xsl:template>
</xsl:stylesheet>
Like module 1, module 2 has a rule for <b> and additionally for <d>, previously without a rule. Its literal result element is <extern2>. Since module 2 is imported after module 1, its same-named rules rank higher, affecting <b>, whose module 1 rule it overrides.
Source document:
<?xml version="1.0" encoding="ISO-8859-1"?>
<beispiel>
<a>Inhalt a</a>
<b>Inhalt b</b>
<c>Inhalt c</c>
<d>Inhalt d</d>
</beispiel>
Result document:
<?xml version="1.0" encoding="utf-8"?>
<importbeispiel>
<ausgabe>Inhalt a</ausgabe>
<extern2>Inhalt b</extern2>
<extern1>Inhalt c</extern1>
<extern2>Inhalt d</extern2>
</importbeispiel>
The principal stylesheet processes element <a> because module 1’s rule has lower import precedence. Element <b> uses module 2, which outranks module 1. Elements <c> and <d> have no conflicts, using module 1 and module 2 respectively.
Example 2 – Nested imports of two stylesheets:
Principal stylesheet:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<!-- importiert Modul 1 ins Hauptstylesheet: -->
<xsl:import href="importbeispiel_1b.xsl"/>
<xsl:template match="/">
<importbeispiel>
<xsl:apply-templates/>
</importbeispiel>
</xsl:template>
<xsl:template match="a">
<ausgabe>
<!-- Verarbeitung des Elements <a> -->
<xsl:apply-imports/>
</ausgabe>
</xsl:template>
</xsl:stylesheet>
The principal stylesheet now imports only importbeispiel_1b.xsl, which imports the second module itself.
First imported stylesheet (module 1b):
<!-- importbeispiel_1b.xsl -->
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<!-- importiert Modul 2 in Modul 1: -->
<xsl:import href="importbeispiel_2.xsl"/>
<xsl:template match="a">
<extern1>
<!-- Verarbeitung des Elements <a> -->
<xsl:apply-templates/>
</extern1>
</xsl:template>
<xsl:template match="b">
<extern1>
<!-- Verarbeitung des Elements <b> -->
<xsl:apply-templates/>
</extern1>
</xsl:template>
<xsl:template match="c">
<extern1>
<!-- Verarbeitung des Elements <c> -->
<xsl:apply-templates/>
</extern1>
</xsl:template>
</xsl:stylesheet>
Stylesheet importbeispiel_1b.xsl imports module 2, the previously used importbeispiel_2.xsl. That module and the source document remain unchanged.
The changed import precedences alter the result. The rule for <b> in module 2 is no longer imported directly into the principal module after module 1, but into module 1. It therefore ranks below module 1’s same-named rule and is inactive in this result.
Result document:
<?xml version="1.0" encoding="utf-8"?>
<importbeispiel>
<ausgabe>Inhalt a</ausgabe>
<extern1>Inhalt b</extern1>
<extern1>Inhalt c</extern1>
<extern2>Inhalt d</extern2>
</importbeispiel>
Element <b> now uses the directly imported module; the nested module’s rule has lower precedence. Otherwise examples 1 and 2 behave identically.
Example 1b – Activating a rule with xsl:apply-imports:
An imported rule can run alongside a local one by replacing instruction xsl:apply-templates with xsl:apply-imports. Modify the principal rule for element <a> in example 1 to surround content with literal result element <ausgabe>:
<xsl:template match="a">
<!-- Verarbeitung des Elements <a> -->
<ausgabe>
<!-- ... unter Einbeziehung importierter Regeln: -->
<xsl:apply-imports/>
</ausgabe>
</xsl:template>
The result changes as follows:
<?xml version="1.0" encoding="utf-8"?>
<importbeispiel>
<ausgabe>
<extern1>Inhalt a</extern1>
</ausgabe>
<extern2>Inhalt b</extern2>
<extern1>Inhalt c</extern1>
<extern2>Inhalt d</extern2>
</importbeispiel>
The local rule for <a> runs first, creating element <ausgabe>.
Instruction xsl:apply-imports searches for lower-precedence imported rules and runs them afterward. Local and imported template bodies are effectively nested.
Note on activating imported rules
Reactivating imported rules with xsl:apply-imports works only directly from the importing stylesheet or module, not from a parallel import. Thus example 1 cannot activate the rule for <b> in module 1 from module 2’s higher-ranking rule. Example 2 permits this; see example 2b.
Example 2b – Activating a rule with xsl:apply-imports:
An imported module that imports another can activate that module’s rules hidden by lower import precedence.
Slightly modify the rule for element <b> in import_beispiel_1b.xsl:
<!-- import_beispiel1b.xsl -->
<xsl:template match="b">
<extern1>
<!-- statt xsl:apply-templates: -->
<xsl:apply-imports/>
</extern1>
</xsl:template>
Example 2’s result changes because module 2’s rule now also runs:
<?xml version="1.0" encoding="utf-8"?>
<importbeispiel>
<ausgabe>Inhalt a</ausgabe>
<extern1>
<extern2>Inhalt b</extern2>
</extern1>
<extern1>Inhalt c</extern1>
<extern2>Inhalt d</extern2>
</importbeispiel>
As above, the nested module 2 runs after module 1’s rule is instantiated.
Element definition:
XSLT 1.0:
<!-- Category: top-level-element -->
<xsl:import
href = uri-reference
/>
XSLT 2.0:
<!-- Category: declaration -->
<xsl:import
href = uri-reference
/>
Copyright © Galileo Press, Bonn 2008
You may print the online version for your private use.
Otherwise, this chapter from “XSLT 2.0 & XPath 2.0 ― Das umfassende Handbuch” is subject to the same conditions as the bound edition. The work, including all its parts, is protected by copyright. All rights reserved, including reproduction, translation, microfilming, and storage and processing in electronic systems.
Galileo Press, Rheinwerkallee 4, 53227 Bonn