Read XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition Online
Authors: Michael Kay
The reasoning behind these rules is that names of elements in the stylesheet use the standard XML default namespace
xmlns=“ ”
; names of elements in the source document use the special default
xpath-default-namespace
, and names of objects other than elements never use a default namespace.
A QName with no prefix appearing in an XPath expression never uses the default namespace defined in the source document.
It's a common mistake to forget this. Your source document starts as follows:
and your stylesheet starts:
xmlns=“http://www.w3.org/1999/xhtml”
version=“2.0”>
Why doesn't the template rule for
match=“html”
fire when the
element is encountered? The answer is that the default namespace (declared with
xmlns=“…”
) applies to unprefixed
QName
s in the source document, but it doesn't apply to unprefixed
QName
s appearing in expressions and match patterns in the stylesheet. You either need to write:
xmlns:xhtml=“http://www.w3.org/1999/xhtml”
version=“2.0”>
or you need to define an
xpath-default-namespace
:
xpath-default-namespace=“http://www.w3.org/1999/xhtml”
version=“2.0”>
What's worse, your source document might actually not start with:
but rather with:
Here it's not obvious that the
element is actually in a namespace. But it is, because hidden away inside the DTD is the sneaky little definition:
%i18n;
id ID #IMPLIED
xmlns %URI; #FIXED ‘http://www.w3.org/1999/xhtml’
>
which has the effect of adding the namespace declaration
xmlns=“http://www.w3.org/1999/xhtml”
to the
element whether you asked for it or not. This means that a bare
match=“html”
in your stylesheet won't match this element; you need to match the namespace as well.
Operators
There is no hard-and-fast rule about exactly what constitutes an operator in the XPath language; but this is a good place to provide a general overview of the different kinds of operator.
We can classify as first-order operators all the operators that take one or more expressions as their operands, and produce a result that is obtained by evaluating the operands, and combining the values of the operands in some way. The first-order operators are listed in the table below, in precedence order. Operators listed on the same row of the table have the same precedence.
Operator | Effect |
, | Sequence concatenation |
or | Boolean disjunction (A or B) |
and | Boolean conjunction (A and B) |
eq ne lt le gt ge | Ordering comparison between single values |
= != < <= > >= | Ordering comparison between sequences |
<< is >> | Ordering/identity comparison between nodes |
to | Constructs a sequence of consecutive integers |
+ − | Addition, subtraction |
* div idiv mod | Multiplication, division, modulus |
| union | Union of two sequences considered as sets of nodes |
intersect except | Intersection and difference of sequences considered as sets of nodes |