XML technologies / XSLT / Introduction to XSLT 2.0 / XSLT and XPath – round three / Modular stylesheets / xsl:apply-imports
(Excerpt from "XSLT 2.0 & XPath 2.0" by Frank Bongers, Chapter 3.)
Normally, an imported template rule is inactive when an equivalent rule with higher import precedence exists, whether in the main module or the same or another import branch. When a node is to be processed and its match-pattern matches, the rule with the highest import precedence is always activated. Sometimes, however, an imported rule should also run as a subordinate template rule in addition to the regular one. In this particular case, use the instruction xsl:apply-imports.
<!-- lokale Regel: -->
<xsl:template match="beispiel">
<html>
<head>
<title>Beispiel für apply-imports</title>
</head>
<body>
<p>Ich verarbeite Beispiele. Mehr kann ich nicht.</p>
<xsl:apply-imports/>
<p>Ende der Verarbeitung des Beispiels.</p>
</body>
</html>
</xsl:template>
Code example: kap03/3.8/ apply-imports-beispiel.xsl.
Exactly one template rule is activated each time: the one with the next lower precedence, never all eligible rules.
<!-- importierte Regel: -->
<xsl:template match="beispiel">
<p><b>Hallo Welt! Hier ist die Importregel!<br/> Der Beispielsatz hat <xsl:value-of select="string-length()"/> Zeichen.</b></p>
<xsl:apply-templates/>
</xsl:template>
Code example: kap03/3.8/modul-apply-imports.xsl.
If no imported rule can be activated, this is not an error. An appropriate default rule is activated instead; in this respect, xsl:apply-imports behaves like xsl:apply-templates.
The call goes from the template rule currently being processed (Current Template Rule) to the imported template whose match-attribute most closely matches that of the current template rule (next matching). Process the example file kap3/3.8/apply-imports-beispiel.xml with the stylesheet above to obtain a view like the one in this figure.
Figure: Result of processing an imported module.
The lines shown in bold are added by the reactivated imported rule. The example text itself is output by the default rule for text nodes, invoked by the imported rule’s xsl:apply-templates instruction.
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 terms as the printed edition: the work and all its parts are protected by copyright. All rights are reserved, including reproduction, translation, microfilming, and storage and processing in electronic systems.
Galileo Press, Rheinwerkallee 4, 53227 Bonn