Xquery
This presentation is the property of its rightful owner.
Sponsored Links
1 / 65

XQuery PowerPoint PPT Presentation


  • 68 Views
  • Uploaded on
  • Presentation posted in: General

XQuery. Informationssysteme, 14.06.2007 Vortragender: Michael Schmidt. XPath (Wiederholung). Anfragesprache auf XML XPath-Expressions selektieren Knoten des Eingabe-Dokuments. bib. book. /bib//title. book. title. author. title. author. XPath (Wiederholung).

Download Presentation

XQuery

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Xquery

XQuery

Informationssysteme, 14.06.2007

Vortragender: Michael Schmidt


Xpath wiederholung

XPath (Wiederholung)

  • Anfragesprache auf XML

  • XPath-Expressions selektieren Knoten des Eingabe-Dokuments

bib

book

/bib//title

book

title

author

title

author


Xpath wiederholung1

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


Xquery1

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)


Node construction und konstanten

Node-Construction und Konstanten

XQuery

Ergebnis

<result>

<text>"Hello World"</text>

</result>

<result>

<text>"Hello World"</text>

</result>

XQuery

Ergebnis

<result>

<text>{"Hello World"}</text>

</result>

<result>

<text>Hello World</text>

</result>


Dokument zugriff

Dokument-Zugriff

bookstore.xml

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

XQuery

<books> {

doc("bookstore.xml")/bib/book[price>30]

} </books>

Ergebnis

<books>

<book>

<title>T1</title>

<author>A1</author>

<price>50</price>

</book>

</books>


Sequenz

Sequenz

bookstore.xml

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

XQuery

<titles> {

(

doc("bookstore.xml")/bib/article/title,

doc("bookstore.xml")/bib/book/title

)

} </titles>

Ordnung bleibt erhalten!

Ergebnis

<titles>

<title>T3</title>

<title>T1</title>

<title>T2</title>

</titles>


Fr expression

FR-Expression

bookstore.xml

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

XQuery

<booktitles> {

for $book in doc("bookstore.xml")//book

return $book/title

} </booktitles>

Ergebnis

<booktitles>

<title>T1</title>

<title>T2</title>

</booktitles>


F w r expressions

FWR-Expressions

bookstore.xml

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

XQuery

<booktitles> {

for $book in doc("bookstore.xml")//book

where $book/price>30

return <booktitle>{

$book/title/text()

}</booktitle>

} </booktitles>

Konstruktion von Knoten, die im Dokument nicht vorkommen

Ergebnis

<booktitles>

<booktitle>T1</booktitle>

</booktitles>


F l wr expressions

FLWR-Expressions

bookstore.xml

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

XQuery

<booktitles> {

for $book in doc("bookstore.xml")//book

let $booktitle := <bt>{ $book/title/text() }</bt>

where ($book/price>30)

return $booktitle

} </booktitles>

Ergebnis

<booktitles>

<bt>T1</bt>

</booktitles>


Flw o r expressions

FLWOR-Expressions

bookstore.xml

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

XQuery

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

Ergebnis

<booktitles>

<bt>T2</bt>

<bt>T1</bt>

</booktitles>


Where expressions

Where-Expressions

bookstore.xml

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

  • Konnektive and, or, fn:not

XQuery

<booktitles> {

for $book in doc("bookstore.xml")//book

where ($book/price>10 and

fn:not($book/author/text()="A1"))

return $book/title

} </booktitles>

Ergebnis

<booktitles>

<title>T2</title>

</booktitles>


Some expressions

Some-Expressions

XQuery

<booktitles> {

for $book in doc("bookstore.xml")//book

where (some $a in $book/author satisfies $a/text()="A2")

return $book/title

} </booktitles>

bookstore.xml

<bib>

<book>

<title>T1</title>

<author>A1</author>

<author>A2</author>

</book>

<book>

<title>T2</title>

<author>A3</author>

<author>A4</author> </book>

</bib>

Ergebnis

<booktitles>

<title>T1</title>

</booktitles>


For vs let expressions

For vs let-Expressions

Ergebnis

<for>

<test>1</test>

<test>2</test>

<test>3</test>

<test>4</test>

<test>5</test>

</for>

XQuery 1

<for>{

for $x in (1 to 5)

return <test> {$x} </test>

}</for>

Iteration

XQuery 2

Ergebnis

<let>{

let $x:=(1 to 5)

return <test> {$x} </test>

}</let>

<let>

<test>1 2 3 4 5</test>

</let>

keine Iteration


If then else expressions

If-then-else Expressions

XQuery

bookstore.xml

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

<bib>

<book>

<title>T1</title>

<author>A1</author>

</book>

<book>

<title>T2</title>

<author>A2</author>

<price>30</price> </book>

</bib>

Ergebnis

<booktitles>

<unpriced><title>T1</title></unpriced>

<priced><title>T2</title></priced>

</booktitles>


Vordefinierte funktionen

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


Vordefinierte funktionen1

Vordefinierte Funktionen

bookstore.xml

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

XQuery

<booktitles> {

for $book in doc("bookstore.xml")//book

where (fn:exists($book/price) and

fn:contains($book/author, "A"))

return $book/title

} </booktitles>

Ergebnis

<booktitles>

<title>T1</title>

<title>T2</title>

</booktitles>


Xquery joins

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


Xquery joins1

XQuery Joins

XQuery

Ergebnis

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

<titles>

<pair>

<title>T1</title>

<title>T4</title>

</pair>

</titles>

$article

$book

bib

article

article

book

book

title

author

title

author

title

author

title

author

T3

A3

T4

A1

T1

A1

T2

A2


Xquery joins2

XQuery Joins

  • Join über zwei Dokumente

XQuery

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


Aggregatfunktionen

Aggregatfunktionen

  • fn:min(), fn:max(), fn:count(), fn:sum(), fn:avg()

XQuery

bookstore.xml

<booktitles> {

for $book in doc("bookstore.xml")//book

return

{

($book/title,

<authors>{ fn:count($book/author) }</authors>)

}

} </booktitles>

<bib>

<book>

<title>T1</title>

<author>A1</author>

<author>A2</author>

</book>

<book>

<title>T2</title>

<author>A3</author> </book>

</bib>

Ergebnis

<booktitles>

<title>T1</title><authors>2</authors>

<title>T2</title><authors>1</authors>

</booktitles>


Aggregatfunktionen1

Aggregatfunktionen

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>

Beispiel: Gegeben Adressdatenbank, berechne Orte und Zahl der Adressen für Orte mit mind. 2 Adressen

Ergebnis

SB66121, NK66538, …

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


Datentransformation

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


Datentransformation1

Datentransformation

bookstore.xml

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

XQuery

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


Datentransformation2

Datentransformation

Ergebnis

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


Vielfalt an konstrukten

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


Xq ein xquery fragment

XQ – ein XQuery Fragment

Syntax Composition-free XQ

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


Composition free xquery

Composition-free XQuery

XQuery

<books> {

let $x := <booklist>{

for $book in /bib/book

return <b>{ $book } </b>

}</booklist>

for $b in $x/booklist/b return $b/*

} </books>

bib

book

book


Composition free xquery1

Composition-free XQuery

XQuery

<books> {

let $x := <booklist>{

for $book in /bib/book

return <b>{ $book } </b>

}</booklist>

for $b in $x/booklist/b return $b/*

} </books>

$x

booklist

$book

bib

b

b

book

book

book

book

neu konstruiert


Composition free xquery2

Composition-free XQuery

XQuery

<books> {

let $x := <booklist>{

for $book in /bib/book

return <b>{ $book } </b>

}</booklist>

for $b in $x/booklist/b return $b/*

} </books>

$x

booklist

$b

$book

bib

books

b

b

book

book

book

book

book

book


Composition free xquery3

Composition-free XQuery

XQuery

<books> {

let $x := <booklist>{

for $book in /bib/book

return <b>{ $book } </b>

}</booklist>

for $b in $x/booklist/b return $b/*

} </books>

in der Praxis:viele XQuery Anfragen mit Kompositionkönnen in äquivalente XQ-Anfragenumgeschrieben werden

<books>{

for $book in /bib/book return $book

}</books>

XQ query


Eigenschaft von xq

niedrigere Komplexität in der Auswertung

Variablen binden immer an einen Knoten im Eingabebaum

Eigenschaft von XQ

XQ expression

<r>{

for $x in //x return

for $y in $x//y return $y}</r>

r

$x

x

x

y

y

Ergebnis

<r>


Eigenschaft von xq1

niedrigere Komplexität in der Auswertung

Variablen binden immer an einen Knoten im Eingabebaum

Eigenschaft von XQ

XQ expression

<r>{

for $x in //x return

for $y in $x//y return $y}</r>

r

$x

x

x

$y

y

y

Ergebnis

<r><y/>


Eigenschaft von xq2

niedrigere Komplexität in der Auswertung

Variablen binden immer an einen Knoten im Eingabebaum

Eigenschaft von XQ

XQ expression

<r>{

for $x in //x return

for $y in $x//y return $y}</r>

r

$x

x

x

$y

y

y

Ergebnis

<r><y/><y/>


Eigenschaft von xq3

niedrigere Komplexität in der Auswertung

Variablen binden immer an einen Knoten im Eingabebaum

Eigenschaft von XQ

XQ expression

<r>{

for $x in //x return

for $y in $x//y return $y}</r>

r

$x

x

x

y

y

Ergebnis

<r><y/><y/>


Eigenschaft von xq4

niedrigere Komplexität in der Auswertung

Variablen binden immer an einen Knoten im Eingabebaum

Eigenschaft von XQ

XQ expression

<r>{

for $x in //x return

for $y in $x//y return $y}</r>

r

$x

x

x

$y

y

y

Ergebnis

<r><y/><y/><y/>


Eigenschaft von xq5

niedrigere Komplexität in der Auswertung

Variablen binden immer an einen Knoten im Eingabebaum

Eigenschaft von XQ

XQ expression

<r>{

for $x in //x return

for $y in $x//y return $y}</r>

r

$x

x

x

$y

y

y

Ergebnis

<r><y/><y/><y/><y/><r/>


Xq formale semantik

XQ formale Semantik

  • Ergebnis: Liste von Knoten

k: Anzahl gebundener Variablen

e: derzeitiges environment (Variablenbindung)

“false” als leere Liste


Xq formale semantik1

XQ formale Semantik


Xq formale semantik2

XQ formale Semantik


Xq evaluierung

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


Xq evaluierung1

XQ Evaluierung

bookstore.xml

<bib>

<book>

<title>T1</title>

<author>A1</author>

</book>

<book>

<title>T2</title>

<author>A2</author>

</book>

</bib>

XQuery

<bt>{

for $book in //book

return

if (some $author in $book/author

satisfies $author/text()=“A1”)

then $book

else ()

}</bt>


Xq evaluierung2

XQ Evaluierung

[[<bt>{ for $book in //book return

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


Xq evaluierung3

XQ Evaluierung

[[for $book in //book return

if (some $author in $book/author satisfies $author/text()=“A1”) then $book else ()]]0 ()

[[<bt>{ for $book in //book return

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


Xq evaluierung4

XQ Evaluierung

l=[[//book]]0 () = [<book><author>A1</author><title>T1</title></book>,

<book><author>A2</author><title>T2</title></book>]

[[for $book in //book return

if (some $author in $book/author satisfies $author/text()=“A1”) then $book else ()]]0 ()

[[<bt>{ for $book in //book return

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


Xq evaluierung5

XQ Evaluierung

[[if (some …)]]1 (l1) + [[if (some …)]]1 (l2)

l=[[//book]]0 () = [<book><author>A1</author><title>T1</title></book>,

<book><author>A2</author><title>T2</title></book>]

[[for $book in //book return

if (some $author in $book/author satisfies $author/text()=“A1”) then $book else ()]]0 ()

[[<bt>{ for $book in //book return

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


Xq evaluierung6

XQ Evaluierung

[[if (some $author in $book/author satisfies $author/text()=“A1”) then $book else ()]]1

(<book><author>A1</author><title>T1</title></book>)

[[if (some …)]]1 (l1) + [[if (some …)]]1 (l2)

l=[[//book]]0 () = [<book><author>A1</author><title>T1</title></book>,

<book><author>A2</author><title>T2</title></book>]

[[for $book in //book return

if (some $author in $book/author satisfies $author/text()=“A1”) then $book else ()]]0 ()

[[<bt>{ for $book in //book return

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


Xq evaluierung7

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

[[if (some …)]]1 (l1) + [[if (some …)]]1 (l2)

l=[[//book]]0 () = [<book><author>A1</author><title>T1</title></book>,

<book><author>A2</author><title>T2</title></book>]

[[for $book in //book return

if (some $author in $book/author satisfies $author/text()=“A1”) then $book else ()]]0 ()

[[<bt>{ for $book in //book return

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


Xq evaluierung8

XQ Evaluierung

l =[[$book/author]]1

(<book><author>A1</author>

<title>T1</title></book>)

=[<author>A1</author>]

[[$author/text()=“A1”]]2

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

[[if (some …)]]1 (l2) + [[if (some …)]]1 (l1)

l=[[//book]]0 () = [<book><author>A1</author><title>T1</title></book>,

<book><author>A2</author><title>T2</title></book>]

[[for $book in //book return

if (some $author in $book/author satisfies $author/text()=“A1”) then $book else ()]]0 ()

[[<bt>{ for $book in //book return

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


Xq evaluierung9

XQ Evaluierung

l =[[$book/author]]1

(<book><author>A1</author>

<title>T1</title></book>)

=[<author>A1</author>]

[[$author/text()=“A1”]]2

(<book>…</book>,

<author>A1</author>)

 [<yes/>]

[[$author/text()]]2 (…)  [“A1”]

[[“A1”]]2 (…)  [“A1”]

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

[[if (some …)]]1 (l1) + [[if (some …)]]1 (l2)

l=[[//book]]0 () = [<book><author>A1</author><title>T1</title></book>,

<book><author>A2</author><title>T2</title></book>]

[[for $book in //book return

if (some $author in $book/author satisfies $author/text()=“A1”) then $book else ()]]0 ()

[[<bt>{ for $book in //book return

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


Xq evaluierung10

XQ Evaluierung

l =[[$book/author]]1

(<book><author>A1</author>

<title>T1</title></book>)

=[<author>A1</author>]

[[$author/text()=“A1”]]2

(<book>…</book>,

<author>A1</author>)

 [<yes/>]

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

[[if (some …)]]1 (l1) + [[if (some …)]]1 (l2)

l=[[//book]]0 () = [<book><author>A1</author><title>T1</title></book>,

<book><author>A2</author><title>T2</title></book>]

[[for $book in //book return

if (some $author in $book/author satisfies $author/text()=“A1”) then $book else ()]]0 ()

[[<bt>{ for $book in //book return

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


Xq evaluierung11

XQ Evaluierung

l =[[$book/author]]1

(<book><author>A1</author>

<title>T1</title></book>)

=[<author>A1</author>]

[[$author/text()=“A1”]]2

(<book>…</book>,

<author>A1</author>)

 [<yes/>]

[[for $author in $book/author return $author/text()=“A1”]]1

(<book><author>A1</author><title>T1</title></book>)

 [<yes/>]

[[$book]]1

(<book><author>A1</author><title>T1</title></book>)

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

 [<book><author>A1</author><title>T1</title></book>]

[[if (some …)]]1 (l1) + [[if (some …)]]1 (l2)

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

[[for $book in //book return

if (some $author in $book/author satisfies $author/text()=“A1”) then $book else ()]]0 ()

[[<bt>{ for $book in //book return

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


Xq evaluierung12

XQ Evaluierung

[[if (some $author in $book/author satisfies $author/text()=“A1”) then $book else ()]]1

(<book><author>A2</author><title>T2</title></book>)

[[if (some …)]]1 (l1) + [[if (some …)]]1 (l2)

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

[[for $book in //book return

if (some $author in $book/author satisfies $author/text()=“A1”) then $book else ()]]0 ()

[[<bt>{ for $book in //book return

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


Xq evaluierung13

XQ Evaluierung

[[for $author in $book/author return $author/text()=“A1”]]1

(<book><author>A2</author><title>T2</title></book>)

[[if (some $author in $book/author satisfies $author/text()=“A1”) then $book else ()]]1

(<book><author>A2</author><title>T2</title></book>)

[[if (some …)]]1 (l0) + [[if (some …)]]1 (l1)

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

[[for $book in //book return

if (some $author in $book/author satisfies $author/text()=“A1”) then $book else ()]]0 ()

[[<bt>{ for $book in //book return

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


Xq evaluierung14

XQ Evaluierung

l =[[$book/author]]1

(<book><author>A2</author>

<title>T2</title></book>)

=[<author>A2</author>]

[[$author/text()=“A1”]]2

(<book>…</book>,

<author>A2</author>)

[[for $author in $book/author return $author/text()=“A1”]]1

(<book><author>A2</author><title>T2</title></book>)

[[if (some $author in $book/author satisfies $author/text()=“A1”) then $book else ()]]1

(<book><author>A2</author><title>T2</title></book>)

[[if (some …)]]1 (l1) + [[if (some …)]]1 (l2)

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

[[for $book in //book return

if (some $author in $book/author satisfies $author/text()=“A1”) then $book else ()]]0 ()

[[<bt>{ for $book in //book return

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


Xq evaluierung15

XQ Evaluierung

l =[[$book/author]]1

(<book><author>A2</author>

<title>T2</title></book>)

=[<author>A2</author>]

[[$author/text()]]2 (…)  [“A2”]

[[$author/text()=“A1”]]2

(<book>…</book>,

<author>A2</author>)

 []

[[“A1”]]2 (…)  [“A1”]

[[for $author in $book/author return $author/text()=“A1”]]1

(<book><author>A2</author><title>T2</title></book>)

[[if (some $author in $book/author satisfies $author/text()=“A1”) then $book else ()]]1

(<book><author>A2</author><title>T2</title></book>)

[[if (some …)]]1 (l1) + [[if (some …)]]1 (l2)

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

[[for $book in //book return

if (some $author in $book/author satisfies $author/text()=“A1”) then $book else ()]]0 ()

[[<bt>{ for $book in //book return

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


Xq evaluierung16

XQ Evaluierung

l =[[$book/author]]1

(<book><author>A2</author>

<title>T2</title></book>)

=[<author>A2</author>]

[[$author/text()=“A1”]]2

(<book>…</book>,

<author>A2</author>)

 []

[[for $author in $book/author return $author/text()=“A1”]]1

(<book><author>A2</author><title>T2</title></book>)

 []

[[if (some $author in $book/author satisfies $author/text()=“A1”) then $book else ()]]1

(<book><author>A2</author><title>T2</title></book>)

[[if (some …)]]1 (l1) + [[if (some …)]]1 (l2)

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

[[for $book in //book return

if (some $author in $book/author satisfies $author/text()=“A1”) then $book else ()]]0 ()

[[<bt>{ for $book in //book return

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


Xq evaluierung17

XQ Evaluierung

l =[[$book/author]]1

(<book><author>A2</author>

<title>T2</title></book>)

=[<author>A2</author>]

[[$author/text()=“A1”]]2

(<book>…</book>,

<author>A2</author>)

 []

[[for $author in $book/author return $author/text()=“A1”]]1

(<book><author>A2</author><title>T2</title></book>)

 []

[[()]]1 (<book><author>A2</author><title>T2</title</book>)  []

[[if (some $author in $book/author satisfies $author/text()=“A1”) then $book else ()]]1

(<book><author>A2</author><title>T2</title></book>)

 []

[[if (some …)]]1 (l1) + [[if (some …)]]1 (l2)

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

[]

[[for $book in //book return

if (some $author in $book/author satisfies $author/text()=“A1”) then $book else ()]]0 ()

[[<bt>{ for $book in //book return

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


Xq evaluierung18

XQ Evaluierung

l =[[$book/author]]1

(<book><author>A2</author>

<title>T2</title></book>)

=[<author>A2</author>]

[[$author/text()=“A1”]]2

(<book>…</book>,

<author>A2</author>)

 []

[[for $author in $book/author return $author/text()=“A1”]]1

(<book><author>A2</author><title>T2</title></book>)

 []

[[()]]1 (<book><author>A2</author><title>T2</title</book>)  []

[[if (some $author in $book/author satisfies $author/text()=“A1”) then $book else ()]]1

(<book><author>A2</author><title>T2</title></book>)

 []

[[if (some …)]]1 (l1) + [[if (some …)]]1 (l2)

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

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

[[<bt>{ for $book in //book return

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


Xq evaluierung19

XQ Evaluierung

l =[[$book/author]]1

(<book><author>A2</author>

<title>T2</title></book>)

=[<author>A2</author>]

[[$author/text()=“A1”]]2

(<book>…</book>,

<author>A2</author>)

 []

[[for $author in $book/author return $author/text()=“A1”]]1

(<book><author>A2</author><title>T2</title></book>)

 []

[[()]]1 (<book><author>A2</author><title>T2</title</book>)  []

[[if (some $author in $book/author satisfies $author/text()=“A1”) then $book else ()]]1

(<book><author>A2</author><title>T2</title></book>)

 []

[[if (some …)]]1 (l1) + [[if (some …)]]1 (l2)

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

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

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


Xq 2 arten von gleichheit

Deep EqualityVergleiche Bäume als Wertez.B.: n1 deep-equal n2 n2 deep-equal n7n3 =deep-equal n10 n4 =deep-equal n11n4 deep-equal n10

Atomic EqualityVergleiche Blätter als Wertez.B.: n4 =atomic n11n4 atomic n9

XQ: 2 Arten von Gleichheit

n1: people

n2: person

n7: person

n3: fn

n5: ln

n8: ln

n10: fn

n4: Karl

n6: Auer

n9: Auer

n11: Karl


Xquery komplexere beispiele

XQuery – Komplexere Beispiele

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

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


Xquery komplexere beispiele1

XQuery – Komplexere Beispiele

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

XQuery

<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 komplexere beispiele2

XQuery – Komplexere Beispiele

Gib alle Paare von Büchern aus, die unterschiedliche Titel aber die selben Autoren (möglicherweise in unterschiedlicher Reihenfolge) haben.

XQuery

Annahme:

Autoren haben die Form

<author>

<first>FIRSTNAME</first>

<last>LASTNAME</last>

</author>

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

<<: „vor“


Literatur und links

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/


  • Login