Professional Documents
Culture Documents
Oscar Díaz
Dpto. de Lenguajes y Sistemas Informáticos
Contents
What is XPath?
XPath Functions
Location Path
• Axes
• Node type tests
• Predicates
Oscar Díaz 2
1
What is XPath?
“A language for addressing parts of an XML
document”.
Oscar Díaz 3
Att ib t
Attribute Elementt
El Elementt
El
(version) (movie) (movie)
Oscar Díaz 4
2
XPath Expressions
An XPath expression can be seen as function composition
Oscar Díaz 5
Example
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
following: selects everything, elements and attributes, that follow the current
node
following-sibling: selects the following siblings
axis::nodeTest[predicate]
An axis can potentially locate a lot of
nodes.
Oscar Díaz 12
5
descendant:: …
actor – filters the “actor” type descendants
Oscar Díaz 13
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
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
< > = >= <= != ~=
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
Oscar Díaz 19
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
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>
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/text()]
Oscar Díaz 29
More examples
Filter Expression
• /movie [ @type = 'mistery' ] /producer
• /movie [ producer/name = 'Ethan' ] /producer
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
Oscar Díaz 31
15