Read XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition Online
Authors: Michael Kay
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 @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 Step selects the third |
chapter/section/para/sentence | This path expression selects every / 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 (chapter|appendix) , which selects all the child |
$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. |