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

BOOK: XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition
8.69Mb size Format: txt, pdf, ePub
Expression
Result
resolve-uri(“g”)
“http://a/b/c/g”
resolve-uri(“./g”)
“http://a/b/c/g”
resolve-uri(“g/”)
“http://a/b/c/g/”
resolve-uri(“/g”)
“http://a/g”
resolve-uri(“?y”)
“http://a/b/c/?y”
resolve-uri(“g?y”)
“http://a/b/c/g?y”
resolve-uri(“”)
“http://a/b/c/d;p?q”
(but see Note)
resolve-uri(“#s”)
“http://a/b/c/d;p”?q#s”
(but see Note)
resolve-uri(“../g”)
“http://a/b/g”

RFC 2396 is rather coy in its description of how a relative reference of
“”
(the zero-length string) is supposed to behave, giving a description that only really makes sense in the context of a web browser. RFC 3986 clears this up.

Usage

The most likely place you will need to use the
resolve-uri()
function is in conjunction with the
doc()
function, described on page 750. By default, a relative reference passed to the
doc()
function is resolved relative to the base URI from the static context of the XPath expression. If the relative URI was read from a source document, it makes much more sense to resolve it against the base URI of the node that contained it. The code usually looks something like this:

doc(resolve-uri(@href, base-uri(.)))

See Also

base-uri()
on page 719

doc()
on page 750

escape-uri()
on page 811

static-base-uri()
on page 876

reverse

The
reverse()
function returns a sequence in reverse order. For example,
reverse(1 to 5)
returns the sequence
5, 4, 3, 2, 1
.

Signature

Argument
Type
Meaning
sequence
item()*
The input sequence
Result
item()*
A sequence containing the same items as the input sequence, but in reverse order

Effect

The result of the function contains exactly the same items as the input sequence, but the order is reversed. The effect is the same as the expression:

for $i in 1 to count($sequence) return

    $sequence[count($sequence) - $i + 1]

or if you prefer a recursive formulation:

if (empty($sequence))

  then ()

  else (reverse(remove($sequence, 1)), $sequence[1])

Examples

Expression
Result
reverse(1 to 5)
5, 4, 3, 2, 1
reverse(1)
1
reverse(())
()
reverse(ancestor::*)
A list of ancestor elements, in reverse document order (that is, innermost first)

See Also

unordered()
on page 901

root

The
root()
function returns the root node of the tree containing a specified start node, or the root of the tree containing the context node.

Signature

Argument
Type
Meaning
start-node
(optional)
node()?
A node in the tree whose root is required. If the argument is omitted, it defaults to the context node. It is then an error if the context item is not a node (for example, if it is an atomic value, or if it is undefined).
Result
node()?
The root of the tree containing the start node
.

Effect

If the
start-node
argument is supplied and its value is an empty sequence, then the result of the function is an empty sequence.

In other cases, the function returns the root node of the tree containing the
start-node
. The result is the same as the path expression
(ancestor-or-self::node())[1]
. This node is not necessarily a document node, since it is possible in the XPath 2.0 data model to have elements or other nodes that are parentless. The system follows the parent axis until it finds a node that has no parent, and then it returns that node. If the start node has no parent, then the start node itself is returned as the result of the function.

Examples

Expression
Result
root()
The root node of the tree containing the context node
root($x)
The root node of the tree containing the node
$x
$seq/root()
A sequence containing the root nodes of all the trees containing nodes in
$seq
, in document order with duplicates removed

Usage

The effect of the
root()
function, when called with no argument, is very similar to the effect of the expression
/
. However,
/
will return the root node of the tree containing the context node only if the root is a document node; in other cases, it reports a runtime error.

Other books

The Golden Day by Ursula Dubosarsky
Songs for the Missing by Stewart O'Nan
Song of the Sword by Edward Willett
Unplugged by Lois Greiman
Devious Magic by Chafer, Camilla
The Innocents by Francesca Segal
The Summer Prince by Alaya Dawn Johnson