XML technologies / XSLT / Reference: XSLT 1.0 and 2.0 elements / apply-imports
(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
Instruction xsl:apply-imports concerns imported stylesheet modules. It reactivates template rules overridden by same-pattern rules of higher import precedence in the importing module.
Classification | Instruction |
Function group | template rules and invocation |
Introduction | XSLT 1.0 |
Position in the stylesheet and permitted content:
xsl:apply-imports is an instruction used only in template blocks, but never inside xsl:for-each or xsl:for-each-group instructions.
Since XSLT 2.0, instructions xsl:with-param are permitted as children of xsl:apply-imports. In XSLT 1.0 the instruction is empty.
Attributes:
All standard attributes are allowed; there are no element-specific attributes.
Purpose:
If a module imports another whose rules share the same match (pattern ) and mode, the imported rule is disabled because it has lower precedence. This prevents imported duplicates from interfering with rules in the main module.
Conversely, general rules intended to be overridden can be collected in a reusable module. Another module, not necessarily the main one, imports it and overrides general processing with special instructions.
Sometimes the imported rule should supplement rather than disappear behind the main rule. Use xsl:apply-imports in the importing module’s template to additionally invoke the imported template rule.
Exactly one rule is activated: the next lower-precedence rule in the same mode, not every candidate. The chain continues only if that rule also contains xsl:apply-imports.
If no imported rule can be activated, this is not an error. A suitable default rule is used. In other words, xsl:apply-imports then behaves like xsl:apply-templates.
Rules in the same import branch:
Only lower-precedence rules in the same import branch as the invoking module are accessible. When invoked from the main module, this includes every imported rule.
An imported module cannot activate a rule from a module it does not import directly or indirectly. A rule in another branch is inaccessible even if its precedence, viewed from the main module, is lower.
Figure: Multiple import branches.
The main module imports A and B; B imports C. The main module can activate rules in all three, and B can activate C’s rules.
A and C cannot activate further imported rules because their branches end there. In particular, A cannot activate B or C, despite C having lower precedence from the main module’s viewpoint.
Current template rule is preserved:
Activating an imported template preserves the current template rule (current template rule), so the current node remains unchanged. The current mode and tunnel-parameter stream also pass through.
Invocation from a named template:
If an invocation xsl:apply-imports occurs in a named template, the current template rule is the one that called it through xsl:call-template. (Note: This is because xsl:call-template does not change the current template rule.)
No activation of imported named templates:
Instruction xsl:apply-imports operates on the current node and rule. A named template therefore cannot use xsl:apply-imports to invoke a same-named template of lower precedence. It instead activates a matching rule relative to the current rule. (Note: An imported named template can only be called when no higher-precedence namesake exists.)
No invocation from xsl:for-each or xsl:for-each-group:
A non-null current template rule must exist at invocation time.
This is normally true inside a template rule except within xsl:for-each or xsl:for-each-group instructions, where the current node changes and the rule becomes temporarily null until the block ends.
Warning – not usable in xsl:for-each- or xsl:for-each-group!
Inside xsl:for-each or xsl:for-each-group blocks, xsl:apply-imports cannot be used because the current template rule is null. Attempting it causes a runtime error (dynamic error).
Passing parameters to activated rules:
In XSLT 1.0, xsl:apply-imports is empty. Since XSLT 2.0, xsl:apply-imports may contain one or more xsl:with-param instructions passing parameter values to the activated rule.
Actual acceptance is not enforced. Unlike parameter passing in xsl:call-template, it is not an error if the target template has no corresponding xsl:param element.
The current tunnel parameters always pass through xsl:apply-imports and remain available in the activated rule and rules it activates.
No activatable rule found:
If no imported rule can be activated, this is not an error: xsl:apply-imports behaves like xsl:apply-templates, activating the built-in default rule for the current node’s type.
Any xsl:with-param elements in the invocation are ignored. (Note: This mirrors xsl:apply-templates and xsl:next-match.)
The powerful alternative – xsl:next-match:
XSLT 2.0 adds an instruction supplementing xsl:apply-imports: xsl:next-match, also able to invoke overridden rules. Unlike xsl:apply-imports, xsl:next-match can activate not only lower import precedence but also lower priority. As with xsl:apply-imports, the mode must match the current rule. Overridden named templates remain inaccessible.
Example – activating an imported rule:
Suppose external module allgemein.xsl defines a rule for element <beispiel> besitzt:
<!-- Regel in externem Modul: -->
<xsl:template match="beispiel">
<b>Beispiel: </b><xsl:apply-templates/>
</xsl:template>
Another stylesheet, spezial.xsl, acting as main module, could import allgemein.xsl and use xsl:apply-imports to supplement processing of elements <beispiel> as follows:
<xsl:import href="spezial.xsl"/>
<!-- Regel im Hauptstylesheet: -->
<xsl:template match="beispiel">
<div style="border: solid red">
<!-- deaktivierte Template-Regel wird aufgerufen: -->
<xsl:apply-imports/>
</div>
</xsl:template>
Together, processing an element <beispiel> produces:
<div style="border: solid red">
<b>Beispiel: </b>
... [hier stehen Inhalte aus dem XML-Dokument] ...
</div>
The result nests the template bodies. Think of the reactivated body as included where the main template contains xsl:apply-imports.
The main template borrows instruction xsl:apply-templates from the imported rule to continue processing. Instruction xsl:apply-imports effectively replaces the usual xsl:apply-templates.
Element definition:
XSLT 1.0:
<!-- Category: instruction -->
<xsl:apply-imports />
XSLT 2.0:
<!-- Category: instruction -->
<xsl:apply-imports>
<!-- Content: xsl:with-param* -->
</xsl:apply-imports>
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