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

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

Don't make the mistake of thinking that if
$n
holds the string
“title”
, say, then
./$n
means the same as
./title
. Variables in XPath represent values, not parts of an expression. To select the child elements whose name is in
$n
, use
*[name()=$n]
.

Examples of the Binary
/
Operator

The following examples illustrate that although axis steps are often used as operands of
/
, any kind of expression is legal:

Expression
Description
descendant::para/@style
In this example both operands are axis steps. The first step selects the descendants of the context node that are

elements; the second step uses the abbreviated syntax
@style
, which is short for
attribute::style
, and selects the
style
attributes of these elements.
section[1]/clause[3]
In this example each of the operands includes a positional predicate. The first step selects the first

element that is a child of the context node, the second
Step
selects the third

element that is a child of the selected

.
chapter/section/para/sentence
This path expression selects every

element that is a child of a

element that is a child of a

element that is a child of a

element that is a child of the context node. The expression can be decomposed into a nested set of expressions each of which uses a binary
/
operator:
((chapter/section)/para)/sentence
.
doc(‘a.xml’)/id(‘Z123’)
This example illustrates that the operands of the
/
operator do not have to be
AxisStep
expressions. This example selects the document with a particular relative URI, and using the resulting document node as the context node, then selects the element with a particular ID value.
book/(chapter|appendix)
This is another example that uses an operand that is not an
AxisStep
. For each selected

element, it evaluates the expression
(chapter|appendix)
, which selects all the child

and

elements of the book, in document order.
$chap/title
Using a variable reference on the left-hand side of
/
is very common in
for
expressions, which we will examine in Chapter 10. A typical example is
for $chap in //chapter return string-length($chap/title)
. This kind of construct is even more common in XQuery.

Other books

The Flavours of Love by Dorothy Koomson
Pleasant Vices by Judy Astley
B00VQNYV1Y (R) by Maisey Yates
Genesis by Jim Crace