2.6 Combining Stylesheets
Combining Stylesheets
XSLT provides two mechanisms to combine stylesheets:
an inclusion mechanism that allows stylesheets to be combined
without changing the semantics of the stylesheets being combined,
and
an import mechanism that allows stylesheets to override each
other.
Stylesheet Inclusion[top]
Stylesheet Inclusion
<
include
href=
uri-reference
>
<-- Content: -->
<
/include>
An XSLT stylesheet may include another XSLT stylesheet using an
xsl:include element. The xsl:include element
has an href attribute whose value is a URI reference
identifying the stylesheet to be included. A relative URI is resolved
relative to the base URI of the xsl:include element (see
[Base URI]).
The xsl:include element is only allowed as a Top-level element.
The inclusion works at the XML tree level. The resource located by
the href attribute value is parsed as an XML document,
and the children of the xsl:stylesheet element in this
document replace the xsl:include element in the including
document. The fact that template rules or definitions are included
does not affect the way they are processed.
The included stylesheet may use the simplified syntax described in
[Literal Result Element as Stylesheet]. The included stylesheet
is treated the same as the equivalent xsl:stylesheet
element.
It is an error if a stylesheet directly or indirectly includes
itself.
NOTE:
Including a stylesheet multiple times can cause errors
because of duplicate definitions. Such multiple inclusions are less
obvious when they are indirect. For example, if stylesheet
B includes stylesheet A, stylesheet C
includes stylesheet A, and stylesheet D includes
both stylesheet B and stylesheet C, then
A will be included indirectly by D twice. If
all of B, C and D are used as
independent stylesheets, then the error can be avoided by separating
everything in B other than the inclusion of A
into a separate stylesheet B' and changing B to
contain just inclusions of B' and A, similarly
for C, and then changing D to include
A, B', C'.
Stylesheet Import[top]
Stylesheet Import
<
import
href=
uri-reference
>
<-- Content: -->
<
/import>
An XSLT stylesheet may import another XSLT stylesheet using an
xsl:import element. Importing a stylesheet is the same
as including it (see [Stylesheet Inclusion]) except that definitions
and template rules in the importing stylesheet take precedence over
template rules and definitions in the imported stylesheet; this is
described in more detail below. The xsl:import element
has an href attribute whose value is a URI reference
identifying the stylesheet to be imported. A relative URI is resolved
relative to the base URI of the xsl:import element (see
[Base URI]).
The xsl:import element is only allowed as a Top-level element. The
xsl:import element children must precede all other
element children of an xsl:stylesheet element, including
any xsl:include element children. When
xsl:include is used to include a stylesheet, any
xsl:import elements in the included document are moved up
in the including document to after any existing
xsl:import elements in the including document.
For example,
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:import href="article.xsl"/>
<xsl:import href="bigfont.xsl"/>
<xsl:attribute-set name="note-style">
<xsl:attribute name="font-style">italic</xsl:attribute>
</xsl:attribute-set>
</xsl:stylesheet>
The
xsl:stylesheet elements encountered during processing of
a stylesheet that contains xsl:import elements are
treated as forming an import tree. In the import tree,
each xsl:stylesheet element has one import child for each
xsl:import element that it contains. Any
xsl:include elements are resolved before constructing the
import tree. An xsl:stylesheet element in the import tree
is defined to have lower import precedence than another
xsl:stylesheet element in the import tree if it would be
visited before that xsl:stylesheet element in a
post-order traversal of the import tree (i.e. a traversal of the
import tree in which an xsl:stylesheet element is visited
after its import children). Each definition and template
rule has import precedence determined by the
xsl:stylesheet element that contains it.
For example, suppose
-
stylesheet A imports stylesheets B
and C in that order;
-
stylesheet B imports stylesheet
D;
-
stylesheet C imports stylesheet
E.
Then the order of import precedence (lowest first) is
D, B, E, C,
A.
NOTE:
Since xsl:import elements are required to occur
before any definitions or template rules, an implementation that
processes imported stylesheets at the point at which it encounters the
xsl:import element will encounter definitions and
template rules in increasing order of import precedence.
In general, a definition or template rule with higher import
precedence takes precedence over a definition or template rule with
lower import precedence. This is defined in detail for each kind of
definition and for template rules.
It is an error if a stylesheet directly or indirectly imports
itself. Apart from this, the case where a stylesheet with a particular
URI is imported in multiple places is not treated specially. The
Import Tree will have a
separate xsl:stylesheet for each place that it is
imported.
NOTE:
If xsl:apply-imports is used (see [Overriding Template Rules]), the behavior may be different from the
behavior if the stylesheet had been imported only at the place with
the highest Import Precedence.
|