An XPath expression is composed of
steps
separated by
/
. Each step designates an
axis
which can be understood as the direction in which the nodes will be collected from the
current node (called here the starting node) in the document tree. An axis is a
predefined name followed by
::
(Figure 4.1
some examples of axes). The axis specification is followed by a
node test
which is often the name of a node or
*
to indicate any element. The node(s) thus specified can be further restricted with a
predicate expression
within square brackets; this is a boolean expression evaluated at each node, which is
kept in the result sequence if it evaluates to
true
; it can also be a positive integer in which case the sequence only
keeps the node with this index.
Using the instance document shown in Example D.1 (page ), here are a few examples of XPath expressions:
/child::cellar-book/child::cellar/child::wine[1]
designates the node corresponding to the first wine, the one whose code
attribute is
C00043125
.
/descendant::wine[purchaseDate>'2005-01-01']
returns a sequence of nodes designating the wines bought in 2005 or later
(the ones with
code
attributes
C00043215
and
C10263859
).
/descendant::wine/attribute::code
returns the sequence containing the codes of the 4 wines in the cellar.
(/descendant::wine/attribute::code)[4]
returns the fourth wine. Note that sequences in XPath are numbered
starting from 1 and not 0 as is the case in most programming languages.
This notation is very powerful but a bit tedious to write so XPath designers have
defined an abbreviated syntax for the most frequent cases:
child::
can be omitted,
attribute::
can be written as
@
and
descendant::
is written as
//
. This notation is thus similar to what is used in computer systems to
designate files and directories; except that at each step (i.e. between slashes) many
nodes can be selected instead of only one file or directory. The above examples can thus
be
simplified
as
/cellar-book/cellar/wine[1]
//wine[purchaseDate>'2005-01-01']
//wine/@code
(//wine/@code)[4]
Figure 4.1. Some XPath axes
![]() |
Given a document tree rooted at node 1 and a starting node indicated by
node 6 (in grey), the rounded rectangles wrap the sequence of nodes in some
XPath axes. The
attribute::
axis is not shown here because it would be
within
the grey node.
After this quick overview of XPath, we give some more details on the evaluation of its expressions, which is at the root of XSLT. We remind the reader of the seven types of nodes in an XML document:
the starting point of the document
the most common type of node, it may contain other elements
containing the real information; it cannot contain any element
string information contained in the start-tag of the containing element, it is considered a child of the element which contains it
information that is normally ignored for processing but that is nevertheless kept in the structure of the document
elements starting with
<?
that will not be discussed in this document
information about the namespace of an element, its processing will not be discussed in this section.
An XPath expression designating a sequence of nodes in the document tree consists of three parts.
an axis specifier gives the path to a set of nodes. Most of the time, we can use the abbreviated syntax.
a
node test
can be the name of a node (with or without the namespace
prefix),
*
to indicate all elements, or it can be a function name such as
node()
,
text()
or
comment()
to indicate the type of the node that is sought.
a
predicate
is a boolean expression given between square brackets ([]
)
that can further filter the set of nodes identified with the axis specifier and the
node test. If the expression is a number
i, then it refers
to the
ith
child element (numbering starts at 1).
The following grammar gives the rules of an XPath expression as taken from the
XPath standard
[6]. Terminals are shown in
monospace
font. As an extended BNF notation (EBNF) is used, some terminals such as parenthesis,
star, plus or vertical bar are used for grouping, to indicate repetition and
alternation. There are essentially the same symbols as the ones used in DTD or Relax NG
compact notation. When a symbol of the EBNF also occurs as a terminal, it is enclosed
within angle quotes (« »).
Similarly to other
functional
programming languages, XPath defines an expression in terms of a sequence of one or
more expressions [rule 2]. The comma (,
) operator is used to
create a sequence out of two sequences which are
concatenated; sequences of sequences are not allowed.
The building blocks of an XPath expressions are the following:
Numeric and string constants
[rules 41 and 42] their syntax is not defined here as it is conventional; as
there are no boolean constants, they are created using the functions call
true()
and
false()
;
Variable
are referenced using the name of the variable prefixed with
$
[rule 44]. The same syntax is used for the index variable of the
for
construct;
Function calls use the name of the function followed by the value of the actual parameters within parentheses [rule 48]. When a user-defined function is called, its name must start with its namespace prefix;
Range expression [rule 11] creates a sequence of scalar values between their bounds given by two numbers;
Operators [rules 7-24] using the standard arithmetic operations [rules 12,13] and some set-like operations [rules 14,15] on sequences; conditional expressions [rule 7] provide a compact notation for creating new values; on top of the usual comparisons [rule 22] which are applied on the values taking into account their types, there are value comparisons [rule 23] which makes sure that the types are compatible and node comparisons [rule 24] which take into account the document order of the nodes;
Filter expressions [rules 39-40] are predicates, boolean expressions or a single number, enclosed in square brackets that further restrict the values produced by the previous one;
For expressions
[rule 4] return the sequence of results evaluating the body (expression
following
return
) for each item in the subject (expression preceding
return
); an index variable is defined to reference an item of the
subject sequence within the body;
Quantified expressions [rule 6] are a special type of loop for checking if any or all elements of a sequence satisfy a certain predicate.
XPath 2.0 grammar | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|