14 Xpath
14 Xpath
2
Terminology
<library> library is the parent of book; book is
<book> the parent of the two chapters
<chapter> The two chapters are the children of
</chapter> book, and the section is the child of
the second chapter
<chapter>
<section>
The two chapters of the book are
<paragraph/> siblings (they have the same parent)
<paragraph/> library, book, and the second chapter
</section> are the ancestors of the section
</chapter>
The two chapters, the section, and the
</book> two paragraphs are the descendents of
</library> the book
3
Paths
Operating system: XPath:
/ = the root directory /library = the root element (if named
library )
/users/dave/foo = the /library/book/chapter/section =
(one) file named foo in every section element in a chapter in
dave in users every book in the library
foo = the (one) file named foo section = every section element
in the current directory that is a child of the current element
5
Brackets and last()
6
Stars
A star, or asterisk, is a “wild card”—it means “all the
elements at this level”
Example: /library/book/chapter/* selects every child of
every chapter of every book in the library
Example: //book/* selects every child of every book
(chapters, tableOfContents, index, etc.)
Example: /*/*/*/paragraph selects every paragraph that
has exactly three ancestors
Example: //* selects every element in the entire document
7
Attributes I
8
Attributes II
//chapter[@num] selects every chapter element with an
attribute num
9
Values of attributes
//chapter[@num='3'] selects every chapter element with an
attribute num with value 3
//chapter[not(@num)] selects every chapter element that
does not have a num attribute
//chapter[@*] selects every chapter element that has any
attribute
//chapter[not(@*)] selects every chapter element with no
attributes
The normalize-space() function can be used to remove leading
and trailing spaces from a value before comparison
Example: //chapter[normalize-space(@num)="3"]
10
Axes
An axis (plural axes) is a set of nodes relative to a given
node; X::Y means “choose Y from the X axis”
self:: is the set of current nodes (not too useful)
self::node() is the current node
12
Axes (tree view)
Starting from a given node, the self, ancestor, descendant , preceding, and following
axes form a partition of all the nodes (if we ignore attribute and namespace nodes)
library
ancestor
following
book[1] book[2]
preceding self
chapter[1] chapter[2] chapter[3]
section[1]
descendant
paragraph[1] paragraph[2]
13
Axis examples
//book/descendant::* is all descendants of every book
//book/descendant::section is all section descendants of
every book
//parent::* is every element that is a parent, i.e., is not a leaf
//section/parent::* is every parent of a section element
//parent::chapter is every chapter that is a parent, i.e., has
children
/library/book[3]/following::* is everything after the third
book in the library
14
More axes
ancestor-or-self:: ancestors plus the current node
descendant-or-self:: descendants plus the current node
attribute:: is all attributes of the current node
namespace:: is all namespace nodes of the current node
preceding:: is everything before the current node in the
entire XML document
following-sibling:: is all siblings after the current node
15
Abbreviations for axes
16
Arithmetic expressions
+ add
- subtract
* multiply
div (not /) divide
mod modulo (remainder)
17
Equality tests
= means “equal to” (Notice it’s not ==)
!= means “not equal to”
But it’s not that simple!
value = node-set will be true if the node-set contains any
node with a value that matches value
value != node-set will be true if the node-set contains any
node with a value that does not match value
Hence,
value = node-set and value != node-set may both be true at
the same time!
18
Other boolean operators
and (infix operator)
or (infix operator)
Example: count = 0 or count = 1
not() (function)
19
Some XPath functions
20
The End
21