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

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

The actual algorithm for URI resolution is described in section 5 of Internet RFC 3986 (
http://www.ietf.org/rfc/rfc3986.txt
). In essence, the relative reference is appended after the last
/
in the path component of the base URI, and some tidying up is then done to remove redundant
/./
and
/../
components.The
resolve-uri()
specification leaves some latitude for implementations in deciding how to handle edge cases. It says that the implementation must use “an algorithm such as the ones described in RFC 2396 or RFC 3986”. There are several reasons for this apparent vagueness. Firstly, the working group wanted to allow implementors to take advantage of existing library code. Secondly, the RFCs themselves leave some corner cases open; in particular, they aren't very prescriptive about what happens when the input strings are not strictly valid according to the RFC rules. RFC 3986, for example, says that the base URI must be an absolute URI (which means it mustn't contain a fragment identifier), but it hints that you can turn it into an absolute URI by stripping off the fragment identifier before you start. Another example: there's a popular URI scheme used in the Java world for addressing files within a JAR archive. These URIs attach special meaning to
!
as a separator, and as such they don't follow the syntax in the RFCs. It's convenient for users if these almost-URIs can be resolved in the same way as true URIs, and the spec deliberately leaves enough wriggle-room to permit this.

If the second argument of
resolve-uri()
is omitted, the effect is the same as using the function call
resolve-uri($relative, static-base-uri())
: this means that the base URI is taken from the static context of the XPath expression. The way this is set up is (as the name implies) very context-dependent.

  • In the case of XPath expressions within an XSLT stylesheet the base URI is reasonably well-defined: the base URI of the stylesheet module is used, unless the stylesheet contains
    xml:base
    attributes, or is split into multiple XML external entities. But the base URI of the stylesheet module may be unknown; this might happen if the XSLT code was read from a string constructed in memory rather than from a file.
  • In the case of XPath expressions constructed programmatically, for example, by a Java or JavaScript application, all bets are off. Your XPath API may provide a way of setting the base URI, but it's more likely in my experience that it won't. In this situation relative URI references are rather meaningless, and it's best to avoid them.

Examples

Most of these examples are taken from Appendix C of RFC 2396, and assume a static base URI of
http://a/b/c/d;p?q
. The RFC includes other more complex examples that are worth consulting.

Other books

Julie's Butterfly by Greta Milán
Motocross Me by Cheyanne Young
From the Ashes by Jeremy Burns
Voodoo by Samantha Boyette
The Gingerbread Bump-Off by Livia J. Washburn
The Explorer's Code by Kitty Pilgrim
Hunting for Hidden Gold by Franklin W. Dixon
Chasing Shadows by Valerie Sherrard
The Hunger Pains by Harvard Lampoon
The Carrie Diaries by Candace Bushnell