Read XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition Online
Authors: Michael Kay
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.
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.