Beruflich Dokumente
Kultur Dokumente
XPath 1.0
http://www.w3.org/TR/xquery/
Roger L. Costello
6 March 2010
XPath is not a
standalone language
XML
Schemas
XSLT
XPath
XQuery
XPointer
Schematron
A complete list of technologies that use XPath:
http://expath.org/wiki/Engines
Document
/
PI
<?xml version=1.0?>
Text
Jeff
Element
FitnessCenter
Element
Member
Element
Member
Element
Name
<?xml version="1.0"?>
<FitnessCenter>
<Member>
<Name>Jeff</Name>
<FavoriteColor>lightgrey</FavoriteColor>
</Member>
<Member>
<Name>David</Name>
<FavoriteColor>lightblue</FavoriteColor>
</Member>
<Member>
<Name>Roger</Name>
<FavoriteColor>lightyellow</FavoriteColor>
</Member>
</FitnessCenter>
Element
FavoriteColor
Text
lightgrey
Element
Name
Text
David
Element
Member
Element
FavoriteColor
Text
lightblue
Element
Name
Text
Roger
Element
FavoriteColor
Text
lightyellow
Terminology: node
Document node
Element nodes
Document
/
PI
<?xml version=1.0?>
Element
FitnessCenter
Element
Member
Element
Member
Text nodes
Element
Name
Text
Jeff
Element
FavoriteColor
Text
lightgrey
Element
Name
Text
David
Element
Member
Element
FavoriteColor
Text
lightblue
Element
Name
Text
Roger
Element
FavoriteColor
Text
lightyellow
Document
/
PI
<?xml version=1.0?>
With
respect to
this node,
these are
its children
Element
Name
Text
Jeff
Element
FitnessCenter
Element
Member
Element
Member
Element
FavoriteColor
Text
lightgrey
Element
Name
Text
David
Element
Member
Element
FavoriteColor
Text
lightblue
Element
Name
Text
Roger
Element
FavoriteColor
Text
lightyellow
Document
/
PI
<?xml version=1.0?>
These are
its
descendant
nodes
Element
Name
Text
Jeff
Element
FitnessCenter
Element
Member
Element
Member
Element
FavoriteColor
Text
lightgrey
Element
Name
Text
David
Element
Member
Element
FavoriteColor
Text
lightblue
Element
Name
Text
Roger
Element
FavoriteColor
Text
lightyellow
Document
/
PI
<?xml version=1.0?>
This is the
context node
Element
Name
Text
Jeff
Element
FitnessCenter
Element
Member
Element
Member
Element
FavoriteColor
Text
lightgrey
Element
Name
Text
David
Element
Member
Element
FavoriteColor
Text
lightblue
Element
Name
Text
Roger
Element
FavoriteColor
Text
lightyellow
Document
/
PI
<?xml version=1.0?>
Element
Member
Element
Name
Text
Jeff
Element
FitnessCenter
That's
its parent
Element
FavoriteColor
Text
lightgrey
Element
Member
Element
Name
Text
David
Element
Member
Element
FavoriteColor
Text
lightblue
Element
Name
Text
Roger
Element
FavoriteColor
Text
lightyellow
Document
/
PI
<?xml version=1.0?>
Element
Member
Element
Name
Text
Jeff
Element
FitnessCenter
Those are
its
ancestors
Element
FavoriteColor
Text
lightgrey
Element
Member
Element
Name
Text
David
Element
Member
Element
FavoriteColor
Text
lightblue
Element
Name
Text
Roger
Element
FavoriteColor
Text
lightyellow
10
Document
/
PI
<?xml version=1.0?>
Element
Member
Element
Name
Text
Jeff
Element
FitnessCenter
It has 2
siblings
Element
FavoriteColor
Text
lightgrey
Element
Member
Element
Name
Text
David
Element
Member
Element
FavoriteColor
Text
lightblue
Element
Name
Text
Roger
Element
FavoriteColor
Text
lightyellow
11
Document
/
PI
<?xml version=1.0?>
Element
Member
Element
Name
Text
Jeff
Element
FitnessCenter
They are
followingsiblings
Element
FavoriteColor
Text
lightgrey
Element
Member
Element
Name
Text
David
Element
Member
Element
FavoriteColor
Text
lightblue
Element
Name
Text
Roger
Element
FavoriteColor
Text
lightyellow
12
Document
/
PI
<?xml version=1.0?>
Element
Member
Element
Name
Text
Jeff
Element
FitnessCenter
It has no
precedingsiblings
Element
FavoriteColor
Text
lightgrey
Element
Member
Element
Name
Text
David
Element
Member
Element
FavoriteColor
Text
lightblue
Element
Name
Text
Roger
Element
FavoriteColor
Text
lightyellow
13
<?xml version="1.0"?>
<Document classification="secret">
<Para classification="unclassified">
One if by land, two if by sea;
</Para>
<Para classification="confidential">
And I on the opposite shore will be,
Ready to ride and spread the alarm
</Para>
<Para classification="unclassified">
Ready to ride and spread the alarm
Through every Middlesex, village and farm,
</Para>
</Document>
14
Document
/
PI
<?xml version=1.0?>
Element
Para
Text
One if
Element
Document
Attribute
classification=unclassified
Element
Para
Text
And I
Attribute
classification=confidential
Attribute
classification=secret
Element
Para
Text
Ready to
Attribute
classification=
unclassified
15
16
17
18
/Document/Para
19
/Document/Para
Hey XPath processor, position yourself at the Document node (i.e., /); use that as the
context node; from there select any child elements with the name Document (theres
only one); now use that as the context node and from there select any child elements
with the name Para (there are four, although only three are shown below due to space
limitations). Show the selected set of nodes.
Document
/
PI
<?xml version=1.0?>
Element
Para
Text
One if
Element
Document
Attribute
classification=unclassified
Element
Para
Text
And I
Attribute
classification=confidential
Attribute
classification=secret
Element
Para
Text
Ready to
Attribute
classification=
unclassified
20
Result
<Para classification="unclassified">
One if by land, two if by sea;
</Para>
<Para classification="confidential">
And I on the opposite shore will be,
Ready to ride and spread the alarm
</Para>
<Para classification="unclassified">
Ready to ride and spread the alarm
Through every Middlesex, village and farm,
</Para>
<Para classification="secret">
For the country folk to be up and to arm.
</Para>
Evaluating the XPath query results in selecting each Para
element, including its attribute and contents.
21
Please understand what this is saying: the first Para element (Para[1]) and its
content is selected, the second Para element and its content is selected, the third
Para and its content is selected, and the fourth Para element and its content is
selected.
22
/Document/Para
This is an absolute XPath expression. i.e., the
selection of nodes starts from the top of the
document (tree).
23
Not XML!
XPath syntax is not XML syntax!
24
25
26
Result
<Para classification="unclassified">
One if by land, two if by sea;
</Para>
<Para classification="confidential">
And I on the opposite shore will be,
Ready to ride and spread the alarm
</Para>
<Para classification="unclassified">
Ready to ride and spread the alarm
Through every Middlesex, village and farm,
</Para>
<Para classification="secret">
For the country folk to be up and to arm.
</Para>
Same result as before.
27
Select here
28
Result
<Document classification="secret">
<Para classification="unclassified">
One if by land, two if by sea;
</Para>
<Para classification="confidential">
And I on the opposite shore will be,
Ready to ride and spread the alarm
</Para>
<Para classification="unclassified">
Ready to ride and spread the alarm
Through every Middlesex, village and farm,
</Para>
<Para classification="secret">
For the country folk to be up and to arm.
</Para>
</Document>
The context node is selected, which is Document.
Thus the result is Document and its content.
29
//Para
The double slash means descendents.
30
Result
<Para classification="unclassified">
One if by land, two if by sea;
</Para>
<Para classification="confidential">
And I on the opposite shore will be,
Ready to ride and spread the alarm
</Para>
<Para classification="unclassified">
Ready to ride and spread the alarm
Through every Middlesex, village and farm,
</Para>
<Para classification="secret">
For the country folk to be up and to arm.
</Para>
All Para elements that descend from / are selected.
31
//Para[1]
32
Result
<Para classification="unclassified">
One if by land, two if by sea;
</Para>
33
//Para[last()]
34
Result
<Para classification="secret">
For the country folk to be up and to arm.
</Para>
35
36
Result
unclassified
37
38
Result
false
39
Comparison Operators
=
<
>
!=
<=
>=
Remember to escape the reserved characters:
Instead of < use <
Instead of > use >
Instead of <= use <=
Instead of >= use >=
40
41
Result
true
42
Boolean Operators
A or B
A and B
not(A)
43
44
Result
<Para classification="secret">
For the country folk to be up and to arm.
</Para>
45
not(//Para[@classification = 'top-secret'])
46
Result
true
47
//Para[@classification != 'top-secret']
48
Result
<Para classification="unclassified">
One if by land, two if by sea;
</Para>
<Para classification="confidential">
And I on the opposite shore will be,
Ready to ride and spread the alarm
</Para>
<Para classification="unclassified">
Ready to ride and spread the alarm
Through every Middlesex, village and farm,
</Para>
<Para classification="secret">
For the country folk to be up and to arm.
</Para>
None of the Para elements have a top-secret classification.
Do Lab1
49
50
following-sibling::*
51
Result
<Para classification="unclassified">
Ready to ride and spread the alarm
Through every Middlesex, village and farm,
</Para>
<Para classification="secret">
For the country folk to be up and to arm.
</Para>
These Para elements are siblings (of the second Para element)
and they follow, i.e., they are following siblings.
52
following-sibling::*[1]
53
Result
<Para classification="unclassified">
Ready to ride and spread the alarm
Through every Middlesex, village and farm,
</Para>
54
55
56
following-sibling::Para
57
Result
<Para classification="unclassified">
Ready to ride and spread the alarm
Through every Middlesex, village and farm,
</Para>
<Para classification="secret">
For the country folk to be up and to arm.
</Para>
Although the Test element is a following sibling of Para[2], it is
not selected because the XPath expression selects only the Para
following siblings.
58
following-sibling::*
59
Result
<Para classification="unclassified">
Ready to ride and spread the alarm
Through every Middlesex, village and farm,
</Para>
<Para classification="secret">
For the country folk to be up and to arm.
</Para>
<Test>Hello World</Test>
The * symbol matches (selects) any element node.
60
following-sibling::Para[1]
61
Result
<Para classification="unclassified">
Ready to ride and spread the alarm
Through every Middlesex, village and farm,
</Para>
62
preceding-sibling::*
63
Result
<Para classification="unclassified">
One if by land, two if by sea;
</Para>
Para[2] has only one preceding sibling element node.
64
65
Equivalent!
Para[1]
child::Para[1]
66
67
../@classification
68
Equivalent!
../@classification
parent::*/@classification
69
70
List of Axis
71
72
Document
/
PI
<?xml version=1.0?>
Element
FitnessCenter
ancestors
Element
Name
Element
Phone
preceding
sibling
Text
Jeff
Element
Member
Element
Phone
Element
FavoriteColor
Text
555-4321
Text
lightgrey
following
siblings
Text
555-1234
73
Document
/
PI
<?xml version=1.0?>
descendants
Element
FitnessCenter
Element
Member
Element
Name
Text
Jeff
Element
Phone
Element
Phone
Element
FavoriteColor
Text
555-1234
Text
555-4321
Text
lightgrey
74
Using an Axis
axis::node
Either the name of an element or the * symbol
Examples: ancestor::*
child::Para
75
76
Common Error
Distinguish between these two:
child::Para
child:Para
The first one has two colons, the second has only
one.
When specifying an axis you must follow it with
two colons. (Otherwise the XPath processor will
think that youre using a namespace!)
77
Default Axis
Para[1]
child::Para[1]
child is the default axis, i.e., if an XPath expression doesnt
specify an axis (see the first expression above) then, by default,
the child axis is used.
Do Lab2
78
XPath Functions
Thus far we have been creating XPath
expressions that select nodes and compare
nodes in the XML document.
The following slides show XPath
expressions that operate on nodes in the
XML document. This is accomplished using
XPath functions.
79
Result:
4
80
Result:
1
81
82
contains(//Para[1], 'SCRIPT')
Result:
true
83
6 Kinds of Nodes
An XML document contains these kinds of
nodes:
Element nodes
Attribute nodes
Text nodes
Document node
Comment nodes
Processing instruction nodes
84
node() vs *
As weve seen, the * symbol matches
(selects) element nodes.
The node() function matches (selects) any
kind of node.
85
//node()[contains(., 'SCRIPT')]
86
PI
<?xml version=1.0?>
Element
Para
Text
One if
SCRIPT
Attribute
classification=unclassified
Element
Para
Text
And I
Element
Document
Attribute
classification=confidential
Attribute
classification=secret
Element
Para
Text
Ready to
Attribute
classification=
unclassified
87
Result
<Document classification="secret">
<Para classification="unclassified">
One if by land, two if by sea;SCRIPT
</Para>
<Para classification="confidential">
And I on the opposite shore will be,
Ready to ride and spread the alarm
</Para>
<Para classification="unclassified">
Ready to ride and spread the alarm
Through every Middlesex, village and farm,
</Para>
<Para classification="secret">
For the country folk to be up and to arm.
</Para>
</Document>
<Para classification="unclassified">
One if by land, two if by sea;SCRIPT
</Para>
One if by land, two if by sea;SCRIPT
88
count(//node()[contains(., 'SCRIPT')])
Result:
3
89
Result:
cr
One if by
10 spaces
carriage return
90
91
string-length(//Para[1])
Result:
52
92
name(/Document/*[last()])
Result:
Test
93
Convert Documents
classification to uppercase
Result:
SECRET
94
95
96
Multiply Cost by 2
Cost * 2
Result:
24
97
Cost mod 2
Result:
0
98
Arithmetic Operators
*
mod
- (must leave space on either side)
+
div
Do Lab3
99
100
Establish FitnessCenter
as the Context Node
101
102
Context Item
Consider this path expression: Member/Name, and this
predicate expression: Member[Name].
In these expressions Name is evaluated once for each item
in the sequence that results from evaluating Member.
<Member>
<Name>Jeff</Name>
<FavoriteColor>lightgrey</FavoriteColor>
</Member>
<Member>
<Name>David</Name>
<FavoriteColor>lightblue</FavoriteColor>
</Member>
<Member>
<Name>Roger</Name>
<FavoriteColor>lightyellow</FavoriteColor>
</Member>
103
Context Item
Evaluating the expression "Member" produces this
sequence of element nodes:
<Member>
<Name>Jeff</Name>
<FavoriteColor>lightgrey</FavoriteColor>
</Member>
<Member>
<Name>David</Name>
<FavoriteColor>lightblue</FavoriteColor>
</Member>
<Member>
<Name>Roger</Name>
<FavoriteColor>lightyellow</FavoriteColor>
</Member>
104
Meaning of "/"
Consider this path expression: Member/Name
The "/" means: "for each Member node, return the result of
evaluating Name".
<Member>
<Name>Jeff</Name>
<FavoriteColor>lightgrey</FavoriteColor>
</Member>
<Member>
<Name>David</Name>
<FavoriteColor>lightblue</FavoriteColor>
</Member>
<Member>
<Name>Roger</Name>
<FavoriteColor>lightyellow</FavoriteColor>
</Member>
Member/Name
<Name>Jeff</Name>
<Name>David</Name>
<Name>Roger</Name>
105
106
Meaning of []
Consider this predicate expression: Member[Name]
The predicate [Name] means: evaluate the expression "Name"
within the context node. If Name exists then return true, else false.
Thus, Member[Name] says: "give me all Member nodes that has a
Name child".
<Member>
<Name>Jeff</Name>
<FavoriteColor>lightgrey</FavoriteColor>
</Member>
<Member>
<Name>David</Name>
<FavoriteColor>lightblue</FavoriteColor>
</Member>
<Member>
<Name>Roger</Name>
<FavoriteColor>lightyellow</FavoriteColor>
</Member>
<Member>
<Name>Jeff</Name>
<FavoriteColor>lightgrey</FavoriteColor>
</Member>
Member[Name]
<Member>
<Name>David/Name>
<FavoriteColor>lighblue</FavoriteColor>
</Member>
<Member>
<Name>Roger</Name>
<FavoriteColor>lightyellow</FavoriteColor>
</Member>
Meaning of [] (concluded)
Here is the general form for using []
expression1[expression2]
This expression
must evaluate to
a boolean value
(true or false).
Consequently, all of these are legal:
Member[true()]
These will select all Member elements
Member[false()]
These will select no Member elements
Member[1 = 1]
Member[1 = 2]
This will select only the Member elements that
Member[Age]
This expression
must evaluate to
a node or node sequence.
107
108
Meaning of "|"
Consider this union expression: expression1|expression2
The union expression means, "create a sequence of nodes composed of the nodes
selected by expression1, unioned with the nodes selected by expression2.
<Member>
<Name>Jeff</Name>
<FavoriteColor>lightgrey</FavoriteColor>
</Member>
<Member>
<FavoriteColor>lightblue</FavoriteColor>
</Member>
<Member>
<Name>Roger</Name>
<FavoriteColor>lightyellow</FavoriteColor>
</Member>
Member[1]|Member[3]
<Member>
<Name>Jeff</Name>
<FavoriteColor>lightgrey</FavoriteColor>
</Member>
<Member>
<Name>Roger</Name>
<FavoriteColor>lightyellow</FavoriteColor>
</Member>
109
Context Position
Recall that an XPath expression returns a
sequence of items.
Consider one of those items in the
sequence. Its context position is the
position of the item in the sequence.
110
Result:
<Member>
<Name>David/Name>
<FavoriteColor>lighblue</FavoriteColor>
</Member>
111
Equivalent!
Member[position() = 2]
Member[2]
112
Context Size
Recall that an expression returns a sequence
of items.
The context size tells you the number of
items in the sequence.
113
Result:
<Member>
<Name>Roger</Name>
<FavoriteColor>lightyellow</FavoriteColor>
</Member>
114
Axis
There are many ways that you might want to navigate
through an XML tree. For example, from a context node
you might want to:
115
Categories of Axes
These are axes used for navigating forward in the XML
tree:
child
descendant
attribute
self
descendant-or-self
following-sibling
following
parent
preceding-sibling
preceding
ancestor
ancestor-or-self
116
Child Axis
The child axis is used to navigate to a child
node from the context node.
./child::Member[1]/child::Name
117
Document
/
PI
<?xml version=1.0?>
Element
FitnessCenter
child::Member[1]
Element
Member
Element
Member
Element
Member
child::Name
Element
Name
Text
Jeff
Element
FavoriteColor
Text
lightgrey
Element
Name
Text
David
Element
FavoriteColor
Text
lightblue
Element
Name
Text
Roger
Element
FavoriteColor
Text
lightyellow
118
119
120
Descendant Axis
The descendant axis is used to navigate to a
node that is "under" the context node. The
node could be a child node, a grandchild, a
great-grandchild, etc.
./descendant::Name[1]
121
122
123
Attribute Axis
The attribute axis is used to navigate to an
attribute of the context node.
./child::Member[1]/attribute::level
124
Self Axis
The self axis is used to select the current
node (i.e., here).
./child::*[not(self::Member)]/child::Name
125
Incorrect
./child::*[not(Member)]/child::Name
126
Equivalent!
./*[not(Member)]/Name
./child::*[not(child::Member)]/child::Name
127
Descendant-or-self Axis
This axis is used to navigate to the current
node and all its descendants.
descendant-or-self::*
128
Following-sibling Axis
This axis is used to navigate to a node that is "parallel" to the context
node (i.e., at the same hierarchy level) and follows the context node.
<Member>
<Name>Jeff</Name>
<FavoriteColor>lightgrey</FavoriteColor>
</Member>
<Member>
<Name>David</Name>
<FavoriteColor>lightblue</FavoriteColor>
</Member>
<Member>
<Name>Roger</Name>
<FavoriteColor>lightyellow</FavoriteColor>
</Member>
<Employee>
<Name>Erika</Name>
</Employee>
<Employee>
<Name>Linda</Name>
</Employee>
<CEO>
<Name>Jack</Name>
</CEO>
<VP>
<Name>Dora</Name>
</VP>
./child::Member[1]/following-sibling::Employee[2]
129
Document
/
PI
<?xml version=1.0?>
Element
FitnessCenter
Element
Member
Element
Member
Element
Member
Element
FavoriteColor
Element
Name
Element
FavoriteColor
Element
Name
Element
FavoriteColor
following-sibling
Text
Jeff
Text
lightgrey
Text
David
Text
lightblue
Text
Roger
Text
lightyellow
Note: a node is a following sibling of another node only if it has the same parent.
Consquently, the Name and FavoriteColor nodes in Member[2] and Member[3]
are not following-siblings of the Name in Member[1].
130
Following Axis
This axis is used to navigate to the nodes
that follow the context node.
./child::Member[1]/following::Name
131
Document
/
PI
<?xml version=1.0?>
child::Member[1]
Element
Member
Element
Member
Element
Name
Text
Jeff
Element
FitnessCenter
Element
FavoriteColor
Text
lightgrey
Element
Name
Text
David
Element
Member
Element
FavoriteColor
Text
lightblue
Element
Name
Text
Roger
Element
FavoriteColor
Text
lightyellow
following nodes
132
Document
/
PI
<?xml version=1.0?>
Element
A
Context node
Element
E
Element
D
Text
Element
C
Element
B
Element
F
Text
Element
H
Element
G
Text
Text
Element
I
Text
Element
J
Text
following nodes
133
<?xml version="1.0"?>
<A>
<B>
<D>...</D>
<E>
<F></F>
<G></G>
</E>
<H>...</H>
</B>
<C>
<I></I>
<J></J>
</C>
</A>
following::E
134
Parent Axis
This axis is used to navigate to the parent of
the context node.
Suppose that Member[1] is the context node
./parent::FitnessCenter
Starting from the context node, navigate to
the parent, provided it is <FitnessCenter>.
Suppose that we wanted to navigate to the
parent, whatever it is. We would use this: parent::*.
Note: if the context node is an attribute node, then its
parent is the element node that the attribute is attached to.
135
Preceding-sibling axis
This axis is used to navigate to a node that is "parallel" to
the context node (i.e., at the same hierarchy level) and
precedes the context node.
<Member>
<Name>Jeff</Name>
<FavoriteColor>lightgrey</FavoriteColor>
</Member>
<Member>
<Name>David</Name>
<FavoriteColor>lightblue</FavoriteColor>
</Member>
<Member>
<Name>Roger</Name>
<FavoriteColor>lightyellow</FavoriteColor>
</Member>
<Employee>
<Name>Erika</Name>
</Employee>
<Employee>
<Name>Linda</Name>
</Employee>
<CEO>
<Name>Jack</Name>
</CEO>
<VP>
<Name>Dora</Name>
</VP>
./child::*[last()]/preceding-sibling::Member[1]
136
Preceding Axis
This axis is used to navigate to all the nodes
that come before the context node.
<Member level="platinum">
<Name>Jeff</Name>
<FavoriteColor>lightgrey</FavoriteColor>
</Member>
<Member level="gold">
<Name>David</Name>
<FavoriteColor>lightblue</FavoriteColor>
</Member>
<Member level="silver">
<Name>Roger</Name>
<FavoriteColor>lightyellow</FavoriteColor>
</Member>
./child::Member[last()]/preceding::Name
137
<?xml version="1.0"?>
<Book>
<Title>Origin of Wealth</Title>
<Chapter>
<Title>The Paradigm Shift</Title>
<Section>
<Title>The Question</Title>
<Para>I sat perched on </Para>
</Section>
</Chapter>
</Book>
This is Book.xml in the example000 folder
138
Ancestor Axis
This axis is used to navigate to an ancestor
node.
ancestor::*[2]/child::Title
139
Ancestor-or-self Axis
This axis is used to navigate to the current
node and its ancestors.
ancestor-or-self::*
Element
Member
Text
"cr
"
Element
Name
Text
Jeff
Text
"cr
"
Element
FavoriteColor
Text
"cr"
Text
lightgrey
Suppose that the context node is the Member element. Then this expression: text()
will result in selecting the 3 child text nodes.
140
141
Document
/
PI
<?xml version=1.0?>
Element
FitnessCenter
Element
Member
Element
Member
Element
Name
Text
Jeff
Element
FavoriteColor
Text
lightgrey
Element
Name
Text
David
/FitnessCenter/Member[1]/Name selects
Element
Member
Element
FavoriteColor
Text
lightblue
Element
Name
Text
Roger
Element
FavoriteColor
Text
lightyellow
142
Document
/
PI
<?xml version=1.0?>
Element
FitnessCenter
Element
Member
Element
Member
Element
Name
Text
Jeff
Element
FavoriteColor
Text
lightgrey
Element
Name
Text
David
/FitnessCenter/Member[1]/Name/text() selects
Element
Member
Element
FavoriteColor
Text
lightblue
Element
Name
Text
Roger
Element
FavoriteColor
Text
lightyellow
143
Element
Member
Text
"cr
"
Element
Name
Text
"cr
"
Comment
This is a comment
Text
"cr
"
PI
foo="blah"
Text
Jeff
Suppose that the context node is the Member element. Then this expression: node() will result
in selecting the 4 child text nodes, the 1 element node, the 1 comment node, and the 1 PI node.
Text
"cr"
144
Examples
child::para selects the para element children of the context node.
child::* selects all the element children of the context node.
child::text() selects all text node children of the context node.
child::node() selects all the children of the context node, whatever their node type (i.e., text node, element
node, document (/) node, comment node, or processing instruction node)
attribute::name selects the name attribute of the context node.
attribute::* selects all the attributes of the context node.
parent::node() selects the parent of the context node. If the context node is an attribute node, this expression
returns the element node to which the attribute is attached.
descendant::para selects all the para element descendants of the context node.
ancestor::div selects all div ancestors of the context node.
ancestor-or-self::div selects the div ancestors of the context node and, if the context node is a div element, the
context node as well.
descendant-or-self::para selects the para element descendants of the context node and, if the context node
is a para element, the context node as well.
self::para selects the context node if it is a para element, and otherwise nothing.
145
Expanded Notation
The expanded notation has several advantages:
Nonambiguous: it makes it very clear how you are
navigating through the XML tree.
Powerful: it provides the means to navigate the XML
tree in pretty much every way that you might desire.
146
Abbreviated Notation
We will now look at the abbreviated notation.
Here are its advantages:
Brevity: the XPath expressions are much shorter.
147
148
Default Axis
The default axis is child. Thus, these forms
are equivalent:
./child::*[not(Member)]/child::Name
./child::*[not(child::Member)]/child::Name
is equivalent to:
child::Member[child::Name]
149
./child::*[not(child::Member)]/child::Name
expanded notation
./Member[1]/Name
abbreviated notation
./*[not(Member)]/Name
abbreviated notation
150
./Member[1]/@level
abbreviated notation
Do Lab5
151
<?xml version="1.0"?>
<Book>
<para>paragraph #1</para>
<para>paragraph #2</para>
<para>paragraph #3</para>
<Chapter>
<para>paragraph #4</para>
<para>paragraph #5</para>
<Section>
<para>paragraph #6</para>
</Section>
</Chapter>
</Book>
This is Book_v2.xml in the example000 folder
152
./descendant-or-self::node()/child::para
expanded notation
These expressions both say: starting from the context node select all
the descendant nodes as well as the context node. Now, for each of
them select the child para elements. In other words, this results in
selecting all para elements that descend from the context node.
153
./descendant-or-self::node()/child::para
Result:
<para>paragraph #1</para>
<para>paragraph #2</para>
<para>paragraph #3</para>
<para>paragraph #4</para>
<para>paragraph #5</para>
<para>paragraph #6</para>
154
.//para[1]
Result:
<para>paragraph #1</para>
<para>paragraph #4</para>
<para>paragraph #6</para>
155
is equivalent to:
descendant-or-self::node()/child::para[1]
156
./descendant::para[1]
Result:
<para>paragraph #1</para>
157
158
Lesson Learned
.//para[1]
./descendant::para[1]
Not equivalent!
Do Lab6
159
.. is an abbreviation of
parent::node()
..
abbreviated notation
../para
abbreviated notation
parent::node()
expanded notation
parent::node()/child::para
expanded notation
160
. is an abbreviation of self::node()
.
abbreviated notation
self::node()
expanded notation
161
Good Example
<Member level="platinum">
<Name>Jeff</Name>
<FavoriteColor>lightgrey</FavoriteColor>
</Member>
<Member level="gold">
<Name>David</Name>
<FavoriteColor>lightblue</FavoriteColor>
</Member>
<Member level="platinum">
<Name>Roger</Name>
<FavoriteColor>lightyellow</FavoriteColor>
</Member>
Member[FavoriteColor='lightblue']
<Member level="gold">
<Name>David</Name>
<FavoriteColor>lightblue</FavoriteColor>
</Member>
162
child::Member[child::FavoriteColor='lightblue']
163
Examples
para selects the para element children of the context node.
* selects all element children of the context node.
text() selects all text node children of the context node.
@name selects the name attribute of the context node.
@* selects all the attributes of the context node.
para[1] selects the first para child of the context node.
para[last()] selects the last para child of the context node.
*/para selects all para grandchildren of the context node.
/book/chapter[5]/section[2] selects the second section of the fifth chapter of the book whose parent is the document node.
chapter//para selects the para element descendants of all the chapter element children of the context node.
//para selects all the para element descendants in the document
//@version selects all the version attribute nodes in the document
//list/member selects all the child member elements of all the list elements.
.//para selects all para element descendants of the context node.
.. selects the parent of the context node.
../@lang selects the lang attribute of the parent of the context node.
para[@type='warning'] selects all para children of the context node that have a type attribute with value warning.
chapter[title='Introduction'] selects the chapter children of the context node that have one or more title children
whose value equals Introduction.
chapter[title] selects the chapter children of the context node that have one or more title children.
employee[@secretary and @assistant] selects all the employee children of the context node that have both a secretary
attribute and an assistant attribute.
Member[Name='Jeff' or Name='Roger']/FavoriteColor selects all FavoriteColor children of Member elements that
have a Name child element equal to Jeff or Roger.
164
Summary
Expanded Notation
Abbreviated Notation
child::para
para
descendant::para
attribute::level
@level
self::node()
.
descendant-or-self::node()/child::para .//para
following-sibling::para
following::name
parent::node()
..
preceding-sibling::Member
preceding::Name
ancestor::FitnessCenter
ancestor-or-self::Title
165
Predicates
A predicate consists of an expression, called a
predicate expression, enclosed in square
brackets.
A predicate serves to filter a sequence-retaining some items and discarding others.
Those items for which the predicate evaluates
to true are retained, and those for which the
predicate evaluates to false are discarded.
166
Predicates
path-expression[predicate-expression]
The predicate-expression
is evaluated within the context
provided by the path-expression.
The result of evaluating the
predicate expression is a boolean value:
true or false.
167
Numeric Predicates
If the predicate expression is a number, then the predicate
evaluates to true if the number equals the context position.
Consider this XPath: Member[2]
The path expression yields this sequence:
<Member>
<Name>Jeff</Name>
<FavoriteColor>lightgrey</FavoriteColor>
</Member>
<Member>
<Name>David</Name>
<FavoriteColor>lightblue</FavoriteColor>
</Member>
<Member>
<Name>Roger</Name>
<FavoriteColor>lightyellow</FavoriteColor>
</Member>
The predicate is evaluated against each of these items. Since the predicate is a number it will be
compared against each item's context position. For the first item its context position is 1 and 1 = 2
so the first item is discarded. For the second item its context position is 2 and 2 = 2 so the second item
is retained. For the third item its context position is 3 and 3 = 2 so the third item is discarded.
168
Element Predicates
If the predicate expression is an element node, then the predicate evaluates to
true if the node exists. (Conversely, a predicate expression which evaluates to
an empty sequence evaluates to false)
Consider this predicate expression: Member[FavoriteColor]
Suppose the Member path expression yields this sequence:
<Member>
<Name>Jeff</Name>
<FavoriteColor>lightgrey</FavoriteColor>
</Member>
<Member>
<Name>David</Name>
</Member>
<Member>
<Name>Roger</Name>
<FavoriteColor>lightyellow</FavoriteColor>
</Member>
Note that the second Member does not have a FavoriteColor. The predicate is evaluated against
each of these items. Since the predicate is an element node the XSLT processor will determine if
the node exists for each item. For the first Member there does exist a FavoriteColor child element so
it is retained. For the second Member there does not exist a FavoriteColor child element so it is
discarded. For the third Member there does exist a FavoriteColor child element so it is retained.
169
Boolean Predicates
If the predicate expression is a boolean expression,
then the predicate evaluates to true if the boolean
expression evaluates to true.
Consider this XPath: Member[Name = 'Jeff']
Suppose the Member path expression yields this
sequence:
<Member>
<Name>Jeff</Name>
<FavoriteColor>lightgrey</FavoriteColor>
</Member>
<Member>
<Name>David</Name>
<FavoriteColor>lightblue</FavoriteColor>
</Member>
<Member>
<Name>Roger</Name>
<FavoriteColor>lightyellow</FavoriteColor>
</Member>
The predicate is evaluated against each of these items. The first Member has a child Name element
with a value equal to Jeff, so it is retained. The second and third Member elements have a child Name
element, but with a value other than Jeff, so they are discarded.
170
Multiple Predicates
It is legal to have multiple predicates:
path-expression[predicate-expression1] [predicate-expression2]
171
Order Matters!
<?xml version="1.0"?>
<FitnessCenter>
<Member level="platinum">
<Name>Jeff</Name>
<FavoriteColor>lightgrey</FavoriteColor>
</Member>
<Member level="gold">
<Name>David</Name>
<FavoriteColor>lightblue</FavoriteColor>
</Member>
<Member level="platinum">
<Name>Roger</Name>
<FavoriteColor>lightyellow</FavoriteColor>
</Member>
</FitnessCenter>
Member[@level='platinum'][2]
<Member level="platinum">
<Name>Roger</Name>
<FavoriteColor>lightyellow</FavoriteColor>
</Member>
Member[2] [@level='platinum']
-- empty --
Do Lab7
172
Arithmetic Expressions
XPath provides these arithmetic operators:
+ (addition)
- (subtraction and unary negation)
* (multiplication)
div (division)
mod (modulus, i.e., the remainder from division)
Example: child::Cost * 2 (multiply the value of the Cost child
element by 2)
Example: position() mod 2 = 0 (this expression will evaluate to
true for context items at even positions, and false otherwise)
173
Boolean Operators
XPath provides these boolean operators:
and
or
not
Example: child::Cost and child::Member (This expression will
evaluate to true only if there exists a Cost child element
and a Member child element)
Example: ((child::Cost * 2 = 50) and (child::Member))
174
Parenthesized Expressions
Use parentheses to ensure that your
expressions are evaluated in the order you
desire.
Example: (child::Cost and child::Member) or (child::Cost and child::Test)
This expression will evaluate to true if there exists a Cost child
element and a Member child element, or there exists a Cost child
element and a Test child element.
175
Comparison Operators
These general comparison operators are
available:
= | != | < | <= | > | >=
176
<?xml version="1.0"?>
<FitnessCenter>
<Member>
<Name>Jeff</Name>
<Age>39</Age>
</Member>
<Member>
<Name>David</Name>
<Age>33</Age>
</Member>
<Member>
<Name>Roger</Name>
<Age>36</Age>
</Member>
</FitnessCenter>
This is FitnessCenter_v4.xml in the example000 folder.
177
General Comparison
Note that I am not specifying a specific Member.
Consequently, I am comparing all Member Ages.
<?xml version="1.0"?>
<FitnessCenter>
<Member>
<Name>Jeff</Name>
<Age>39</Age>
</Member>
<Member>
<Name>David</Name>
<Age>33</Age>
</Member>
<Member>
<Name>Roger</Name>
<Age>36</Age>
</Member>
</FitnessCenter>
Member/Age = 39
Member/Age != 20
Member/Age < 40
Member/Age <= 40
Member/Age > 30
Member/Age >= 30
Result:
Member/Age = 39 true
Member/Age != 20 true
Member/Age < 40 true
Member/Age <= 40 true
Member/Age > 30 true
Member/Age >= 30 true
178
General Comparison
<?xml version="1.0"?>
<FitnessCenter>
<Member id="1" level="platinum">
<Name>Jeff</Name>
<Phone type="home">555-1234</Phone>
<Phone type="work">555-4321</Phone>
<FavoriteColor>lightgrey</FavoriteColor>
<MembershipFee>340</MembershipFee>
<Age>39</Age>
</Member>
<Member id="2" level="gold">
<Name>David</Name>
<Phone type="home">383-1234</Phone>
<Phone type="work">383-4321</Phone>
<FavoriteColor>lightblue</FavoriteColor>
<MembershipFee>500</MembershipFee>
<Age>33</Age>
</Member>
<Member id="3" level="platinum">
<Name>Roger</Name>
<Phone type="home">888-1234</Phone>
<Phone type="work">888-4321</Phone>
<FavoriteColor>lightyellow</FavoriteColor>
<MembershipFee>340</MembershipFee>
<Age>36</Age>
</Member>
</FitnessCenter>
Member/Age = 39?
39
179
Member/Age = Member/Salary
Result:
Member/Age = Member/Salary true
180
All Combinations of
Sequence Values are Compared
Age
39
33
36
Salary
20
39
45
181
XPath Functions
XPath provides a lot of functions for you to use. We have
already seen some, such as position(), last(), text(), node(),
count(), and translate().
Here's how the XPath functions are categorized:
String functions: those functions which when evaluated return a string.
Boolean functions: those functions which when evaluated return a
boolean value (true or false).
Number functions: those functions which when evaluated return a
number.
Node functions: those functions which when evaluated return a node.
Namespace functions: those functions that are intended for use with
namespaces.
182
Notation
Some functions have optional arguments. For
example, the name function has an optional
argument. I will use "?" after the argument to
indicate that it is optional, e.g., name(node?).
Some functions allow zero or more occurrences of
an argument. For example, the concat function
allows zero or more strings after the first two
arguments. I will use "*" after the argument to
indicate zero or more occurrences, e.g.,
concat(string, string, string*)
183
String Functions
name(node?) - this function returns the name of a node. If
no argument is provided then it returns the name of the
context item.
string(object?) - If object is a node this function returns the
value of the node (if the node is not a leaf node then it
concatenates the values of all the leaf nodes that are under
it)
concat(string, string, string*) - this function concatenates
all its arguments. There must be at least two arguments.
Example: concat(Name; , child::Name)
184
185
186
187
188
Boolean Functions
starts-with(string1, string2) - this function returns true if
string1 starts with string2, and otherwise returns false.
contains(string1, string2) - this function returns true if
string1 contains string2, and otherwise returns false.
Example: starts-with(child::Phone, '555')
Example: contains(child::FavoriteColor, 'blue')
189
190
Number Functions
position() - this function returns an integer, representing the position of the
context item within a sequence of items.
last() - this function returns an integer, representing the context size.
count(node sequence) - this function returns an integer representing the
number of nodes in node sequence.
string-length(string?) - this function returns an integer representing the
number of characters in string. If the argument is omitted, it defaults to the
context node converted to a string.
Example: string-length(child::FavoriteColor)
191
192
193
Node Functions
text() - this function matches on any text node.
node() - this function matches on any text, element,
document, comment, or PI node.
id(node id) - this function returns the node with an ID
value equal to node id.
Example: id(child::GuestAuthor/BookForSigning/@isbn-ref)
will return the element which has an ID value that
matches isbn-ref.
194
Establish this
as the context
node
<?xml version="1.0"?>
<FitnessCenter xmlns="http://www.gym.com">
<Member level="platinum">
<Name>Jeff</Name>
<Phone type="home">555-1234</Phone>
<Phone type="work">555-4321</Phone>
<FavoriteColor>lightgrey</FavoriteColor>
</Member>
</FitnessCenter>
This is FitnessCenter_v5.xml in the example000 folder.
195
Namespace Terminology
{http://www.gym.com}Member
Local name
Namespace URI
Expanded name = The combination of the namespace URI and the local name
196
</gym:FitnessCenter>
<gym:Member>
prefix
197
local-name(node?)
This is an XPath function which returns a
string, corresponding to the local name of
the node.
local-name(.)
Result:
Name
198
namespace-uri(node?)
This is an XPath function which returns a string
corresponding to the namespace URI of the node.
namespace-uri(.)
Result:
http://www.gym.com
199
Congratulations!
You now know everything there is to know
about XPath 1.0!
We have covered the entire XPath
specification. We have left no stone
unturned.