You are on page 1of 15

XPath

Oscar Díaz
Dpto. de Lenguajes y Sistemas Informáticos

Contents
 What is XPath?
 XPath Functions
 Location Path
• Axes
• Node type tests
• Predicates

 Location Path Composition


 Examples

Oscar Díaz 2

1
What is XPath?
 “A language for addressing parts of an XML
document”.

 XPath is a query language whose syntax uses


path expressions on the document

 XPath is used in other W3C specifications


• XSLT
• XLink
• XQuery

Oscar Díaz 3

XPath is a functional language:


input: tree  output: node set
<?xml version="1.0" ?>
<!-- Mis peliculas preferidas -->
<movies>
<movie rating="PG-13" >
<title>Raising Arizona</title>
<writer>Ethan Coen</writer>
<writer>Joel Coen</writer> Document
(#document)
</movie>
<movie> .... </movie>
</movies> ProcessingInstruction Comment Element
(xml) (#comment) (movies)

Att ib t
Attribute Elementt
El Elementt
El
(version) (movie) (movie)

Attribute Element Element Element


Node type (rating) (title) (writer) (writer)

Node name Text


(#text)
Text
(#text)
Text
(#text)

Oscar Díaz 4

2
XPath Expressions
 An XPath expression can be seen as function composition

 A function (“location path”)


• E.g. “child::movie[3]” is a function that returns a “movie” set
• Input: context node  Output: node set

 Function composition (“/”)


• E.g.: child::movie/child:actor returns “actor” nodes
• It evaluates from left to right:
– the output of child:movie becomes the input of child:actor

• The first function (child:movie) is evaluated on the context node


– “.” denotes the context node

Oscar Díaz 5

Example

Document context element


(#document)
“you are here”

ProcessingInstruction Comment Element


(xml) (#comment) (movies)

Attribute Element Element


(version) (movie) (movie)

Attribute Element Element Element


((rating)
g) ((title)) ((writer)) ((writer))

Text Text Text


(#text) (#text) (#text)

child::movie child::movie/child::writer

Oscar Díaz 6

3
Location Path
 Consists of
• axis
• node test
• predicates (optional)

 Syntax: axis::nodeTest[predicate]
• E.g.: child::movie[title=`Raising Arizona´]

Oscar Díaz 7

axis::nodeTest[predicate]

Oscar Díaz 8

4
Cont.
 parent: selects the parent
 ancestor: selects all the ancestors
 ancestor-or-self: selects all the ancestors, including the current node

 child: selects all the child elements (DEFAULT AXIS)


 descendant: selects all the descendants
 descendant-or-self: selects all the descendants, including the current node

 following: selects everything, elements and attributes, that follow the current
node
 following-sibling: selects the following siblings

 preceding: selects everything, elements and attributes, that precede the


current node
 preceding-sibling: select the preceding siblings

 self: selects the current node


 attribute: selects all the attributes
Oscar Díaz 11

axis::nodeTest[predicate]
 An axis can potentially locate a lot of
nodes.

 The test is a predicate that is applied to


each node of the axis and filters only
nodes of a given type.

 If the test is fulfilled, the node is kept.


Otherwise, it is discarded.

Oscar Díaz 12

5
descendant:: …
 actor – filters the “actor” type descendants

 text() – filters the text type descendants


 attribute() - filters the attribute type descendants
 comment() - filters the comment type descendants

 * - filters the non-terminal nodes (that are not text)


 @* - filters the context node attributes + its descendants
(not only de attribute() type ones)
 node() – filters the nodes that are not comments
• equivalent to: * or @* or text()

Oscar Díaz 13

Examples taking movies as the


context node
 descendant::movie
•  retrieves the movie nodes

 descendant::title
•  retrieves the title nodes

 descendant::comment()
•  retrieves the comment nodes

 descendant::attribute
•  retrieves the attribute nodes

Oscar Díaz 14

6
axis::nodeTest[predicate]
 descendant::movie[title = “Airbang”]
 The p
predicate acts as a filter: e[p]
[p]
• for each element in the sequence e, if p is true, then
propagate the element to the output, otherwise
discard it
 Existential semantics of predicates
• [actor = “Barden”] is true if at least one element
returned byy “actor” is string
g and equals
q to “Barden”
• note that [actor = “Barden”] is false if “actor” returns
the empty sequence
 The predicate may be a simple XPath
• [actor] is true if “actor” returns a non-empty sequence
Oscar Díaz 15

Functions
 XPath provides more than 25 functions
 Functions with no argument operate on the context node

 Node Set Functions


• count() – returns the number of nodes that the argument returns

 String Functions
• concat() – returns the concatenation of its arguments

 Number Functions
• sum() – returns the sum of the values of a node set

 Boolean Functions
• not() – true if the argument is false, and vice versa
• contains(string1, string2) - true if string1 contains string2
• starts-with(string,pattern) - true if string starts with the pattern pattern

 http://www.w3.org/TR/xpath-functions/

Oscar Díaz 16

7
Operators

•Boolean operators:
• and,
and or,
or not

•Arithmetic operators:
•+ - * div mod

•Relational operators:
< > = >= <= <> LIKE
&lt; &gt; = &gt;= &lt;= != ~=

Oscar Díaz 17

Examples

 descendant::movie[title = “Airbang”]

 descendant::movie[count(actor) > 3]

 descendant::movie[contains(actor, ‘Marlon’)]

 descendant::movie[position(.) = 3]

Oscar Díaz 18

8
Function composition
 It is specified using a slash: /
• f1 / f2 / f3

 The composition goes from left to right


• f1  f2  f3

 The output of a function provides the


context node to evaluate the next function

Oscar Díaz 19

Examples and their abbreviations


 child::movie/child:title
•  movie/title

 child::movie/descendant::surname
•  movie//surname

 child::movie/parent::.
•  movie/..
i /

 child::movie/attribute::rating
•  movie/@rating

Oscar Díaz 20

9
Path types

 Absolute
• they are evaluated from the root node
• they start with /
• /child::movies//child::title  would retrieve all the titles

 Relative
• they are evaluated from a context node
• they start directly with the expression
• child::title  would select the title of the context node

Oscar Díaz 21

<bib>
<bib>
<book>
<book> <publisher>
<publisher>Addison-Wesley
Addison-Wesley</publisher>
</publisher>
Examples III <author>
<author>Serge
<author>
SergeAbiteboul
Abiteboul</author>
<author><first-name>
<first-name>Rick
</author>
Rick</first-name>
</first-name>
<last-name>
<last-name>Hull
Hull</last-name>
</last-name>
</author>
</author>
<author>
<author>Victor
VictorVianu
Vianu</author>
</author>
<title>
<title>Foundations
FoundationsofofDatabases
Databases</title>
</title>
<year>
<year>1995
1995</year>
</year>
</book>
/b k
</book>
<book
<bookprice=“55”>
price=“55”>
<publisher>
<publisher>Freeman
Freeman</publisher>
</publisher>
/bib/book/year <author>
<author>Jeffrey
JeffreyD.
D.Ullman
Ullman</author>
</author>
<title>
<title>Principles
Principlesof
ofDatabase
Databaseand
andKnowledge
Knowledge
<year>
<year>1998
1998</year>
</year>
</book>
</book>
Result: <year> 1995 </year>
</bib>
</bib>
<year>
y 1998 </year>
y
(*) Taken
T k from
f Prof.
P f D Don S
Suciu
i

/bib/paper/year

Result: empty (there are no “papers”)


Oscar Díaz 22

10
<bib>
<bib>
<book>
<book> <publisher>
<publisher>Addison-Wesley
Addison-Wesley</publisher>
</publisher>
Examples … <author>
<author>Serge
<author>
SergeAbiteboul
Abiteboul</author>
<author><first-name>
<first-name>Rick
</author>
Rick</first-name>
</first-name>
<last-name>
<last-name>Hull
Hull</last-name>
</last-name>
</author>
</author>
<author>
<author>Victor
VictorVianu
Vianu</author>
</author>
<title>
<title>Foundations
FoundationsofofDatabases
Databases</title>
</title>
<year>
<year>1995
1995</year>
</year>
</book>
/b k
</book>
//author (Restricted Kleene Closure) <book <bookprice=“55”>
price=“55”>
<publisher>
<publisher>Freeman
Freeman</publisher>
</publisher>
<author>
<author>Jeffrey
JeffreyD.
D.Ullman
Ullman</author>
</author>
Result:<author> Serge Abiteboul </author> <title> <title>Principles
<year>
Principlesof
ofDatabase
Databaseand
andKnowledge
Knowledge
<year>1998
1998</year>
</year>
<author> <first-name> Rick </first-name>
</book>
</book>
</bib>
<last-name> Hull </last-name>
</bib>
</author>
<author> Victor Vianu </author>
<author> Jeffrey D. Ullman </author>

/bib//first-name
Result: <first-name> Rick </first-name>
Oscar Díaz 23

<bib>
<bib>
<book>
<book> <publisher>
<publisher>Addison-Wesley
Addison-Wesley</publisher>
</publisher>
Examples … <author>
<author>Serge
<author>
SergeAbiteboul
Abiteboul</author>
<author><first-name>
<first-name>Rick
</author>
Rick</first-name>
</first-name>
<last-name>
<last-name>Hull
Hull</last-name>
</last-name>
</author>
</author>
<author>
<author>Victor
VictorVianu
Vianu</author>
</author>
<title>
<title>Foundations
FoundationsofofDatabases
Databases</title>
</title>
<year>
<year>1995
1995</year>
</year>
</book>
/b k
</book>
<book
<bookprice=“55”>
price=“55”>
<publisher>
<publisher>Freeman
Freeman</publisher>
</publisher>
<author>
<author>Jeffrey
JeffreyD.
D.Ullman
Ullman</author>
</author>
<title>
<title>Principles
Principlesof
ofDatabase
Databaseand
andKnowledge
Knowledge
<year>
<year>1998
1998</year>
</year>
</book>
</book>
/bib/book/author/text() </bib>
</bib>

Result: Serge Abiteboul


Victor Vianu
Jeffrey D. Ullman

Rick Hull does not appear because it has a firstname, lastname structure
Oscar Díaz 24

11
<bib>
<bib>
<book>
<book> <publisher>
<publisher>Addison-Wesley
Addison-Wesley</publisher>
</publisher>
Examples… <author>
<author>Serge
<author>
SergeAbiteboul
Abiteboul</author>
<author><first-name>
<first-name>Rick
</author>
Rick</first-name>
</first-name>
<last-name>
<last-name>Hull
Hull</last-name>
</last-name>
</author>
</author>
<author>
<author>Victor
VictorVianu
Vianu</author>
</author>
<title>
<title>Foundations
FoundationsofofDatabases
Databases</title>
</title>
<year>
<year>1995
1995</year>
</year>
</book>
/b k
</book>
<book
<bookprice=“55”>
price=“55”>
<publisher>
<publisher>Freeman
Freeman</publisher>
</publisher>
<author>
<author>Jeffrey
JeffreyD.
D.Ullman
Ullman</author>
</author>
<title>
<title>Principles
Principlesof
ofDatabase
Databaseand
andKnowledge
Knowledge
<year>
<year>1998
1998</year>
</year>
</book>
//author/* </book>
</bib>
</bib>

Result:
<first-name> Rick </first-name>
<last-name> Hull </last-name>
Oscar Díaz 25

<bib>
<bib>
<book>
<book> <publisher>
<publisher>Addison-Wesley
Addison-Wesley</publisher>
</publisher>
Examples … <author>
<author>Serge
<author>
SergeAbiteboul
Abiteboul</author>
<author><first-name>
<first-name>Rick
</author>
Rick</first-name>
</first-name>
<last-name>
<last-name>Hull
Hull</last-name>
</last-name>
</author>
</author>
<author>
<author>Victor
VictorVianu
Vianu</author>
</author>
<title>
<title>Foundations
FoundationsofofDatabases
Databases</title>
</title>
<year>
<year>1995
1995</year>
</year>
</book>
/b k
</book>
<book
<bookprice=“55”>
price=“55”>
<publisher>
<publisher>Freeman
Freeman</publisher>
</publisher>
<author>
<author>Jeffrey
JeffreyD.
D.Ullman
Ullman</author>
</author>
<title>
<title>Principles
Principlesof
ofDatabase
Databaseand
andKnowledge
Knowledge
<year>
<year>1998
1998</year>
</year>
</book>
</book>
</bib>
/bib/book/@price </bib>

Resultado: “55”

Oscar Díaz 26

12
<bib>
<bib>
<book>
<book> <publisher>
<publisher>Addison-Wesley
Addison-Wesley</publisher>
</publisher>
Examples … <author>
<author>Serge
<author>
SergeAbiteboul
Abiteboul</author>
<author><first-name>
<first-name>Rick
</author>
Rick</first-name>
</first-name>
<last-name>
<last-name>Hull
Hull</last-name>
</last-name>
</author>
</author>
<author>
<author>Victor
VictorVianu
Vianu</author>
</author>
<title>
<title>Foundations
FoundationsofofDatabases
Databases</title>
</title>
<year>
<year>1995
1995</year>
</year>
</book>
/b k
</book>
<book
<bookprice=“55”>
price=“55”>
<publisher>
<publisher>Freeman
Freeman</publisher>
</publisher>
<author>
<author>Jeffrey
JeffreyD.
D.Ullman
Ullman</author>
</author>
<title>
<title>Principles
Principlesof
ofDatabase
Databaseand
andKnowledge
Knowledge
<year>
<year>1998
1998</year>
</year>
</book>
/bib/book/author[first-name] </book>
</bib>
</bib>

Result: <author>
<first-name> Rick </first-name>
<last-name> Hull </last-name>
</author>
Oscar Díaz 27

<bib>
<bib>
<book>
<book> <publisher>
<publisher>Addison-Wesley
Addison-Wesley</publisher>
</publisher>
Examples … <author>
<author>Serge
<author>
SergeAbiteboul
Abiteboul</author>
<author><first-name>
<first-name>Rick
</author>
Rick</first-name>
</first-name>
<last-name>
<last-name>Hull
Hull</last-name>
</last-name>
</author>
</author>
<author>
<author>Victor
VictorVianu
Vianu</author>
</author>
<title>
<title>Foundations
FoundationsofofDatabases
Databases</title>
</title>
<year>
<year>1995
1995</year>
</year>
</book>
/b k
</book>
<book
<bookprice=“55”>
price=“55”>
<publisher>
<publisher>Freeman
Freeman</publisher>
</publisher>
<author>
<author>Jeffrey
JeffreyD.
D.Ullman
Ullman</author>
</author>
<title>
<title>Principles
Principlesof
ofDatabase
Databaseand
andKnowledge
Knowledge
<year>
<year>1998
1998</year>
</year>
</book>
</book>
</bib>
</bib>

/bib/book/author[first-name][address[//zip][city]]/last-
/bib/b k/ h [fi ][ dd [// i ][ i ]]/l
name
Result: <last-name> … </last-name>
<last-name> … </last-name>
Oscar Díaz 28

13
<bib>
<bib>
<book>
<book> <publisher>
<publisher>Addison-Wesley
Addison-Wesley</publisher>
</publisher>
Examples … <author>
<author>Serge
<author>
SergeAbiteboul
Abiteboul</author>
<author><first-name>
<first-name>Rick
</author>
Rick</first-name>
</first-name>
<last-name>
<last-name>Hull
Hull</last-name>
</last-name>
</author>
</author>
<author>
<author>Victor
VictorVianu
Vianu</author>
</author>
<title>
<title>Foundations
FoundationsofofDatabases
Databases</title>
</title>
<year>
<year>1995
1995</year>
</year>
</book>
/b k
</book>
<book
<bookprice=“55”>
price=“55”>
<publisher>
<publisher>Freeman
Freeman</publisher>
</publisher>
<author>
<author>Jeffrey
JeffreyD.
D.Ullman
Ullman</author>
</author>
<title>
<title>Principles
Principlesof
ofDatabase
Databaseand
andKnowledge
Knowledge
<year>
<year>1998
1998</year>
</year>
/bib/book[@price < “60”] </book>
</book>
</bib>
</bib>

/bib/book[author/@age < “25”]/title

/bib/book[author/text()]
Oscar Díaz 29

More examples
 Filter Expression
• /movie [ @type = 'mistery' ] /producer
• /movie [ producer/name = 'Ethan' ] /producer

 Relational Operators ( = != > >= <


<= ~=)
=)
• /movie[ @type != 'mistery']
• /movie/pruducer[ name = "Athen" ]
• /movie[producer/name ~= "Nico*" ]

Oscar Díaz 30

14
More examples
 Arithmetic Operators ( + - * div
mod)
• //movie[
i [ (budget
(b d t -200)
200) div
di 5 < (2000 * 1.16)
1 16)
mod 1000 ]/producer

 Logical Operators (and or)


• /movie[budget < 1000 and producer/name
~= "abellan"]

Oscar Díaz 31

15

You might also like