Read XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition Online
Authors: Michael Kay
If any of the nodes contains a relative URI, it will be resolved relative to the base URI of that node. The base URI of a node is established using the rules given on page 45. In fact, each node in the supplied sequence could potentially have a different base URI.
This all sounds terribly complicated, but all it really means is that if the source document contains the link
data.xml
, then the system will look for the file
data.xml
in the same directory as the source document.
These rules also cover the case where the argument is a reference to a variable containing a temporary tree, for example:
…
In this case relative URI
index.xml
is resolved relative to the base URI of the
Usage: document() Applied to Nodes
A common use of the
document()
function is to access a document referenced from the source document, typically in an attribute such as
href
. For example, a book catalog might include links to reviews of each book, in a format such as:
text=“reviews/NYT/19991228/rev3.xml”/>
text=“reviews/WPost/20000106/rev12.xml”/>
If you want to incorporate the text of these reviews in your output document, you can achieve this using the
document()
function. For example:
Review in
As the argument
@text
is a node, the result will be the root node of the document whose URI is the value of the
text
attribute, interpreted relative to the base URI of the
xml:base
attribute on some ancestor node) will be the same as the URI of the source document itself.
Note that in processing the review document, exactly the same template rules are used as we used for the source document itself. There is no concept of particular template rules being tied to particular document types. If the review document uses the same element tags as the book catalog, but with different meanings, this can potentially create problems. There are two possible ways round this:
You might find that even if the element names are distinct, the use of modes is a good discipline for maintaining readability of your stylesheet. For more detail on modes, see
Another useful approach, which helps to keep your stylesheet modular, is to include the templates for processing the review document in a separate stylesheet incorporated using
Example: Using the document() Function to Analyze a Stylesheet
A stylesheet is an XML document, so it can be used as the input to another stylesheet. This makes it very easy to write little tools that manipulate stylesheets. This example shows such a tool, designed to report on the hierarchic structure of the modules that make up a stylesheet.
This example uses the
document()
function to examine a stylesheet and see which stylesheet modules it incorporates using
Source
The source is any stylesheet, preferably one that uses
dummy.xsl
is provided in the code download for the book for you to use as a sample.
Stylesheet
The stylesheet
list-includes.xsl
uses the
document()
function to access the document referenced in the
href
attribute of
xmlns:xsl=“http://www.w3.org/1999/XSL/Transform”
version=“1.0”
>
Stylesheet Module Structure
select=“$module/*/xsl:include | $module/*/xsl:import”/>
Output
The output for the
dummy.xsl
stylesheet is as shown in
Figure 13-1
.
URIs as Atomic Values
As an alternative to supplying a URI that is held in the content of a node, the first argument may supply a URI as an atomic string. For convenience, the function accepts both
xs:string
and
xs:anyURI
types, as well as untyped atomic values. (Untyped atomic values are unlikely to arise in practice, since they normally arise only from atomizing a node in a schemaless document, and if you supply a node as an argument to the
document()
function, then the rules that apply are those in the previous section,
URIs Held in Nodes
.)
The first argument may be evaluated to produce a single atomic value containing a URI or a sequence of them. It is even possible to mix atomic values and nodes in the input sequence; nodes are processed as described in the previous section, and atomic values as described here.
The most common case is a URL hard-coded in the stylesheet, for example
document (‘tax- rates.xml’)
.