XPath
XPath is core for XML query languages
Language for addressing parts of an
XML document.
It operates on the tree data model of XML
It has a non-XML syntax
Types of Path Expressions
Absolute (starting at the root of the tree)
Syntactically they begin with the symbol /
It refers to the root of the document (situated
one level above the root element of the
document)
Relative to a context node
An XML Example
<library location="Bremen">
<author name="Henry Wise">
<book title="Artificial Intelligence"/>
<book title="Modern Web Services"/>
<book title="Theory of Computation"/>
</author>
<author name="William Smart">
<book title="Artificial Intelligence"/>
</author>
<author name="Cynthia Singleton">
<book title="The Semantic Web"/>
<book title="Browser Technology Revised"/>
</author>
</library>
Tree Representation
Examples of Path Expressions in
XPath
/library/author
Addresses all author elements that are
children of the library element node, which
resides immediately below the root
/t1/.../tn, where each ti+1 is a child node
of ti, is a path through the tree
representation
Examples of Path Expressions in
XPath (2)
Address all author elements
//author
Here // says that we should consider all
elements in the document and check
whether they are of type author
This path expression addresses all author
elements anywhere in the document
Examples of Path Expressions in
XPath (3)
Address the location attribute nodes
within library element nodes
/library/@location
The symbol @ is used to denote
attribute nodes
Examples of Path Expressions in
XPath (4)
Address all title attribute nodes within
book elements anywhere in the
document, which have the value “Artificial
Intelligence”
//book/@title="Artificial Intelligence"
Examples of Path Expressions in
XPath (5)
Address all books with title “Artificial Intelligence”
//book[@title="Artificial Intelligence"]
Test within square brackets: a filter expression
It restricts the set of addressed nodes.
Differences
//book/@title="Artificial Intelligence“
//book[@title="Artificial Intelligence"]
Differences.
First query collects title attribute nodes of book elements
Second query addresses book elements, the title of which
satisfies a certain condition.
Tree Representation of Query 4
//book/@title="Artificial Intelligence"
Tree Representation of Query 5
//book[@title="Artificial Intelligence"]
Examples of Path Expressions in
XPath (6)
Address the first author element node in the
XML document
//author[1]
Address the last book element within the
first author element node in the document
//author[1]/book[last()]
Address all book element nodes without a
title attribute
//book[not @title]
General Form of Path
Expressions
A path expression consists of a series of
steps, separated by slashes
A step consists of
An axis specifier,
A node test, and
An optional predicate
General Form of Path
Expressions (2)
An axis specifier determines the tree
relationship between the nodes to be
addressed and the context node
E.g. parent, ancestor, child (the default),
sibling, attribute node
// is such an axis specifier: descendant or
self
General Form of Path
Expressions (3)
A node test specifies which nodes to
address
The most common node tests are element
names
E.g., * addresses all element nodes
comment() addresses all comment nodes
General Form of Path
Expressions (4)
Predicates (or filter expressions) are optional and are
used to refine the set of addressed nodes
E.g., the expression [1] selects the first node
[position()=last()] selects the last node
[position() mod 2 =0] selects the even nodes
XPath has a more complicated full syntax.
We have only presented the abbreviated syntax