XML technologies / XSLT / Reference: XSLT 1.0 and 2.0 elements / include

(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

 

The top-level element xsl:include inserts declarations and template rules from one stylesheet into another. Inclusion has the effect of physically copying the content into the principal stylesheet at the declaration’s position.

Classification

Declaration

Function group

Stylesheet structure

Introduction

XSLT 1.0

Position in the stylesheet and permitted contents:

xsl:include may occur only as a child of xsl:stylesheet or xsl:transform. Unlike xsl:import, its position relative to other top-level elements is unrestricted, so xsl:include may occur anywhere at this level, any number of times, though not before any existing xsl:import. Any number of xsl:include instructions may occur. The element is always empty.

Attributes:

Standard attributes apply. Declaration xsl:include also has a required attribute href.

href

Value

uri-reference

Usage

Required

Introduction

XSLT 1.0

The required href attribute contains a relative or absolute URI. A relative URI is resolved against the including stylesheet’s base URI.

Purpose:

Instruction xsl:include inserts an external stylesheet’s declarations and template rules into the principal stylesheet. Included declarations and rules have exactly the same import precedence as those defined locally. They behave as though physically inserted in place of the xsl:include instruction.

Note – Creating compatible stylesheets
To create a stylesheet for both XSLT 1.0 and 2.0, write two different principal modules (principal stylesheet modules) specifying version version="1.0" or version="2.0" respectively, serving only as entry modules.
Use xsl:import or xsl:include to load the desired modules, possibly different ones. Each can have its own version, “1.0” or “2.0”, according to whether it contains XSLT 2.0 instructions.

No self-inclusion
A stylesheet must not accidentally include itself, directly or indirectly; this produces an error. As with xsl:import, such a circular reference is forbidden and is a static error (ERR XTSE0180).

Resolving conflicts
If inclusion creates identical template rules, the processor handles them according to document order. Usually the last rule in the stylesheet applies, so place the inclusion near the beginning. This lets the XSLT processor prefer local rules when duplication occurs accidentally.

Included xsl:import declarations are an exception to external-content placement. They are not inserted with other declarations and rules at the xsl:include instruction, but between local import instructions and subsequent top-level elements. The last import declaration has highest import precedence, so an included import takes precedence over local imports.

Including global parameter and variable declarations
Take care when including global parameter and variable declarations xsl:param and xsl:variable. Never include global declarations with names already defined in the including stylesheet or introduced by other inclusions. Import precedence cannot resolve this, since included declarations have equal rank. Duplicate names therefore cause a static error (ERR XTSE0630). Local template parameters and variables are unaffected.

Including function declarations
Modules containing function declarations xsl:function can be included provided this does not duplicate both function name and number of parameters (arity). Functions sharing a name but having different parameter counts are allowed. Forbidden duplicates cause a static error (ERR XTSE0770).

Including key declarations
Declarations of xsl:key can also be included with an external stylesheet. Import precedence plays no role; all keys are valid. When referencing them using key(), ensure the document containing the elements to search is in focus.

Base URIs belong to modules
Included rules and declarations retain their base URIs, which matters when they contain further xsl:include or xsl:import instructions or use function document() in XPath expressions.

Namespace declarations belong to modules
The namespaces defined in external modules still apply only to their included rules and declarations. This also applies to namespaces excluded with exclude-result-prefixes, extension-function namespaces declared with extension-element-prefixes and namespaces excluded from result copying for literal result elements with xsl:exclude-result-prefixes. Likewise, the including stylesheet’s namespace declarations do not extend to embedded rules.

Examples:

Example 1 – Including a stylesheet module:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
  <xsl:include href="beispiel_include.xsl"/>
  <xsl:template match="/">
    ...
  </xsl:template>
</xsl:stylesheet>

An external module is included in the principal stylesheet.

Example 2 – Alternative inclusion using use-when

In XSLT 2.0, standard attribute use-when permits conditional inclusion:

<xsl:include href="beispiel_include_1.xsl" use-when="system-property('xsl:vendor')='Altova'"/>
<xsl:include href="beispiel_include_2.xsl" use-when="system-property('xsl:vendor')='Saxonica'"/>

Different modules are included depending on the executing processor. This works only with XSLT 2.0 processors: an XSLT 1.0 processor in forwards-compatible mode ignores the use-when attribute and includes both.

Element definition:

XSLT 1.0:

<!-- Category: top-level-element -->
<xsl:include 
     href = uri-reference 
/>

XSLT 2.0:

<!-- Category: declaration -->
<xsl:include
     href = uri-reference 
/>

   

  

back to top