Base URIs and the document() Function

In our previous stylesheet, we used the document() function to select some number of nodes from the original source document (our list of purchase orders), and then open those files. There are a number of ways to invoke the document() function; we’ll discuss them briefly here.

The most common way to use the document() function is as we just did. We use an XPath expression to describe a node-set; the document() function takes each node in the node-set, converts it to a string, and then uses that string as a URI. So, when we passed a node-set containing the filename attributes in the list of purchase orders, each one is used as a URI. If those URIs are relative references (i.e., they don’t begin with a protocol such as http), the XSLT processor needs a base URI. If the argument is a node, the base URI of the node is used to resolve the relative reference. If the argument is a string, as it is here, the base URI of the stylesheet is used.

Every node in the XPath source tree is associated with a base URI. When using the document() function, the base URI is important for resolving references to various resources specified with relative links.

Note

Here I’ll offer more detail about base URIs than you’re ever likely to need: If a given node is an element or processing instruction node, and that node occurs in an external entity, then the base URI for that node is the base URI of the external entity. If an element or processing instruction node does not ...

Get XSLT, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.