View
213
Download
0
Embed Size (px)
Citation preview
Friday, September 4th, 2009
The Systems Groupat ETH Zurich
XML and DatabasesXML and DatabasesExercise Session 6Exercise Session 6
courtesy of Ghislain Fourny/ETHcourtesy of Ghislain Fourny/ETH
© Department of Computer Science | ETH Zürich
Friday, September 4th, 2009 PUL Algebra Theory
Previously, in XML&DatabasesPreviously, in XML&Databases
2
DTDXML Schema
InfosetPSVIXDM
<a/>
All of this should be familiar to you
by now.
All of this should be familiar to you
by now.
XMLwith/withoutnamespaces
Friday, September 4th, 2009 PUL Algebra Theory
Previously, in XML&DatabasesPreviously, in XML&Databases
3
DTDXML Schema
InfosetPSVIXDM
<a/>
So, now we can leverage the power
of XML.
So, now we can leverage the power
of XML.
XQueryXSLT
XMLwith/withoutnamespaces
Friday, September 4th, 2009 PUL Algebra Theory
Previously, in XML&DatabasesPreviously, in XML&Databases
4
XMLwith
namespaces
(XML Schema)optional
XDM
<a/>
This would be the typical setup.
This would be the typical setup.
XQuery
Friday, September 4th, 2009 PUL Algebra Theory
Reminder: XML and Data ModelsReminder: XML and Data Models
5
<a> <d e="f"/> <c>This is <b>text</b>.</c></a>
Physical view(syntax)
Logical view(data model)
a
d
This is
c
b .
text
e:f
Friday, September 4th, 2009 PUL Algebra Theory
Reminder: XQuery and XPath Data Reminder: XQuery and XPath Data ModelModel
6
In XQuery, anything that goes into or comes out of an expression is a sequence of items!In XQuery, anything that goes into or comes out of an expression is a sequence of items!
ExpressionExpression
for if thenelse where
order by
whileany
every
letreturn
exit with
=
+
Friday, September 4th, 2009 PUL Algebra Theory
Ever played Lego?Ever played Lego?
9
if( )
then
else
my:func( )my:func( )
Friday, September 4th, 2009 PUL Algebra Theory
Ever played Lego?Ever played Lego?
10
if( )
then
else
my:func( )my:func( )
Friday, September 4th, 2009 PUL Algebra Theory
Ever played Lego?Ever played Lego?
11
if( )
then
else
my:func( )my:func( )2
Friday, September 4th, 2009 PUL Algebra Theory
Ever played Lego?Ever played Lego?
12
if( )
then
else
for $x in
let $y :=
return
my:func( )my:func( )2
Friday, September 4th, 2009 PUL Algebra Theory
Ever played Lego?Ever played Lego?
13
if( )
then
else
for $x in
let $y :=
return
my:func( )my:func( )2
Friday, September 4th, 2009 PUL Algebra Theory
Ever played Lego?Ever played Lego?
14
if( )
then
else
for $x in
let $y :=
return
my:func( )my:func( )2
Friday, September 4th, 2009 PUL Algebra Theory
Ever played Lego?Ever played Lego?
15
if( )
then
else
for $x in
let $y :=
return
my:func( )my:func( )2
Friday, September 4th, 2009 PUL Algebra Theory
Ever played Lego?Ever played Lego?
16
if( )
then
else
for $x in
let $y :=
return
my:func( )my:func( )2
Friday, September 4th, 2009 PUL Algebra Theory
XQuery ExpressionsXQuery Expressions
17
if( )
then
else
Friday, September 4th, 2009 PUL Algebra Theory
XQuery ExpressionsXQuery Expressions
18
for $x in
let $y :=
for $z in
where
order by
return
Friday, September 4th, 2009 PUL Algebra Theory
PrecedencePrecedence
Some expressions have priority over others while parsing an XQuery program: Lowest priority: comma (,) Then FLWOR, if/then/else, ... on same level Operators (+,...) Path expressions (/) Highest priority: literals, variables, function calls,
element constructors...
Use parentheses when in doubt!
Friday, September 4th, 2009 PUL Algebra Theory
Exercise 1: Simple queriesExercise 1: Simple queries
21
Friday, September 4th, 2009 PUL Algebra Theory
Exercise 1: Simple queriesExercise 1: Simple queries
22
Friday, September 4th, 2009 PUL Algebra Theory
Exercise 1: Titles sorted by priceExercise 1: Titles sorted by price
23
Friday, September 4th, 2009 PUL Algebra Theory
Exercise 1: Titles sorted by priceExercise 1: Titles sorted by price
24
Friday, September 4th, 2009 PUL Algebra Theory
Exercise 1: Titles sorted by priceExercise 1: Titles sorted by price
25
Friday, September 4th, 2009 PUL Algebra Theory
Exercise 1: Titles sorted by priceExercise 1: Titles sorted by price
26
Friday, September 4th, 2009 PUL Algebra Theory
Exercise 1: How many books Exercise 1: How many books written by Abiteboul?written by Abiteboul?
27
Friday, September 4th, 2009 PUL Algebra Theory
Exercise 1: How many books Exercise 1: How many books written by Abiteboul?written by Abiteboul?
28
Friday, September 4th, 2009 PUL Algebra Theory
Exercise 1: How many books Exercise 1: How many books written by Abiteboul?written by Abiteboul?
29
Friday, September 4th, 2009 PUL Algebra Theory
Exercise 1: How many books Exercise 1: How many books written by Abiteboul?written by Abiteboul?
30
2
Friday, September 4th, 2009 PUL Algebra Theory
Exercise 1: Number of books by Exercise 1: Number of books by authorauthor
31
Friday, September 4th, 2009 PUL Algebra Theory
Exercise 1: Number of books by Exercise 1: Number of books by authorauthor
32
Friday, September 4th, 2009 PUL Algebra Theory
Exercise 1: Number of books by Exercise 1: Number of books by authorauthor
33
Friday, September 4th, 2009 PUL Algebra Theory
Exercise 1: Number of books by Exercise 1: Number of books by authorauthor
34
Friday, September 4th, 2009 PUL Algebra Theory
Exercise 1: Number of books by Exercise 1: Number of books by authorauthor
35
<?xml version="1.0" encoding="UTF-8"?><res> <name>Stevens</name>
</res><res> <name>Abiteboul</name>
</res><res> <name>Buneman</name>
</res><res> <name>Suciu</name>
</res>
Friday, September 4th, 2009 PUL Algebra Theory
Exercise 1: Number of books by Exercise 1: Number of books by authorauthor
36
<?xml version="1.0" encoding="UTF-8"?><res> <name>Stevens</name> <count>2</count></res><res> <name>Abiteboul</name> <count>2</count></res><res> <name>Buneman</name> <count>1</count></res><res> <name>Suciu</name> <count>1</count></res>
Friday, September 4th, 2009 PUL Algebra Theory
The document (from first exercise The document (from first exercise sheet)sheet)
37
You (should) know how namespaces work in XML (prefix-binding)You (should) know how namespaces work in XML (prefix-binding)
Friday, September 4th, 2009 PUL Algebra Theory
The document (from first exercise The document (from first exercise sheet)sheet)
38
How should we navigate a document which has namespaces with XQuery?
How should we navigate a document which has namespaces with XQuery?
Friday, September 4th, 2009 PUL Algebra Theory
Exercise 2: How prefixes are Exercise 2: How prefixes are bound to namespaces in XQuery bound to namespaces in XQuery (Basics)(Basics) Very simple! Just a prolog declaration...
And then you can use the prefix in the XQuery code (e.g., in path expressions).
39
Friday, September 4th, 2009 PUL Algebra Theory
Exercise 2: How prefixes are Exercise 2: How prefixes are bound to namespaces in XQuery bound to namespaces in XQuery (Basics)(Basics) Very simple! Just a prolog declaration...
And then you can use the prefix in the XQuery code (e.g., in path expressions).
Actually, you can also bind them in element constructors just like in XML.
40
Friday, September 4th, 2009 PUL Algebra Theory
Exercise 2: Let‘s try this out!Exercise 2: Let‘s try this out!
41
Friday, September 4th, 2009 PUL Algebra Theory
Exercise 2: Let‘s try this out!Exercise 2: Let‘s try this out!
42
Friday, September 4th, 2009 PUL Algebra Theory
Exercise 2: Let‘s try this out!Exercise 2: Let‘s try this out!
43
Friday, September 4th, 2009 PUL Algebra Theory
Exercise 2: Let‘s try this out!Exercise 2: Let‘s try this out!
44
Friday, September 4th, 2009 PUL Algebra Theory
Exercise 2: Let‘s try this out!Exercise 2: Let‘s try this out!
45
Friday, September 4th, 2009 PUL Algebra Theory
Exercise 2: Let‘s try this out!Exercise 2: Let‘s try this out!
46
Friday, September 4th, 2009 PUL Algebra Theory
Exercise 2: Let‘s try this out!Exercise 2: Let‘s try this out!
47
Friday, September 4th, 2009 PUL Algebra Theory
Exercise 2: Let‘s try this out!Exercise 2: Let‘s try this out!
48
Friday, September 4th, 2009 PUL Algebra Theory
Exercise 2: Let‘s try this out!Exercise 2: Let‘s try this out!
49
Friday, September 4th, 2009 PUL Algebra Theory
Exercise 2: Let‘s try this out!Exercise 2: Let‘s try this out!
50
Friday, September 4th, 2009 PUL Algebra Theory
Exercise 2: Let‘s try this out!Exercise 2: Let‘s try this out!
51
Friday, September 4th, 2009 PUL Algebra Theory
Exercise 2: Let‘s try this out!Exercise 2: Let‘s try this out!
52
Friday, September 4th, 2009 PUL Algebra Theory
Exercise 2: Let‘s try this out!Exercise 2: Let‘s try this out!
53
Friday, September 4th, 2009 PUL Algebra Theory
Exercise 2: Let‘s try this out!Exercise 2: Let‘s try this out!
54
Friday, September 4th, 2009 PUL Algebra Theory
Exercise 2: Let‘s try this out!Exercise 2: Let‘s try this out!
55
Friday, September 4th, 2009 PUL Algebra Theory
Exercise 2: Let‘s try this out!Exercise 2: Let‘s try this out!
56
Friday, September 4th, 2009 PUL Algebra Theory
Exercise 2: The other wayExercise 2: The other way
57
Friday, September 4th, 2009 PUL Algebra Theory
Exercise 3: Comparison Operators Exercise 3: Comparison Operators in XQueryin XQuery
58
Friday, September 4th, 2009 PUL Algebra Theory
Exercise 3: Comparison Operators Exercise 3: Comparison Operators in XQueryin XQuery
In XQuery, there are three kinds of comparisons General Comparisons
Value Comparisons
Node Comparisons
59
Friday, September 4th, 2009 PUL Algebra Theory
Exercise 3: Comparison Operators Exercise 3: Comparison Operators in XQueryin XQuery
In XQuery, there are three kinds of comparisons General Comparisons
= < > <= >= Value Comparisons
Node Comparisons
60
Friday, September 4th, 2009 PUL Algebra Theory
Exercise 3: Comparison Operators Exercise 3: Comparison Operators in XQueryin XQuery
In XQuery, there are three kinds of comparisons General Comparisons
= < > <= >= Value Comparisons
eq lt gt le ge Node Comparisons
61
Friday, September 4th, 2009 PUL Algebra Theory
Exercise 3: Comparison Operators Exercise 3: Comparison Operators in XQueryin XQuery
In XQuery, there are three kinds of comparisons General Comparisons
= < > <= >= Value Comparisons
eq lt gt le ge Node Comparisons
is
62
Friday, September 4th, 2009 PUL Algebra Theory
Exercise 3: General ComparisonsExercise 3: General Comparisons
To compare sequences of items each item: atomic item or node item which is
atomized
63
In XQuery, if you receive a node item where an atomic item is expected, the node is atomized.
In XQuery, if you receive a node item where an atomic item is expected, the node is atomized.
= < > <= >=
Friday, September 4th, 2009 PUL Algebra Theory
Exercise 3: General ComparisonsExercise 3: General Comparisons
To compare sequences of items each item: atomic item or node which is atomized
Answers to the question: does any combination of a value from the first sequence and a value from the second sequence
fulfil the condition?
64
=
= < > <= >=
Friday, September 4th, 2009 PUL Algebra Theory
Exercise 3: General ComparisonsExercise 3: General Comparisons
To compare sequences of items each item: atomic item or node which is atomized
Answers to the question: does any combination of a value from the first sequence and a value from the second sequence
fulfil the condition?
(1,2) = (3,<a>1</a>)65
= < > <= >=
Friday, September 4th, 2009 PUL Algebra Theory
Exercise 3: General ComparisonsExercise 3: General Comparisons
To compare sequences of items each item: atomic item or node which is atomized
Answers to the question: does any combination of a value from the first sequence and a value from the second sequence
fulfil the condition?
(1,2) = (3,<a>1</a>)66
= < > <= >=
Friday, September 4th, 2009 PUL Algebra Theory
Exercise 3: General ComparisonsExercise 3: General Comparisons
To compare sequences of items each item: atomic item or node which is atomized
Answers to the question: does any combination of a value from the first sequence and a value from the second sequence
fulfil the condition?
(1,2) = (3,<a>1</a>) (1,2) > (3,4)67
= < > <= >=
Friday, September 4th, 2009 PUL Algebra Theory
Exercise 3: General ComparisonsExercise 3: General Comparisons
To compare sequences of items each item: atomic item or node which is atomized
Answers to the question: does any combination of a value from the first sequence and a value from the second sequence
fulfil the condition?
(1,2) = (3,<a>1</a>) (1,2) > (3,4)68
= < > <= >=
Friday, September 4th, 2009 PUL Algebra Theory
Exercise 3: Value ComparisonsExercise 3: Value Comparisons
Compares one value to one value (Empty sequence allowed)
69
le
eq lt gt le ge
Friday, September 4th, 2009 PUL Algebra Theory
Exercise 3: Value ComparisonsExercise 3: Value Comparisons
Compares one value to one value (Empty sequence allowed)
32 eq xs:integer(<a>32</a>)
70
eq lt gt le ge
Friday, September 4th, 2009 PUL Algebra Theory
Exercise 3: Value ComparisonsExercise 3: Value Comparisons
Compares one value to one value (Empty sequence allowed)
32 eq xs:integer(<a>32</a>)
71
eq lt gt le ge
Friday, September 4th, 2009 PUL Algebra Theory
Exercise 3: Value ComparisonsExercise 3: Value Comparisons
Compares one value to one value (Empty sequence allowed)
32 eq xs:integer(<a>32</a>)
31 gt xs:integer(<a>32</a>)
72
eq lt gt le ge
Friday, September 4th, 2009 PUL Algebra Theory
Exercise 3: Value ComparisonsExercise 3: Value Comparisons
Compares one value to one value (Empty sequence allowed)
32 eq xs:integer(<a>32</a>)
31 gt xs:integer(<a>32</a>)
73
eq lt gt le ge
Friday, September 4th, 2009 PUL Algebra Theory
Exercise 3: Node ComparisonsExercise 3: Node Comparisons
Compares node identities
74
is
Friday, September 4th, 2009 PUL Algebra Theory
Exercise 3: Node ComparisonsExercise 3: Node Comparisons
Compares node identities
let $a := <a/> let $b := $a return $a is $b
75
is
Friday, September 4th, 2009 PUL Algebra Theory
Exercise 3: Node ComparisonsExercise 3: Node Comparisons
Compares node identities
let $a := <a/> let $b := $a return $a is $b
76
is
Friday, September 4th, 2009 PUL Algebra Theory
Exercise 3: Node ComparisonsExercise 3: Node Comparisons
Compares node identities
let $a := <a/> let $b := $a return $a is $b
let $a := <a/> let $b := <b/> return $a is $b
77
is
Friday, September 4th, 2009 PUL Algebra Theory
Exercise 3: Node ComparisonsExercise 3: Node Comparisons
Compares node identities
let $a := <a/> let $b := $a return $a is $b
let $a := <a/> let $b := <b/> return $a is $b
78
is
Friday, September 4th, 2009 PUL Algebra Theory
Exercise 3: General/Value Exercise 3: General/Value comparisonscomparisons
79
Friday, September 4th, 2009 PUL Algebra Theory
Exercise 3: General/Value Exercise 3: General/Value comparisonscomparisons
For both, values have to have comparable types (e.g., string is not comparable to integer)
80
Friday, September 4th, 2009 PUL Algebra Theory
Exercise 3: General/Value Exercise 3: General/Value comparisonscomparisons
For both, values have to have comparable types (e.g., string is not comparable to integer)
Empty sequence treated differently General comparison returns false Value comparison returns ()
81
Friday, September 4th, 2009 PUL Algebra Theory
Exercise 3: General/Value Exercise 3: General/Value comparisonscomparisons
For both, values have to have comparable types (e.g., string is not comparable to integer)
Empty sequence treated differently General comparison returns false Value comparison returns ()
Untyped data treated differently General comparison casts to type of other operand Value comparison casts to string
82
Friday, September 4th, 2009 PUL Algebra Theory
Exercise 3: 1=2Exercise 3: 1=2
Find $x s.t. $x = 1 and $x = 2
83
Friday, September 4th, 2009 PUL Algebra Theory
Exercise 3: 1=2Exercise 3: 1=2
Find $x s.t. $x = 1 and $x = 2
84
(1,2)
Friday, September 4th, 2009 PUL Algebra Theory
Exercise 3: Transitivity of >Exercise 3: Transitivity of >
Find $x, $y, $z s.t. $x > $y and $y > $z and not $x > $z
85
Friday, September 4th, 2009 PUL Algebra Theory
Exercise 3: Transitivity of >Exercise 3: Transitivity of >
Find $x, $y, $z s.t. $x > $y and $y > $z and not $x > $z
86
$x := 2$y := (1,4)$z := 3
Friday, September 4th, 2009 PUL Algebra Theory
Exercise 3: $x eq $x, $x = $xExercise 3: $x eq $x, $x = $x
Find $x so that both $x eq $x and $x = $x are not true.
87
Friday, September 4th, 2009 PUL Algebra Theory
Exercise 3: $x eq $x, $x = $xExercise 3: $x eq $x, $x = $x
Find $x so that both $x eq $x and $x = $x are not true.
88
()