Read XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition Online
Authors: Michael Kay
Content
None; the element is always empty.
Effect
The URI contained in the
href
attribute may be an absolute URI or a relative URI reference. If relative, it is interpreted relative to the base URI of the XML document or external entity containing the
main.xsl
contains the element
date.xsl
in the same directory as
main.xsl
. You can change this behavior by using the
xml:base
attribute to specify a base URI explicitly, as described in Chapter 2 on page 54.
The URI (once resolved) must identify an XML document that is a valid XSLT stylesheet. The top-level elements of this stylesheet are logically inserted into the including stylesheet module at the point where the
The included stylesheet module may use the simplified stylesheet syntax, described in Chapter 3. This allows an entire stylesheet module to be defined as the content of an element such as
. It is then treated as if it were a module containing a single template, whose match pattern is
/
and whose content is the literal result element.
The included stylesheet module may contain
It is not an error to include the same stylesheet module more than once, either directly or indirectly, but it is not a useful thing to do. It may well cause errors due to the presence of duplicate declarations; in fact, if the module contains definitions of global variables or named templates and is included more than once at the same import precedence, such errors are inevitable.
Usage and Examples
#include
directive in C. It provides a way of writing a stylesheet in a modular way so that commonly used definitions can be held in a library and used wherever they are needed.
If you are handling a wide range of different document types, the chances are they will have some elements in common, which are to be processed in the same way regardless of where they occur. For example, these might include standard definitions of toolbars, backgrounds, and navigation buttons to appear on your Web pages, as well as standard styles applied to data elements such as product names, e-mail contact addresses, or dates.
To incorporate such standard content into a stylesheet without change, use
It can make a difference where in your stylesheet the
Example: Using
This example shows the use of
Source
This example can be used with any source document, or with none, if you start execution with the named template
main
.
Stylesheet
Consider a principal stylesheet
picture.xsl
that includes a stylesheet
attributes.xsl
, as follows:
xmlns:xsl=“http://www.w3.org/1999/XSL/Transform”>
This includes the module
attributes.xsl
:
xmlns:xsl=“http://www.w3.org/1999/XSL/Transform”>
The named attribute set in the included stylesheet is used exactly as if it were defined in the principal stylesheet, at the point where the
Output
The resulting output is:
This is because attributes generated using
See Also
xsl:key
The
key()
function in expressions and patterns.
Changes in 2.0
The restrictions on using global variables in the
match
and
use
attributes have been eased.
It is now possible to define keys of any type (for example numbers or dates) and to a specify a collation to be used when deciding whether two string-valued keys match.
The key values for a node can now be evaluated using a sequence constructor contained in the
use
attribute. This allows the evaluation to invoke templates or other XSLT constructs such as
Format
name = qname
match = pattern
use? = expression
collation? = uri>
Position
Attributes
Name | Value | Meaning |
name mandatory | Lexical QName | The name of the key |
match mandatory | Pattern | Defines the nodes to which this key is applicable |
use optional | XPath Expression | The expression used to determine the value of the key for each of these nodes |
collation optional | URI | The name of a collation used to compare string-valued keys |
The syntax for a
Pattern
is defined in Chapter 12.
The
use
attribute and the sequence constructor are mutually exclusive: if the
use
attribute is present, the element must be empty, and if it is absent, then there must be a nonempty sequence constructor.
Content
A sequence constructor. This is used as an alternative to the
use
attribute to determine the value of the key.
Effect
The
name
attribute specifies the name of the key. It must be a valid lexical QName; if it contains a namespace prefix, the prefix must identify a namespace declaration that is in scope on the
The
match
attribute specifies the nodes to which the key applies. The value is a
Pattern
, as described in Chapter 12. If a node doesn't match the pattern, then it will have no values for the named key. If a node does match the pattern, then the node will have zero or more values for the named key, as determined by the
use
attribute or the contained sequence constructor.
The simplest case is where each node has one key value, and the key values are unique. For example, consider the following source document:
In the stylesheet you can define a key for the registration number of these vehicles, as follows:
The
use
attribute (or in its absense, the contained sequence constructor) specifies an expression used to determine the value or values of the key. This expression doesn't have to select an attribute, like
@reg
in the example above. For example, it could select a child element. It could also be a computed value, such as
count(*)
(the number of child elements that the node has). If the value is a sequence, the system creates an index entry for each item in the sequence, and the node can be found using any one of these values.