Xpath: Xpath Is Core For XML Query Languages Language For Addressing Parts of An XML Document
Xpath: Xpath Is Core For XML Query Languages Language For Addressing Parts of An XML Document
/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)
//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