1 xquery informationssysteme, 14.06.2007 vortragender: michael schmidt

65
1 XQuery Informationssysteme, 14.06.2007 Vortragender: Michael Schmidt

Upload: jannike-wickland

Post on 05-Apr-2015

108 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 1 XQuery Informationssysteme, 14.06.2007 Vortragender: Michael Schmidt

1

XQuery

Informationssysteme, 14.06.2007

Vortragender: Michael Schmidt

Page 2: 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

Page 3: 1 XQuery Informationssysteme, 14.06.2007 Vortragender: Michael Schmidt

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/...

Page 4: 1 XQuery Informationssysteme, 14.06.2007 Vortragender: Michael Schmidt

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)

Page 5: 1 XQuery Informationssysteme, 14.06.2007 Vortragender: Michael Schmidt

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

Page 6: 1 XQuery Informationssysteme, 14.06.2007 Vortragender: Michael Schmidt

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

Page 7: 1 XQuery Informationssysteme, 14.06.2007 Vortragender: Michael Schmidt

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!

Page 8: 1 XQuery Informationssysteme, 14.06.2007 Vortragender: Michael Schmidt

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

Page 9: 1 XQuery Informationssysteme, 14.06.2007 Vortragender: Michael Schmidt

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

Page 10: 1 XQuery Informationssysteme, 14.06.2007 Vortragender: Michael Schmidt

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

Page 11: 1 XQuery Informationssysteme, 14.06.2007 Vortragender: Michael Schmidt

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

Page 12: 1 XQuery Informationssysteme, 14.06.2007 Vortragender: Michael Schmidt

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

Page 13: 1 XQuery Informationssysteme, 14.06.2007 Vortragender: Michael Schmidt

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>

Page 14: 1 XQuery Informationssysteme, 14.06.2007 Vortragender: Michael Schmidt

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

Page 15: 1 XQuery Informationssysteme, 14.06.2007 Vortragender: Michael Schmidt

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

Page 16: 1 XQuery Informationssysteme, 14.06.2007 Vortragender: Michael Schmidt

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() …

Page 17: 1 XQuery Informationssysteme, 14.06.2007 Vortragender: Michael Schmidt

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

Page 18: 1 XQuery Informationssysteme, 14.06.2007 Vortragender: Michael Schmidt

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

Page 19: 1 XQuery Informationssysteme, 14.06.2007 Vortragender: Michael Schmidt

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

Page 20: 1 XQuery Informationssysteme, 14.06.2007 Vortragender: Michael Schmidt

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

Page 21: 1 XQuery Informationssysteme, 14.06.2007 Vortragender: Michael Schmidt

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

Page 22: 1 XQuery Informationssysteme, 14.06.2007 Vortragender: Michael Schmidt

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, …

Page 23: 1 XQuery Informationssysteme, 14.06.2007 Vortragender: Michael Schmidt

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

Page 24: 1 XQuery Informationssysteme, 14.06.2007 Vortragender: Michael Schmidt

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

Page 25: 1 XQuery Informationssysteme, 14.06.2007 Vortragender: Michael Schmidt

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

Page 26: 1 XQuery Informationssysteme, 14.06.2007 Vortragender: Michael Schmidt

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

Page 27: 1 XQuery Informationssysteme, 14.06.2007 Vortragender: Michael Schmidt

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

Page 28: 1 XQuery Informationssysteme, 14.06.2007 Vortragender: Michael Schmidt

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

Page 29: 1 XQuery Informationssysteme, 14.06.2007 Vortragender: Michael Schmidt

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

Page 30: 1 XQuery Informationssysteme, 14.06.2007 Vortragender: Michael Schmidt

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

Page 31: 1 XQuery Informationssysteme, 14.06.2007 Vortragender: Michael Schmidt

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

Page 32: 1 XQuery Informationssysteme, 14.06.2007 Vortragender: Michael Schmidt

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

Page 33: 1 XQuery Informationssysteme, 14.06.2007 Vortragender: Michael Schmidt

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

Page 34: 1 XQuery Informationssysteme, 14.06.2007 Vortragender: Michael Schmidt

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

Page 35: 1 XQuery Informationssysteme, 14.06.2007 Vortragender: Michael Schmidt

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

Page 36: 1 XQuery Informationssysteme, 14.06.2007 Vortragender: Michael Schmidt

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

Page 37: 1 XQuery Informationssysteme, 14.06.2007 Vortragender: Michael Schmidt

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

Page 38: 1 XQuery Informationssysteme, 14.06.2007 Vortragender: Michael Schmidt

38

XQ formale Semantik

k: Anzahl gebundener Variablen

e: derzeitiges environment (Variablenbindung)

“false” als leere Liste

Ergebnis: Liste von Knoten

Page 39: 1 XQuery Informationssysteme, 14.06.2007 Vortragender: Michael Schmidt

39

XQ formale Semantik

Page 40: 1 XQuery Informationssysteme, 14.06.2007 Vortragender: Michael Schmidt

40

XQ formale Semantik

Page 41: 1 XQuery Informationssysteme, 14.06.2007 Vortragender: Michael Schmidt

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>]

Page 42: 1 XQuery Informationssysteme, 14.06.2007 Vortragender: Michael Schmidt

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

Page 43: 1 XQuery Informationssysteme, 14.06.2007 Vortragender: Michael Schmidt

43

XQ Evaluierung

[[<bt>{ for $book in //book return if (some $author in $book/author satisfies $author/text()=“A1”) then $book else ()}</bt>]]0 ()

Page 44: 1 XQuery Informationssysteme, 14.06.2007 Vortragender: Michael Schmidt

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 ()

Page 45: 1 XQuery Informationssysteme, 14.06.2007 Vortragender: Michael Schmidt

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>]

Page 46: 1 XQuery Informationssysteme, 14.06.2007 Vortragender: Michael Schmidt

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)

Page 47: 1 XQuery Informationssysteme, 14.06.2007 Vortragender: Michael Schmidt

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)

Page 48: 1 XQuery Informationssysteme, 14.06.2007 Vortragender: Michael Schmidt

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)

Page 49: 1 XQuery Informationssysteme, 14.06.2007 Vortragender: Michael Schmidt

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>)

Page 50: 1 XQuery Informationssysteme, 14.06.2007 Vortragender: Michael Schmidt

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”]

Page 51: 1 XQuery Informationssysteme, 14.06.2007 Vortragender: Michael Schmidt

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)

Page 52: 1 XQuery Informationssysteme, 14.06.2007 Vortragender: Michael Schmidt

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>] +

Page 53: 1 XQuery Informationssysteme, 14.06.2007 Vortragender: Michael Schmidt

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>)

Page 54: 1 XQuery Informationssysteme, 14.06.2007 Vortragender: Michael Schmidt

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>)

Page 55: 1 XQuery Informationssysteme, 14.06.2007 Vortragender: Michael Schmidt

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>)

Page 56: 1 XQuery Informationssysteme, 14.06.2007 Vortragender: Michael Schmidt

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”]

Page 57: 1 XQuery Informationssysteme, 14.06.2007 Vortragender: Michael Schmidt

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>) []

Page 58: 1 XQuery Informationssysteme, 14.06.2007 Vortragender: Michael Schmidt

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>) []

[]

Page 59: 1 XQuery Informationssysteme, 14.06.2007 Vortragender: Michael Schmidt

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>) []

Page 60: 1 XQuery Informationssysteme, 14.06.2007 Vortragender: Michael Schmidt

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>) []

Page 61: 1 XQuery Informationssysteme, 14.06.2007 Vortragender: Michael Schmidt

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

Page 62: 1 XQuery Informationssysteme, 14.06.2007 Vortragender: Michael Schmidt

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

Page 63: 1 XQuery Informationssysteme, 14.06.2007 Vortragender: Michael Schmidt

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

Page 64: 1 XQuery Informationssysteme, 14.06.2007 Vortragender: Michael Schmidt

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“

Page 65: 1 XQuery Informationssysteme, 14.06.2007 Vortragender: Michael Schmidt

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/