1 XQuery Informationssysteme, 14.06.2007 Vortragender: Michael Schmidt.
-
Upload
jannike-wickland -
Category
Documents
-
view
107 -
download
0
Transcript of 1 XQuery Informationssysteme, 14.06.2007 Vortragender: Michael Schmidt.
1
XQuery
Informationssysteme, 14.06.2007
Vortragender: Michael Schmidt
2
XPath (Wiederholung)
Anfragesprache auf XML XPath-Expressions selektieren Knoten des
Eingabe-Dokuments
bib
book book
title titleauthor author
/bib//title
3
XPath (Wiederholung)
Path Steps: axis::nodetest Axen: „child“, „descendant“, „ancestor“,
„following“, „following-sibling“, ... Node-tests: Tag, „*“, „node()“, „text()“
Absolute Pfade: /step/step/… Relative Pfade: step/step/...
4
XQuery
„SQL für XML“ W3C Recommendation Funktionale Programmiersprache,
Turing-vollständig XQueries enthalten XPath Ausdrücke Ergebnis einer XQuery: wohlgeformte XML-
Dokumente (XPath: Menge von Knoten) Keine Datenmanipulationssprache (z.B. keine
Updates)
5
Node-Construction und Konstanten
<result> <text>{ "Hello World" }</text></result>
XQuery
<result> <text>Hello World</text></result>
Ergebnis
<result> <text>"Hello World"</text></result>
XQuery
<result> <text>"Hello World"</text></result>
Ergebnis
6
Dokument-Zugriff
<books> { doc("bookstore.xml")/bib/book[price>30]} </books>
<bib> <book> <title>T1</title> <author>A1</author> <price>50</price> </book> <book> <title>T2</title> <author>A2</author> <price>20</price> </book> <article> <title>T3</title> <author>A3</author> </article></bib>
bookstore.xml
<books> <book> <title>T1</title> <author>A1</author> <price>50</price> </book> </books>
Ergebnis
XQuery
7
Sequenz
<titles> { ( doc("bookstore.xml")/bib/article/title, doc("bookstore.xml")/bib/book/title )} </titles>
<titles> <title>T3</title> <title>T1</title> <title>T2</title></titles>
<bib> <book> <title>T1</title> <author>A1</author> <price>50</price> </book> <book> <title>T2</title> <author>A2</author> <price>20</price> </book> <article> <title>T3</title> <author>A3</author> </article></bib>
bookstore.xml
Ergebnis
XQuery
Ordnung bleibt erhalten!
8
FR-Expression
<booktitles> { for $book in doc("bookstore.xml")//book return $book/title} </booktitles>
<bib> <book> <title>T1</title> <author>A1</author> <price>50</price> </book> <book> <title>T2</title> <author>A2</author> <price>20</price> </book> <article> <title>T3</title> <author>A3</author> </article></bib>
bookstore.xml
<booktitles> <title>T1</title> <title>T2</title></booktitles>
Ergebnis
XQuery
9
FWR-Expressions<booktitles> { for $book in doc("bookstore.xml")//book where $book/price>30 return <booktitle>{ $book/title/text() }</booktitle>} </booktitles>
<bib> <book> <title>T1</title> <author>A1</author> <price>50</price> </book> <book> <title>T2</title> <author>A2</author> <price>20</price> </book> <article> <title>T3</title> <author>A3</author> </article></bib>
bookstore.xml
<booktitles> <booktitle>T1</booktitle></booktitles>
Ergebnis
Konstruktion von Knoten, die im Dokument nicht
vorkommen
XQuery
10
FLWR-Expressions<bib> <book> <title>T1</title> <author>A1</author> <price>50</price> </book> <book> <title>T2</title> <author>A2</author> <price>20</price> </book> <article> <title>T3</title> <author>A3</author> </article></bib>
bookstore.xml
<booktitles> { for $book in doc("bookstore.xml")//book let $booktitle := <bt>{ $book/title/text() }</bt> where ($book/price>30) return $booktitle} </booktitles>
<booktitles> <bt>T1</bt></booktitles>
Ergebnis
XQuery
11
FLWOR-Expressions
<booktitles> { for $book in doc("bookstore.xml")//book let $booktitle := $book/title where ($book/price>10) order by $booktitle/text() descending return <bt>{ $booktitle/text() }</bt>} </booktitles>
<bib> <book> <title>T1</title> <author>A1</author> <price>50</price> </book> <book> <title>T2</title> <author>A2</author> <price>20</price> </book> <article> <title>T3</title> <author>A3</author> </article></bib>
bookstore.xml
<booktitles> <bt>T2</bt> <bt>T1</bt></booktitles>
Ergebnis
XQuery
12
Where-Expressions
Konnektive and, or, fn:not
<bib> <book> <title>T1</title> <author>A1</author> <price>50</price> </book> <book> <title>T2</title> <author>A2</author> <price>20</price> </book> <article> <title>T3</title> <author>A3</author> </article></bib>
bookstore.xml
<booktitles> { for $book in doc("bookstore.xml")//book where ($book/price>10 and
fn:not($book/author/text()="A1")) return $book/title} </booktitles>
<booktitles> <title>T2</title></booktitles>
Ergebnis
XQuery
13
Some-Expressions
bookstore.xml
<booktitles> { for $book in doc("bookstore.xml")//book where (some $a in $book/author satisfies $a/text()="A2") return $book/title} </booktitles>
Ergebnis
XQuery
<bib> <book> <title>T1</title> <author>A1</author> <author>A2</author> </book> <book> <title>T2</title> <author>A3</author> <author>A4</author> </book></bib>
<booktitles> <title>T1</title></booktitles>
14
For vs let-Expressions
<for>{ for $x in (1 to 5) return <test> {$x} </test>}</for>
<let>{ let $x:=(1 to 5) return <test> {$x} </test>}</let>
<for> <test>1</test> <test>2</test> <test>3</test> <test>4</test> <test>5</test></for>
XQuery 1
XQuery 2
Ergebnis
Iteration
keine Iteration<let> <test>1 2 3 4 5</test></let>
Ergebnis
15
If-then-else Expressions
<booktitles> { for $book in doc("bookstore.xml")//book return if (fn:exists($book/price)) then <priced>{ $book/title }</priced> else <unpriced>{ $book/title }</unpriced>} </booktitles>
XQuery bookstore.xml
<bib> <book> <title>T1</title> <author>A1</author> </book> <book> <title>T2</title> <author>A2</author> <price>30</price> </book></bib>
<booktitles> <unpriced><title>T1</title></unpriced> <priced><title>T2</title></priced></booktitles>
Ergebnis
16
Vordefinierte Funktionen
Zahlreiche built-in Functions: Boolean: fn:exists(), fn:empty() … String Manipulation: fn:concat(), fn:uppercase() … String Tests: fn:contains(), fn:starts-with() … Sequenzen: fn:exactly-one(), fn:distinct-values() … Mathematisch: fn:floor(), fn:round(), fn:abs() …
…
17
Vordefinierte Funktionen
<booktitles> { for $book in doc("bookstore.xml")//book where (fn:exists($book/price) and fn:contains($book/author, "A")) return $book/title} </booktitles>
XQuery
<bib> <book> <title>T1</title> <author>A1</author> <price>50</price> </book> <book> <title>T2</title> <author>A2</author> <price>20</price> </book> <article> <title>T3</title> <author>A3</author> </article></bib>
bookstore.xml
<booktitles> <title>T1</title> <title>T2</title></booktitles>
Ergebnis
18
XQuery Joins
Vergleich unterschiedlicher Teile des Eingabe-Dokuments
Auch Joins zwischen verschiedenen Dokumenten möglich
Im Gegensatz zu SQL-Joins, oftmals nicht leicht zu erkennen, da keine eigene Syntax (kein JOIN-Keyword)
Optimierung der Auswertung von Joins durch herkömmliche und XML-spezifische Methoden
19
XQuery Joins<titles> { for $bib in doc("bookstore.xml")/bib return for $book in $bib/book return for $article in $bib/article where $book/author=$article/author return <pair>{ $book/title, $article/title}</pair>} </titles>
XQuery
bib
book
title author
T1 A1
book
title author
T2 A2
article
title author
T3 A3
article
title author
T4 A1
$book $article
<titles> <pair> <title>T1</title> <title>T4</title> </pair></titles>
Ergebnis
20
XQuery Joins
<books-with-prices> {
for $a in doc("www.amazon.com/review.xml")//book
for $b in doc("www.bn.com/bib.xml")//entry
where $a/isbn = $b/isbn
return
<book-with-prices>
{ $b/title }
<amazon> { $a/price/text() } </amazon>
<bn> { $b/price/text() } </bn>
</book-with-prices>
} </books-with-prices>
XQuery
Join über zwei Dokumente
21
Aggregatfunktionen
fn:min(), fn:max(), fn:count(), fn:sum(), fn:avg()
<booktitles> {
for $book in doc("bookstore.xml")//book return { ($book/title, <authors>{ fn:count($book/author) }</authors>) }} </booktitles>
XQuery bookstore.xml
<bib> <book> <title>T1</title> <author>A1</author> <author>A2</author> </book> <book> <title>T2</title> <author>A3</author> </book></bib>
<booktitles> <title>T1</title><authors>2</authors> <title>T2</title><authors>1</authors></booktitles>
Ergebnis
22
Aggregatfunktionen
<adressliste> { for $o in fn:distinct-values(doc("adressen.xml")//ort) let $ort:=doc("adressen.xml")//ort[self::node()=$o] where fn:count($ort)>=2 order by $o return <ortsliste> <ort>{($ort[1]/name,$ort[1]/plz)}</ort> <anzahl>{count($ort)}</anzahl></ortsliste>} </adressliste>
Beispiel: Gegeben Adressdatenbank, berechne Orte und Zahl der Adressen für Orte mit mind. 2 Adressen
adressen.xml
<adressliste> <adresse> <ort> <name>SB</name> <plz>66121</plz> </ort> <strasse>S1</strasse> </adresse> <adresse> <ort> <name>SB</name> <plz>66121</plz> </ort> <strasse>S2</strasse> </adresse> …</adressliste>
ErgebnisSB66121, NK66538, …
23
Datentransformation
XML: Generalisierung von HTML XQuery ermöglicht Anfragen auf XML-
Dokumente und direkte Ausgabe von HTML
XML und XQuery in Web-Szenarien gut geeignet
24
Datentransformation<bib> <book> <title>T1</title> <author>A1</author> <price>50</price> </book> <book> <title>T2</title> <author>A2</author> <price>20</price> </book> <article> <title>T3</title> <author>A3</author> </article></bib>
bookstore.xml
<html> <head> <title>Angebot</title> </head> <body> <h1>Buchliste</h1> <table border="1">{ for $book in doc("bookstore.xml")//book return <tr> <td><b>{$book/title/text()}</b></td> <td>{$book/author/text()}</td> </tr> }</table> </body></html>
XQuery
25
Datentransformation<html> <head> <title>Angebot</title> </head> <body> <h1>Buchliste</h1> <table border=1> <tr> <td><b>T1</b></td> <td>A1</td> </tr> <tr> <td><b>T2</b></td> <td>A2</td> </tr> </table> </body></html>
Ergebnis
26
Vielfalt an Konstrukten
FLWOR – Expressions Aggregationen Typkonversionen (nicht besprochen) Gleichheit von Knoten (Benutzerdefinierte) Funktionen
Sehr komplex, effiziente Auswertung von XQuery noch immer ein aktives Forschungsgebiet
27
XQ – ein XQuery Fragment
query ::= Ɛ | <a>query</a> | query query
| var | var/axis::v
| for var in var/axis::v return query
| if cond then query else query
cond ::= var = var | var = <a/> | true
| some var in var/axis::v satisfies cond
| cond and cond | cond or cond | not cond
Syntax Composition-free XQ
28
Composition-free XQuery
book
bib
book
<books> { let $x := <booklist>{ for $book in /bib/book return <b>{ $book } </b> }</booklist> for $b in $x/booklist/b return $b/* } </books>
XQuery
29
Composition-free XQuery
book
bib
book book
booklist
book
b b
$x$book
<books> { let $x := <booklist>{ for $book in /bib/book return <b>{ $book } </b> }</booklist> for $b in $x/booklist/b return $b/* } </books>
neu konstruiert
XQuery
30
Composition-free XQuery
book
bib
book book
booklist
book
b b
$x
book
books
book
$book $b
<books> { let $x := <booklist>{ for $book in /bib/book return <b>{ $book } </b> }</booklist> for $b in $x/booklist/b return $b/* } </books>
XQuery
31
Composition-free XQuery
in der Praxis: viele XQuery Anfragen mit Komposition
können in äquivalente XQ-Anfragenumgeschrieben werden
<books> { let $x := <booklist>{ for $book in /bib/book return <b>{ $book } </b> }</booklist> for $b in $x/booklist/b return $b/* } </books>
<books>{
for $book in /bib/book return $book
}</books>
XQuery
XQ query
32
Eigenschaft von XQ
niedrigere Komplexität in der Auswertung
Variablen binden immer an einen Knoten im Eingabebaum
<r>{
for $x in //x return
for $y in $x//y return $y}</r>
x
x
y y
$x
r
Ergebnis
<r>
XQ expression
33
Eigenschaft von XQ
niedrigere Komplexität in der Auswertung
Variablen binden immer an einen Knoten im Eingabebaum
x
x
y y
$x
$y
r
Ergebnis
<r><y/>
<r>{
for $x in //x return
for $y in $x//y return $y}</r>
XQ expression
34
Eigenschaft von XQ
niedrigere Komplexität in der Auswertung
Variablen binden immer an einen Knoten im Eingabebaum
x
x
y y
$x
$y
r
Ergebnis
<r><y/><y/>
<r>{
for $x in //x return
for $y in $x//y return $y}</r>
XQ expression
35
Eigenschaft von XQ
niedrigere Komplexität in der Auswertung
Variablen binden immer an einen Knoten im Eingabebaum
x
x
y y
$x
r
Ergebnis
<r><y/><y/>
<r>{
for $x in //x return
for $y in $x//y return $y}</r>
XQ expression
36
Eigenschaft von XQ
niedrigere Komplexität in der Auswertung
Variablen binden immer an einen Knoten im Eingabebaum
x
x
y y$y
r
$x
Ergebnis
<r><y/><y/><y/>
<r>{
for $x in //x return
for $y in $x//y return $y}</r>
XQ expression
37
Eigenschaft von XQ
niedrigere Komplexität in der Auswertung
Variablen binden immer an einen Knoten im Eingabebaum
x
x
y y
r
$x
$y
<r><y/><y/><y/><y/><r/>
Ergebnis
<r>{
for $x in //x return
for $y in $x//y return $y}</r>
XQ expression
38
XQ formale Semantik
k: Anzahl gebundener Variablen
e: derzeitiges environment (Variablenbindung)
“false” als leere Liste
Ergebnis: Liste von Knoten
39
XQ formale Semantik
40
XQ formale Semantik
41
XQ Evaluierung
[[<r>{ $x2 $x1}</r>]]2 (<a/>,<b/>)
= [<r> [[$x2 $x1]]2 (<a/>,<b/>) </r>]
= [<r> [[$x2]]2 (<a/>,<b/>) [[$x1]]2 (<a/>,<b/>) </r>]
= [<r> <b/> <a/> </r>]
42
XQ Evaluierung
<bt>{ for $book in //book return if (some $author in $book/author satisfies $author/text()=“A1”) then $book else ()}</bt>
bookstore.xml
<bib> <book> <title>T1</title> <author>A1</author> </book> <book> <title>T2</title> <author>A2</author> </book></bib>
XQuery
43
XQ Evaluierung
[[<bt>{ for $book in //book return if (some $author in $book/author satisfies $author/text()=“A1”) then $book else ()}</bt>]]0 ()
44
XQ Evaluierung
[[<bt>{ for $book in //book return if (some $author in $book/author satisfies $author/text()=“A1”) then $book else ()}</bt>]]0 ()
[[for $book in //book return if (some $author in $book/author satisfies $author/text()=“A1”) then $book else ()]]0 ()
45
XQ Evaluierung
[[<bt>{ for $book in //book return if (some $author in $book/author satisfies $author/text()=“A1”) then $book else ()}</bt>]]0 ()
[[for $book in //book return if (some $author in $book/author satisfies $author/text()=“A1”) then $book else ()]]0 ()
l=[[//book]]0 () = [<book><author>A1</author><title>T1</title></book>,
<book><author>A2</author><title>T2</title></book>]
46
XQ Evaluierung
[[<bt>{ for $book in //book return if (some $author in $book/author satisfies $author/text()=“A1”) then $book else ()}</bt>]]0 ()
[[for $book in //book return if (some $author in $book/author satisfies $author/text()=“A1”) then $book else ()]]0 ()
l=[[//book]]0 () = [<book><author>A1</author><title>T1</title></book>,
<book><author>A2</author><title>T2</title></book>]
[[if (some …)]]1 (l1) + [[if (some …)]]1 (l2)
47
XQ Evaluierung
[[if (some $author in $book/author satisfies $author/text()=“A1”) then $book else ()]]1
(<book><author>A1</author><title>T1</title></book>)
[[<bt>{ for $book in //book return if (some $author in $book/author satisfies $author/text()=“A1”) then $book else ()}</bt>]]0 ()
[[for $book in //book return if (some $author in $book/author satisfies $author/text()=“A1”) then $book else ()]]0 ()
l=[[//book]]0 () = [<book><author>A1</author><title>T1</title></book>,
<book><author>A2</author><title>T2</title></book>]
[[if (some …)]]1 (l1) + [[if (some …)]]1 (l2)
48
XQ Evaluierung
[[for $author in $book/author return $author/text()=“A1”]]1 (<book><author>A1</author><title>T1</title></book>)
[[if (some $author in $book/author satisfies $author/text()=“A1”) then $book else ()]]1
(<book><author>A1</author><title>T1</title></book>)
[[<bt>{ for $book in //book return if (some $author in $book/author satisfies $author/text()=“A1”) then $book else ()}</bt>]]0 ()
[[for $book in //book return if (some $author in $book/author satisfies $author/text()=“A1”) then $book else ()]]0 ()
l=[[//book]]0 () = [<book><author>A1</author><title>T1</title></book>,
<book><author>A2</author><title>T2</title></book>]
[[if (some …)]]1 (l1) + [[if (some …)]]1 (l2)
49
XQ Evaluierung
l =[[$book/author]]1 (<book><author>A1</author> <title>T1</title></book>) =[<author>A1</author>]
[[for $author in $book/author return $author/text()=“A1”]]1 (<book><author>A1</author><title>T1</title></book>)
[[<bt>{ for $book in //book return if (some $author in $book/author satisfies $author/text()=“A1”) then $book else ()}</bt>]]0 ()
[[for $book in //book return if (some $author in $book/author satisfies $author/text()=“A1”) then $book else ()]]0 ()
l=[[//book]]0 () = [<book><author>A1</author><title>T1</title></book>,
<book><author>A2</author><title>T2</title></book>]
[[if (some …)]]1 (l2) + [[if (some …)]]1 (l1)
[[if (some $author in $book/author satisfies $author/text()=“A1”) then $book else ()]]1
(<book><author>A1</author><title>T1</title></book>)
[[$author/text()=“A1”]]2
(<book>…</book>, <author>A1</author>)
50
XQ Evaluierung
[[$author/text()=“A1”]]2
(<book>…</book>, <author>A1</author>) [<yes/>]
l =[[$book/author]]1 (<book><author>A1</author> <title>T1</title></book>) =[<author>A1</author>]
[[for $author in $book/author return $author/text()=“A1”]]1 (<book><author>A1</author><title>T1</title></book>)
[[if (some $author in $book/author satisfies $author/text()=“A1”) then $book else ()]]1
(<book><author>A1</author><title>T1</title></book>)
[[<bt>{ for $book in //book return if (some $author in $book/author satisfies $author/text()=“A1”) then $book else ()}</bt>]]0 ()
[[for $book in //book return if (some $author in $book/author satisfies $author/text()=“A1”) then $book else ()]]0 ()
l=[[//book]]0 () = [<book><author>A1</author><title>T1</title></book>,
<book><author>A2</author><title>T2</title></book>]
[[if (some …)]]1 (l1) + [[if (some …)]]1 (l2)
[[$author/text()]]2 (…) [“A1”]
[[“A1”]]2 (…) [“A1”]
51
XQ Evaluierung
[[$author/text()=“A1”]]2
(<book>…</book>, <author>A1</author>) [<yes/>]
l =[[$book/author]]1 (<book><author>A1</author> <title>T1</title></book>) =[<author>A1</author>]
[[for $author in $book/author return $author/text()=“A1”]]1 (<book><author>A1</author><title>T1</title></book>) [<yes/>]
[[if (some $author in $book/author satisfies $author/text()=“A1”) then $book else ()]]1
(<book><author>A1</author><title>T1</title></book>)
[[<bt>{ for $book in //book return if (some $author in $book/author satisfies $author/text()=“A1”) then $book else ()}</bt>]]0 ()
[[for $book in //book return if (some $author in $book/author satisfies $author/text()=“A1”) then $book else ()]]0 ()
l=[[//book]]0 () = [<book><author>A1</author><title>T1</title></book>,
<book><author>A2</author><title>T2</title></book>]
[[if (some …)]]1 (l1) + [[if (some …)]]1 (l2)
52
XQ Evaluierung
[[$book]]1
(<book><author>A1</author><title>T1</title></book>)
[<book><author>A1</author><title>T1</title></book>]
[[$author/text()=“A1”]]2
(<book>…</book>, <author>A1</author>) [<yes/>]
l =[[$book/author]]1 (<book><author>A1</author> <title>T1</title></book>) =[<author>A1</author>]
[[for $author in $book/author return $author/text()=“A1”]]1 (<book><author>A1</author><title>T1</title></book>) [<yes/>][[if (some $author in $book/author satisfies $author/text()=“A1”) then $book else ()]]1
(<book><author>A1</author><title>T1</title></book>) [<book><author>A1</author><title>T1</title></book>]
[[<bt>{ for $book in //book return if (some $author in $book/author satisfies $author/text()=“A1”) then $book else ()}</bt>]]0 ()
[[for $book in //book return if (some $author in $book/author satisfies $author/text()=“A1”) then $book else ()]]0 ()
l=[[//book]]0 () = [<book><author>A1</author><title>T1</title></book>,
<book><author>A2</author><title>T2</title></book>]
[[if (some …)]]1 (l1) + [[if (some …)]]1 (l2)[<book><author>A1</author><title>T1</title></book>] +
53
XQ Evaluierung
[[<bt>{ for $book in //book return if (some $author in $book/author satisfies $author/text()=“A1”) then $book else ()}</bt>]]0 ()
[[for $book in //book return if (some $author in $book/author satisfies $author/text()=“A1”) then $book else ()]]0 ()
l=[[//book]]0 () = [<book><author>A1</author><title>T1</title></book>,
<book><author>A2</author><title>T2</title></book>]
[[if (some …)]]1 (l1) + [[if (some …)]]1 (l2)[<book><author>A1</author><title>T1</title></book>] +
[[if (some $author in $book/author satisfies $author/text()=“A1”) then $book else ()]]1
(<book><author>A2</author><title>T2</title></book>)
54
XQ Evaluierung
[[<bt>{ for $book in //book return if (some $author in $book/author satisfies $author/text()=“A1”) then $book else ()}</bt>]]0 ()
[[for $book in //book return if (some $author in $book/author satisfies $author/text()=“A1”) then $book else ()]]0 ()
l=[[//book]]0 ()= [<book><author>A1</author><title>T1</title></book>,
<book><author>A2</author><title>T2</title></book>]
[[if (some …)]]1 (l0) + [[if (some …)]]1 (l1)[<book><author>A1</author><title>T1</title></book>] +
[[if (some $author in $book/author satisfies $author/text()=“A1”) then $book else ()]]1
(<book><author>A2</author><title>T2</title></book>)
[[for $author in $book/author return $author/text()=“A1”]]1 (<book><author>A2</author><title>T2</title></book>)
55
XQ Evaluierung
[[<bt>{ for $book in //book return if (some $author in $book/author satisfies $author/text()=“A1”) then $book else ()}</bt>]]0 ()
[[for $book in //book return if (some $author in $book/author satisfies $author/text()=“A1”) then $book else ()]]0 ()
l=[[//book]]0 ()= [<book><author>A1</author><title>T1</title></book>,
<book><author>A2</author><title>T2</title></book>]
[[if (some …)]]1 (l1) + [[if (some …)]]1 (l2)[<book><author>A1</author><title>T1</title></book>] +
[[if (some $author in $book/author satisfies $author/text()=“A1”) then $book else ()]]1
(<book><author>A2</author><title>T2</title></book>)
[[for $author in $book/author return $author/text()=“A1”]]1 (<book><author>A2</author><title>T2</title></book>)
l =[[$book/author]]1 (<book><author>A2</author> <title>T2</title></book>) =[<author>A2</author>]
[[$author/text()=“A1”]]2
(<book>…</book>, <author>A2</author>)
56
XQ Evaluierung
[[<bt>{ for $book in //book return if (some $author in $book/author satisfies $author/text()=“A1”) then $book else ()}</bt>]]0 ()
[[for $book in //book return if (some $author in $book/author satisfies $author/text()=“A1”) then $book else ()]]0 ()
l=[[//book]]0 () = [<book><author>A1</author><title>T1</title></book>,
<book><author>A2</author><title>T2</title></book>]
[[if (some …)]]1 (l1) + [[if (some …)]]1 (l2)[<book><author>A1</author><title>T1</title></book>] +
[[if (some $author in $book/author satisfies $author/text()=“A1”) then $book else ()]]1
(<book><author>A2</author><title>T2</title></book>)
[[for $author in $book/author return $author/text()=“A1”]]1 (<book><author>A2</author><title>T2</title></book>)
l =[[$book/author]]1 (<book><author>A2</author> <title>T2</title></book>) =[<author>A2</author>]
[[$author/text()=“A1”]]2
(<book>…</book>, <author>A2</author>) []
[[$author/text()]]2 (…) [“A2”]
[[“A1”]]2 (…) [“A1”]
57
XQ Evaluierung
[[<bt>{ for $book in //book return if (some $author in $book/author satisfies $author/text()=“A1”) then $book else ()}</bt>]]0 ()
[[for $book in //book return if (some $author in $book/author satisfies $author/text()=“A1”) then $book else ()]]0 ()
l=[[//book]]0 () = [<book><author>A1</author><title>T1</title></book>,
<book><author>A2</author><title>T2</title></book>]
[[if (some …)]]1 (l1) + [[if (some …)]]1 (l2)[<book><author>A1</author><title>T1</title></book>] +
[[if (some $author in $book/author satisfies $author/text()=“A1”) then $book else ()]]1
(<book><author>A2</author><title>T2</title></book>)
[[for $author in $book/author return $author/text()=“A1”]]1 (<book><author>A2</author><title>T2</title></book>) []
l =[[$book/author]]1 (<book><author>A2</author> <title>T2</title></book>) =[<author>A2</author>]
[[$author/text()=“A1”]]2
(<book>…</book>, <author>A2</author>) []
58
XQ Evaluierung
[[<bt>{ for $book in //book return if (some $author in $book/author satisfies $author/text()=“A1”) then $book else ()}</bt>]]0 ()
[[for $book in //book return if (some $author in $book/author satisfies $author/text()=“A1”) then $book else ()]]0 ()
l=[[//book]]0 () = [<book><author>A1</author><title>T1</title></book>,
<book><author>A2</author><title>T2</title></book>]
[[if (some …)]]1 (l1) + [[if (some …)]]1 (l2)[<book><author>A1</author><title>T1</title></book>] +
[[if (some $author in $book/author satisfies $author/text()=“A1”) then $book else ()]]1
(<book><author>A2</author><title>T2</title></book>) []
[[for $author in $book/author return $author/text()=“A1”]]1 (<book><author>A2</author><title>T2</title></book>) []
l =[[$book/author]]1 (<book><author>A2</author> <title>T2</title></book>) =[<author>A2</author>]
[[$author/text()=“A1”]]2
(<book>…</book>, <author>A2</author>) []
[[()]]1 (<book><author>A2</author><title>T2</title</book>) []
[]
59
XQ Evaluierung
[[<bt>{ for $book in //book return if (some $author in $book/author satisfies $author/text()=“A1”) then $book else ()}</bt>]]0 ()
[[for $book in //book return if (some $author in $book/author satisfies $author/text()=“A1”) then $book else ()]]0 () [<book><author>A1</author><title>T1</title></book>]
l=[[//book]]0 () = [<book><author>A1</author><title>T1</title></book>,
<book><author>A2</author><title>T2</title></book>]
[[if (some …)]]1 (l1) + [[if (some …)]]1 (l2)[<book><author>A1</author><title>T1</title></book>] + []
[[if (some $author in $book/author satisfies $author/text()=“A1”) then $book else ()]]1
(<book><author>A2</author><title>T2</title></book>) []
[[for $author in $book/author return $author/text()=“A1”]]1 (<book><author>A2</author><title>T2</title></book>) []
l =[[$book/author]]1 (<book><author>A2</author> <title>T2</title></book>) =[<author>A2</author>]
[[$author/text()=“A1”]]2
(<book>…</book>, <author>A2</author>) []
[[()]]1 (<book><author>A2</author><title>T2</title</book>) []
60
XQ Evaluierung
[[<bt>{ for $book in //book return if (some $author in $book/author satisfies $author/text()=“A1”) then $book else ()}</bt>]]0 () [<bt><book><author>A1</author><title>T1</title></book></bt>]
[[for $book in //book return if (some $author in $book/author satisfies $author/text()=“A1”) then $book else ()]]0 () [<book><author>A1</author><title>T1</title></book>]
l=[[//book]]0 () = [<book><author>A1</author><title>T1</title></book>,
<book><author>A2</author><title>T2</title></book>]
[[if (some …)]]1 (l1) + [[if (some …)]]1 (l2)[<book><author>A1</author><title>T1</title></book>] + []
[[if (some $author in $book/author satisfies $author/text()=“A1”) then $book else ()]]1
(<book><author>A2</author><title>T2</title></book>) []
[[for $author in $book/author return $author/text()=“A1”]]1 (<book><author>A2</author><title>T2</title></book>) []
l =[[$book/author]]1 (<book><author>A2</author> <title>T2</title></book>) =[<author>A2</author>]
[[$author/text()=“A1”]]2
(<book>…</book>, <author>A2</author>) []
[[()]]1 (<book><author>A2</author><title>T2</title</book>) []
61
XQ: 2 Arten von Gleichheit Deep Equality
Vergleiche Bäume als Werte
z.B.: n1 deep-equal n2
n2 deep-equal n7 n3 =deep-equal n10
n4 =deep-equal n11
n4 deep-equal n10
Atomic EqualityVergleiche Blätter als Werte
z.B.: n4 =atomic n11
n4 atomic n9
n1: people
n2: person
n4: Karl n6: Auer
n7: person
n5: lnn3: fn
n9: Auer n11: Karl
n10: fnn8: ln
62
XQuery – Komplexere Beispiele
<bib> { for $b in doc("doc.xml")//book where fn:count($b/author) > 0 return <book> { $b/title } { for $a in $b/author[position()<=2] return $a } { if (fn:count($b/author) > 2) then <et-al/> else () } </book>} </bib>
Für jedes Buch mit mindestens einem Autor, gib den Titel und die ersten beiden Autoren aus und ein leeres „et-al“-Element wenn das Buch weitere Autoren besitzt
XQuery
63
XQuery – Komplexere BeispieleFinde den minimalen Preis für jedes Buch und gib ihn in der Form eines „minprice“-Elements mit dem Titel des Buchs als „title“-Attribut aus.
<results> { let $doc := doc("doc.xml") for $t in fn:distinct-values($doc//book/title) let $p := $doc//book[title = $t]/price return <minprice title="{ $t }"> <price>{ fn:min($p) }</price> </minprice>} </results>
XQuery
64
XQuery – Komplexere BeispieleGib alle Paare von Büchern aus, die unterschiedliche Titel aber die
selben Autoren (möglicherweise in unterschiedlicher Reihenfolge) haben.
<bib> { for $book1 in doc("doc.xml")//book, $book2 in doc("doc.xml")//book let $aut1:=for $a in $book1/author order by $a/last, $a/first return $a let $aut2 := for $a in $book2/author order by $a/last, $a/first return $a where $book1 << $book2 and not($book1/title = $book2/title) and fn:deep-equal($aut1, $aut2) return <book-pair> { $book1/title } { $book2/title } </book-pair>} </bib>
XQuery
Annahme:
Autoren haben die Form
<author>
<first>FIRSTNAME</first>
<last>LASTNAME</last>
</author><<: „vor“
65
Literatur und Links
Buch „XQuery from the Experts“, Addison Wesley (Kapitel 1)(Howard Katz, Don Chamberlin, Denise Draper, Mary Fernandez, Michael Kay, Jonathan Robie, Michael Rys, Jerome Simeon, Jim Tivy, Philip Wadler)http://www.datadirect.com/developer/xml/xquery/docs/katz_c01.pdf
XQuery Tutorial:http://www.w3schools.com/xquery/default.asp
W3C XQuery:http://www.w3.org/TR/2007/REC-xquery-20070123/
W3C XQuery Functions: http://www.w3.org/TR/xpath-functions/
Galax XQuery Engine (installiert in CIP-Pools):http://www.galaxquery.org/