xquery
Download
Skip this Video
Download Presentation
XQuery

Loading in 2 Seconds...

play fullscreen
1 / 65

XQuery - PowerPoint PPT Presentation


  • 107 Views
  • Uploaded on

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

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'XQuery' - lilli


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

"Hello World"

"Hello World"

XQuery

Ergebnis

{"Hello World"}

Hello World

dokument zugriff
Dokument-Zugriff

bookstore.xml

T1

A1

50

T2

A2

20

T3

A3

XQuery

{

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

}

Ergebnis

T1

A1

50

sequenz
Sequenz

bookstore.xml

T1

A1

50

T2

A2

20

T3

A3

XQuery

{

(

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

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

)

}

Ordnung bleibt erhalten!

Ergebnis

T3

T1

T2

fr expression
FR-Expression

bookstore.xml

T1

A1

50

T2

A2

20

T3

A3

XQuery

{

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

return $book/title

}

Ergebnis

T1

T2

f w r expressions
FWR-Expressions

bookstore.xml

T1

A1

50

T2

A2

20

T3

A3

XQuery

{

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

where $book/price>30

return {

$book/title/text()

}

}

Konstruktion von Knoten, die im Dokument nicht vorkommen

Ergebnis

T1

f l wr expressions
FLWR-Expressions

bookstore.xml

T1

A1

50

T2

A2

20

T3

A3

XQuery

{

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

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

where ($book/price>30)

return $booktitle

}

Ergebnis

T1

flw o r expressions
FLWOR-Expressions

bookstore.xml

T1

A1

50

T2

A2

20

T3

A3

XQuery

{

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

let $booktitle := $book/title

where ($book/price>10)

order by $booktitle/text() descending

return { $booktitle/text() }

}

Ergebnis

T2

T1

where expressions
Where-Expressions

bookstore.xml

T1

A1

50

T2

A2

20

T3

A3

  • Konnektive and, or, fn:not

XQuery

{

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

where ($book/price>10 and

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

return $book/title

}

Ergebnis

T2

some expressions
Some-Expressions

XQuery

{

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

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

return $book/title

}

bookstore.xml

T1

A1

A2

T2

A3

A4

Ergebnis

T1

for vs let expressions
For vs let-Expressions

Ergebnis

1

2

3

4

5

XQuery 1

{

for $x in (1 to 5)

return {$x}

}

Iteration

XQuery 2

Ergebnis

{

let $x:=(1 to 5)

return {$x}

}

1 2 3 4 5

keine Iteration

if then else expressions
If-then-else Expressions

XQuery

bookstore.xml

{

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

return

if (fn:exists($book/price))

then { $book/title }

else { $book/title }

}

T1

A1

T2

A2

30

Ergebnis

T1

T2

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

T1

A1

50

T2

A2

20

T3

A3

XQuery

{

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

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

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

return $book/title

}

Ergebnis

T1

T2

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

{

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 { $book/title, $article/title}

}

T1

T4

$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

{

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

{ $b/title }

{ $a/price/text() }

{ $b/price/text() }

}

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

XQuery

bookstore.xml

{

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

return

{

($book/title,

{ fn:count($book/author) })

}

}

T1

A1

A2

T2

A3

Ergebnis

T12

T21

aggregatfunktionen1
Aggregatfunktionen

adressen.xml

SB

66121

S1

SB

66121

S2

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

Ergebnis

SB66121, NK66538, …

{

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

{($ort[1]/name,$ort[1]/plz)}

{count($ort)}

}

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

T1

A1

50

T2

A2

20

T3

A3

XQuery

Angebot

Buchliste

{

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

return

}

{$book/title/text()}{$book/author/text()}

datentransformation2
Datentransformation

Ergebnis

Angebot

Buchliste

T1A1
T2A2

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 ::= Ɛ | query | query query

| var | var/axis::v

| for var in var/axis::v return query

| if cond then query else query

cond ::= var = var | var = | true

| some var in var/axis::v satisfies cond

| cond and cond | cond or cond | not cond

composition free xquery
Composition-free XQuery

XQuery

{

let $x := {

for $book in /bib/book

return { $book }

}

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

}

bib

book

book

composition free xquery1
Composition-free XQuery

XQuery

{

let $x := {

for $book in /bib/book

return { $book }

}

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

}

$x

booklist

$book

bib

b

b

book

book

book

book

neu konstruiert

composition free xquery2
Composition-free XQuery

XQuery

{

let $x := {

for $book in /bib/book

return { $book }

}

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

}

$x

booklist

$b

$book

bib

books

b

b

book

book

book

book

book

book

composition free xquery3
Composition-free XQuery

XQuery

{

let $x := {

for $book in /bib/book

return { $book }

}

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

}

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

{

for $book in /bib/book return $book

}

XQ query

eigenschaft von xq
niedrigere Komplexität in der Auswertung

Variablen binden immer an einen Knoten im Eingabebaum

Eigenschaft von XQ

XQ expression

{

for $x in //x return

for $y in $x//y return $y}

r

$x

x

x

y

y

Ergebnis

eigenschaft von xq1
niedrigere Komplexität in der Auswertung

Variablen binden immer an einen Knoten im Eingabebaum

Eigenschaft von XQ

XQ expression

{

for $x in //x return

for $y in $x//y return $y}

r

$x

x

x

$y

y

y

Ergebnis

eigenschaft von xq2
niedrigere Komplexität in der Auswertung

Variablen binden immer an einen Knoten im Eingabebaum

Eigenschaft von XQ

XQ expression

{

for $x in //x return

for $y in $x//y return $y}

r

$x

x

x

$y

y

y

Ergebnis

eigenschaft von xq3
niedrigere Komplexität in der Auswertung

Variablen binden immer an einen Knoten im Eingabebaum

Eigenschaft von XQ

XQ expression

{

for $x in //x return

for $y in $x//y return $y}

r

$x

x

x

y

y

Ergebnis

eigenschaft von xq4
niedrigere Komplexität in der Auswertung

Variablen binden immer an einen Knoten im Eingabebaum

Eigenschaft von XQ

XQ expression

{

for $x in //x return

for $y in $x//y return $y}

r

$x

x

x

$y

y

y

Ergebnis

eigenschaft von xq5
niedrigere Komplexität in der Auswertung

Variablen binden immer an einen Knoten im Eingabebaum

Eigenschaft von XQ

XQ expression

{

for $x in //x return

for $y in $x//y return $y}

r

$x

x

x

$y

y

y

Ergebnis

xq formale semantik
XQ formale Semantik
  • Ergebnis: Liste von Knoten

k: Anzahl gebundener Variablen

e: derzeitiges environment (Variablenbindung)

“false” als leere Liste

xq evaluierung1
XQ Evaluierung

bookstore.xml

T1

A1

T2

A2

XQuery

{

for $book in //book

return

if (some $author in $book/author

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

then $book

else ()

}

xq evaluierung2
XQ Evaluierung

[[{ for $book in //book return

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

xq evaluierung3
XQ Evaluierung

[[for $book in //book return

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

[[{ for $book in //book return

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

xq evaluierung4
XQ Evaluierung

l=[[//book]]0 () = [A1T1,

A2T2]

[[for $book in //book return

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

[[{ for $book in //book return

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

xq evaluierung5
XQ Evaluierung

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

l=[[//book]]0 () = [A1T1,

A2T2]

[[for $book in //book return

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

[[{ for $book in //book return

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

xq evaluierung6
XQ Evaluierung

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

(A1T1)

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

l=[[//book]]0 () = [A1T1,

A2T2]

[[for $book in //book return

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

[[{ for $book in //book return

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

xq evaluierung7
XQ Evaluierung

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

(A1T1)

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

(A1T1)

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

l=[[//book]]0 () = [A1T1,

A2T2]

[[for $book in //book return

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

[[{ for $book in //book return

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

xq evaluierung8
XQ Evaluierung

l =[[$book/author]]1

(A1

T1)

=[A1]

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

(,

A1)

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

(A1T1)

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

(A1T1)

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

l=[[//book]]0 () = [A1T1,

A2T2]

[[for $book in //book return

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

[[{ for $book in //book return

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

xq evaluierung9
XQ Evaluierung

l =[[$book/author]]1

(A1

T1)

=[A1]

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

(,

A1)

 []

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

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

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

(A1T1)

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

(A1T1)

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

l=[[//book]]0 () = [A1T1,

A2T2]

[[for $book in //book return

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

[[{ for $book in //book return

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

xq evaluierung10
XQ Evaluierung

l =[[$book/author]]1

(A1

T1)

=[A1]

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

(,

A1)

 []

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

(A1T1)

 []

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

(A1T1)

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

l=[[//book]]0 () = [A1T1,

A2T2]

[[for $book in //book return

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

[[{ for $book in //book return

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

xq evaluierung11
XQ Evaluierung

l =[[$book/author]]1

(A1

T1)

=[A1]

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

(,

A1)

 []

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

(A1T1)

 []

[[$book]]1

(A1T1)

 [A1T1]

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

(A1T1)

 [A1T1]

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

[A1T1] +

l=[[//book]]0 () = [A1T1,

A2T2]

[[for $book in //book return

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

[[{ for $book in //book return

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

xq evaluierung12
XQ Evaluierung

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

(A2T2)

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

[A1T1] +

l=[[//book]]0 () = [A1T1,

A2T2]

[[for $book in //book return

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

[[{ for $book in //book return

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

xq evaluierung13
XQ Evaluierung

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

(A2T2)

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

(A2T2)

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

[A1T1] +

l=[[//book]]0 ()= [A1T1,

A2T2]

[[for $book in //book return

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

[[{ for $book in //book return

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

xq evaluierung14
XQ Evaluierung

l =[[$book/author]]1

(A2

T2)

=[A2]

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

(,

A2)

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

(A2T2)

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

(A2T2)

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

[A1T1] +

l=[[//book]]0 ()= [A1T1,

A2T2]

[[for $book in //book return

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

[[{ for $book in //book return

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

xq evaluierung15
XQ Evaluierung

l =[[$book/author]]1

(A2

T2)

=[A2]

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

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

(,

A2)

 []

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

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

(A2T2)

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

(A2T2)

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

[A1T1] +

l=[[//book]]0 () = [A1T1,

A2T2]

[[for $book in //book return

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

[[{ for $book in //book return

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

xq evaluierung16
XQ Evaluierung

l =[[$book/author]]1

(A2

T2)

=[A2]

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

(,

A2)

 []

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

(A2T2)

 []

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

(A2T2)

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

[A1T1] +

l=[[//book]]0 () = [A1T1,

A2T2]

[[for $book in //book return

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

[[{ for $book in //book return

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

xq evaluierung17
XQ Evaluierung

l =[[$book/author]]1

(A2

T2)

=[A2]

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

(,

A2)

 []

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

(A2T2)

 []

[[()]]1 (A2T2</title</book>)  []</p><p>[[if (some $author in $book/author satisfies $author/text()=“A1”) then $book else ()]]1</p><p> (<book><author>A2</author><title>T2)

 []

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

[A1T1] +

l=[[//book]]0 () = [A1T1,

A2T2]

[]

[[for $book in //book return

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

[[{ for $book in //book return

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

xq evaluierung18
XQ Evaluierung

l =[[$book/author]]1

(A2

T2)

=[A2]

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

(,

A2)

 []

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

(A2T2)

 []

[[()]]1 (A2T2</title</book>)  []</p><p>[[if (some $author in $book/author satisfies $author/text()=“A1”) then $book else ()]]1</p><p> (<book><author>A2</author><title>T2)

 []

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

[A1T1] + []

l=[[//book]]0 () = [A1T1,

A2T2]

[[for $book in //book return

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

 [A1T1]

[[{ for $book in //book return

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

xq evaluierung19
XQ Evaluierung

l =[[$book/author]]1

(A2

T2)

=[A2]

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

(,

A2)

 []

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

(A2T2)

 []

[[()]]1 (A2T2</title</book>)  []</p><p>[[if (some $author in $book/author satisfies $author/text()=“A1”) then $book else ()]]1</p><p> (<book><author>A2</author><title>T2)

 []

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

[A1T1] + []

l=[[//book]]0 () = [A1T1,

A2T2]

[[for $book in //book return

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

 [A1T1]

[[{ for $book in //book return

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

 [A1T1]

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

{

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

where fn:count($b/author) > 0

return

{ $b/title }

{ for $a in $b/author[position()<=2] return $a }

{ if (fn:count($b/author) > 2) then else () }

}

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

{

let $doc := doc("doc.xml")

for $t in fn:distinct-values($doc//book/title)

let $p := $doc//book[title = $t]/price

return

{ fn:min($p) }

}

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

FIRSTNAME

LASTNAME

{

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

{ $book1/title } { $book2/title }

}

<<: „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/

ad