XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition (549 page)

BOOK: XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition
9.14Mb size Format: txt, pdf, ePub

Another common case is
document(‘’)
, which refers to the stylesheet itself. This construct was often used with XSLT 1.0, where it provided a convenient way to maintain look-up tables in the stylesheet itself. It is likely to be less common with XSLT 2.0, since the ability to hold a temporary tree in a global variable is usually much more convenient. The URI may be supplied as an
xs:string
, an
xs:anyURI
, or an untyped atomic value, and in each case is converted to a string. (XSLT 1.0 also allowed it to be supplied as a boolean or an integer, which creates a theoretical backward incompatibility—but since converting a boolean or number is unlikely to yield a useful URL, the point is rather academic.)

The string is treated as a URI reference; that is, a URI optionally followed by a fragment identifier separated from the URI proper by a
#
character. If it is a relative URI, it is treated as being relative to the base URI of the stylesheet element that contains the expression in which the function call was encountered. This will normally be the URI of the principal stylesheet document, but it may be different if

or

was used, or if pieces of the stylesheet are contained in external XML entities, or if the base URI of any relevant element in the stylesheet has been set explicitly by using the
xml:base
attribute.

Again, all this really means is that relative URLs are handled just like relative URLs in HTML. If you write
document(‘tax-rates.xml’)
in a particular stylesheet module, then the system looks for the file
tax-rates.xml
in the same directory as that stylesheet module.

If the string is an empty string, then the document referenced by the base URI is used. The XSLT specification states that
document(“”)
will return the root node of the stylesheet. Strictly speaking, however, this is true only if the base URI of the XSL element containing the call to the
document()
function is the same as the system identifier of the stylesheet module. If the base URI is different, perhaps because the stylesheet has been built up from a number of external entities, or because the
xml:base
attribute has been used, the object loaded by
document(“”)
will not necessarily be the current stylesheet module; in fact, it might not be a well-formed document at all, in which case an error will be reported.

If the call is contained in a stylesheet brought in using

or

, it returns the root node of the included or imported stylesheet, not that of the principal stylesheet document.

Usage: document() Applied to Atomic Values

With XSLT 1.0, this form of the
document()
function was very useful for handling data used by the stylesheet for reference information; for example, look-up tables to expand abbreviations, message files in different languages, or the text of the message of the day, to be displayed to users on the login screen. Such data can either be in the stylesheet itself (referenced as
document(“”)
) or be in a separate file held in the same directory as the stylesheet (referenced as
document(“messages.xml”)
) or a related directory (for example
document(“../data/messages.xml”)
.

Other books

Ship Who Searched by Mercedes Lackey, Anne McCaffrey
5 Minutes and 42 Seconds by Timothy Williams
The Circle: Rain's Story by Blue, Treasure E.
Dirty Trouble by J.M. Griffin
What A Girl Wants by Liz Maverick
The Master of the Priory by Annie Haynes
Beads of Doubt by Smith, Barbara Burnett
On Borrowed Time by David Rosenfelt