28
1 XQuery Slides From Dr. Suciu

1 XQuery Slides From Dr. Suciu. 2 XQuery Based on Quilt, which is based on XML-QL Uses XPath to express more complex queries

Embed Size (px)

Citation preview

Page 1: 1 XQuery Slides From Dr. Suciu. 2 XQuery Based on Quilt, which is based on XML-QL Uses XPath to express more complex queries

1

XQuery

Slides From

Dr. Suciu

Page 2: 1 XQuery Slides From Dr. Suciu. 2 XQuery Based on Quilt, which is based on XML-QL Uses XPath to express more complex queries

2

XQuery

• Based on Quilt, which is based on XML-QL

• Uses XPath to express more complex queries

Page 3: 1 XQuery Slides From Dr. Suciu. 2 XQuery Based on Quilt, which is based on XML-QL Uses XPath to express more complex queries

3

Xpath: Summarybib matches a bib element

* matches any element

/ matches the root element

/bib matches a bib element under root

bib/paper matches a paper in bib

bib//paper matches a paper in bib, at any depth

//paper matches a paper at any depth

paper|book matches a paper or a book

@price matches a price attribute

bib/book/@price matches price attribute in book, in bib

Page 4: 1 XQuery Slides From Dr. Suciu. 2 XQuery Based on Quilt, which is based on XML-QL Uses XPath to express more complex queries

4

FLWR (“Flower”) Expressions

FOR ...

LET...

WHERE...

RETURN...

FOR ...

LET...

WHERE...

RETURN...

Page 5: 1 XQuery Slides From Dr. Suciu. 2 XQuery Based on Quilt, which is based on XML-QL Uses XPath to express more complex queries

5

<bib><book> <publisher> Addison-Wesley </publisher> <author> Serge Abiteboul </author> <author> <first-name> Rick </first-name> <last-name> Hull </last-name> </author> <author> Victor Vianu </author> <title> Foundations of Databases </title> <year> 1995 </year></book><book price=“55”> <publisher> Freeman </publisher> <author> Jeffrey D. Ullman </author> <title> Principles of Database and Knowledge Base Systems </title> <year> 1998 </year></book>

</bib>

<bib><book> <publisher> Addison-Wesley </publisher> <author> Serge Abiteboul </author> <author> <first-name> Rick </first-name> <last-name> Hull </last-name> </author> <author> Victor Vianu </author> <title> Foundations of Databases </title> <year> 1995 </year></book><book price=“55”> <publisher> Freeman </publisher> <author> Jeffrey D. Ullman </author> <title> Principles of Database and Knowledge Base Systems </title> <year> 1998 </year></book>

</bib>

Sample Data for Queries (more or less)

Page 6: 1 XQuery Slides From Dr. Suciu. 2 XQuery Based on Quilt, which is based on XML-QL Uses XPath to express more complex queries

6

FOR-WHERE-RETURN

Find all book titles published after 1995:

for $x in doc("bib.xml")/bib/bookwhere data($x/@year) > 1999return $x/title

for $x in doc("bib.xml")/bib/bookwhere data($x/@year) > 1999return $x/title

Result: <title> abc </title> <title> def </title> <title> ghi </title>

Page 7: 1 XQuery Slides From Dr. Suciu. 2 XQuery Based on Quilt, which is based on XML-QL Uses XPath to express more complex queries

7

FOR-WHERE-RETURN

Equivalently (perhaps more geekish)

FOR $x IN doc("bib.xml")/bib/book[@year > 1995] /title

RETURN $x

FOR $x IN doc("bib.xml")/bib/book[@year > 1995] /title

RETURN $x

And even shorter:

doc("bib.xml")/bib/book[@year > 1995] /title doc("bib.xml")/bib/book[@year > 1995] /title

Page 8: 1 XQuery Slides From Dr. Suciu. 2 XQuery Based on Quilt, which is based on XML-QL Uses XPath to express more complex queries

8

FOR-WHERE-RETURN

• Find all book titles and the year when they were published:

FOR $x IN doc("bib.xml")/bib/bookRETURN <answer> <what>{ $x/title/text() } </what> <when>{ $x/year/text() } </when> </answer>

FOR $x IN doc("bib.xml")/bib/bookRETURN <answer> <what>{ $x/title/text() } </what> <when>{ $x/year/text() } </when> </answer>

We can construct whatever XML results we want !

Page 9: 1 XQuery Slides From Dr. Suciu. 2 XQuery Based on Quilt, which is based on XML-QL Uses XPath to express more complex queries

9

Answer<answer> <what> How to cook a Turkey </what> <when> 2003 </when></answer><answer> <what> Cooking While Watching TV </what> <when> 2004 </when></answer><answer> <what> Turkeys on TV</what> <when> 2002 </when></answer>. . . . .

<answer> <what> How to cook a Turkey </what> <when> 2003 </when></answer><answer> <what> Cooking While Watching TV </what> <when> 2004 </when></answer><answer> <what> Turkeys on TV</what> <when> 2002 </when></answer>. . . . .

Page 10: 1 XQuery Slides From Dr. Suciu. 2 XQuery Based on Quilt, which is based on XML-QL Uses XPath to express more complex queries

10

FOR-WHERE-RETURN

• Notice the use of “{“ and “}”

• What is the result without them ?

FOR $x IN doc("bib.xml")/bib/bookRETURN <answer> <title> $x/title/text() </title> <year> $x/year/text() </year> </answer>

FOR $x IN doc("bib.xml")/bib/bookRETURN <answer> <title> $x/title/text() </title> <year> $x/year/text() </year> </answer>

Page 11: 1 XQuery Slides From Dr. Suciu. 2 XQuery Based on Quilt, which is based on XML-QL Uses XPath to express more complex queries

11

XQuery: NestingFor each author of a book by Morgan

Kaufmann, list all books she published:

FOR $b IN doc(“bib.xml”)/bib, $a IN $b/book[publisher /text()=“Morgan Kaufmann”]/authorRETURN <result> { $a, FOR $t IN $b/book[author/text()=$a/text()]/title RETURN $t } </result>

FOR $b IN doc(“bib.xml”)/bib, $a IN $b/book[publisher /text()=“Morgan Kaufmann”]/authorRETURN <result> { $a, FOR $t IN $b/book[author/text()=$a/text()]/title RETURN $t } </result>

Page 12: 1 XQuery Slides From Dr. Suciu. 2 XQuery Based on Quilt, which is based on XML-QL Uses XPath to express more complex queries

12

XQuery

<result> <author>Jones</author> <title> abc </title> <title> def </title> </result> <result> <author> Smith </author> <title> ghi </title> </result>

<result> <author>Jones</author> <title> abc </title> <title> def </title> </result> <result> <author> Smith </author> <title> ghi </title> </result>

Result:

Page 13: 1 XQuery Slides From Dr. Suciu. 2 XQuery Based on Quilt, which is based on XML-QL Uses XPath to express more complex queries

13

Inverting Hierarchy• <listings>• {• for $p in distinct-values(doc("bib.xml")//publisher)• order by $p• return• <result>• { $p }• {• for $b in doc("bib.xml")/bib/book• where $b/publisher = $p• order by $b/title• return $b/title• }• </result>• }• </listings>

Page 14: 1 XQuery Slides From Dr. Suciu. 2 XQuery Based on Quilt, which is based on XML-QL Uses XPath to express more complex queries

14

Aggregates

Find all books with more than 3 authors:

count = a function that countsavg = computes the averagesum = computes the sumdistinct-values = eliminates duplicates

FOR $x IN doc("bib.xml")/bib/bookWHERE count($x/author)>3 RETURN $x

FOR $x IN doc("bib.xml")/bib/bookWHERE count($x/author)>3 RETURN $x

for $l in distinct-values(doc("bib.xml")//author/last)

return <last>{ $l }</last>

for $l in distinct-values(doc("bib.xml")//author/last)

return <last>{ $l }</last>

Page 15: 1 XQuery Slides From Dr. Suciu. 2 XQuery Based on Quilt, which is based on XML-QL Uses XPath to express more complex queries

15

Aggregates

<answer>{for $b in doc("books.xml")//booklet $c := $b/authorreturn <book>{ $b/title, <count>{ count($c) }</count>}</book>}</answer>

Page 16: 1 XQuery Slides From Dr. Suciu. 2 XQuery Based on Quilt, which is based on XML-QL Uses XPath to express more complex queries

16

XQuery

Find books whose price is larger than average:<answer>

{for $b in doc("bib.xml")/biblet $a := avg($b/book/price/text())for $x in $b/bookwhere ($x/price/text() > $a)return $x}</answer>

<answer>{for $b in doc("bib.xml")/biblet $a := avg($b/book/price/text())for $x in $b/bookwhere ($x/price/text() > $a)return $x}</answer>

LET binds a variable to one value;FOR iterates a variable over a list of values

Page 17: 1 XQuery Slides From Dr. Suciu. 2 XQuery Based on Quilt, which is based on XML-QL Uses XPath to express more complex queries

17

FOR v.s. LET

FOR $x IN /bib/bookRETURN <result> { $x } </result>

FOR $x IN /bib/bookRETURN <result> { $x } </result>

Returns: <result> <book>...</book></result> <result> <book>...</book></result> <result> <book>...</book></result> ...

LET $x := /bib/bookRETURN <result> { $x } </result>

LET $x := /bib/bookRETURN <result> { $x } </result>

Returns: <result> <book>...</book> <book>...</book> <book>...</book> ... </result>

Page 18: 1 XQuery Slides From Dr. Suciu. 2 XQuery Based on Quilt, which is based on XML-QL Uses XPath to express more complex queries

18

For vs Let (cont’d)

for $i in (1, 2, 3)let $j := "a"return <tuple><i>{ $i }</i><j>{ $j }</j></tuple>

for $i in (1, 2, 3), $j in (4, 5, 6)return <tuple><i>{ $i }</i><j>{ $j }</j> </tuple>

Page 19: 1 XQuery Slides From Dr. Suciu. 2 XQuery Based on Quilt, which is based on XML-QL Uses XPath to express more complex queries

19

SQL and XQuery Side-by-sideProduct(pid, name, maker, price) Find all product names, prices,

sort by price

SELECT x.name, x.priceFROM Product xORDER BY x.price

SELECT x.name, x.priceFROM Product xORDER BY x.price

SQL

FOR $x in doc(“db.xml”)/db/Product/rowORDER BY $x/price/text()RETURN <answer> { $x/name, $x/price } </answer>

FOR $x in doc(“db.xml”)/db/Product/rowORDER BY $x/price/text()RETURN <answer> { $x/name, $x/price } </answer>

XQuery

Page 20: 1 XQuery Slides From Dr. Suciu. 2 XQuery Based on Quilt, which is based on XML-QL Uses XPath to express more complex queries

20

<answer> <name> abc </name> <price> 7 </price></answer> <answer> <name> def </name> <price> 23 </price></answer> . . . .

Answers

Notice: this is NOT awell-formed document !(WHY ???)

Name Price

abc 7

def 23

. . . . . .

Page 21: 1 XQuery Slides From Dr. Suciu. 2 XQuery Based on Quilt, which is based on XML-QL Uses XPath to express more complex queries

21

Producing a Well-Formed Answer

<myQuery> { FOR $x in doc(“db.xml”)/db/Product/row ORDER BY $x/price/text() RETURN <answer> { $x/name, $x/price } </answer> }</myQuery>

<myQuery> { FOR $x in doc(“db.xml”)/db/Product/row ORDER BY $x/price/text() RETURN <answer> { $x/name, $x/price } </answer> }</myQuery>

Page 22: 1 XQuery Slides From Dr. Suciu. 2 XQuery Based on Quilt, which is based on XML-QL Uses XPath to express more complex queries

22

<myQuery> <answer> <name> abc </name> <price> 7 </price> </answer> <answer> <name> def </name> <price> 23 </price> </answer> . . . .</myQuery>

Xquery’s Answer

Now it is well-formed !

Page 23: 1 XQuery Slides From Dr. Suciu. 2 XQuery Based on Quilt, which is based on XML-QL Uses XPath to express more complex queries

23

SQL and XQuery Side-by-sideProduct(pid, name, maker, price)Company(cid, name, city, revenues) Find all products made in Seattle

SELECT x.nameFROM Product x, Company yWHERE x.maker=y.cid and y.city=“Seattle”

SELECT x.nameFROM Product x, Company yWHERE x.maker=y.cid and y.city=“Seattle”

SQL

FOR $r in doc(“db.xml”)/db, $x in $r/Product/row, $y in $r/Company/rowWHERE $x/maker/text()=$y/cid/text() and $y/city/text() = “Seattle”RETURN { $x/name }

FOR $r in doc(“db.xml”)/db, $x in $r/Product/row, $y in $r/Company/rowWHERE $x/maker/text()=$y/cid/text() and $y/city/text() = “Seattle”RETURN { $x/name }

XQuery

FOR $y in /db/Company/row[city/text()=“Seattle”], $x in /db/Product/row[maker/text()=$y/cid/text()]RETURN { $x/name }

FOR $y in /db/Company/row[city/text()=“Seattle”], $x in /db/Product/row[maker/text()=$y/cid/text()]RETURN { $x/name }

CoolXQuery

Page 24: 1 XQuery Slides From Dr. Suciu. 2 XQuery Based on Quilt, which is based on XML-QL Uses XPath to express more complex queries

24

<product> <row> <pid> 123 </pid> <name> abc </name> <maker> efg </maker> </row> <row> …. </row> …</product><product> . . .</product>. . . .

Page 25: 1 XQuery Slides From Dr. Suciu. 2 XQuery Based on Quilt, which is based on XML-QL Uses XPath to express more complex queries

25

SQL and XQuery Side-by-sideFor each company with revenues < 1M count the products over $100

SELECT y.name, count(*)FROM Product x, Company yWHERE x.price > 100 and x.maker=y.cid and y.revenue < 1000000GROUP BY y.cid, y.name

SELECT y.name, count(*)FROM Product x, Company yWHERE x.price > 100 and x.maker=y.cid and y.revenue < 1000000GROUP BY y.cid, y.name

FOR $r in doc(“db.xml”)/db, $y in $r/Company/row[revenue/text()<1000000]RETURN <proudCompany> <companyName> { $y/name/text() } </companyName> <numberOfExpensiveProducts> { count($r/Product/row[maker/text()=$y/cid/text()][price/text()>100]) } </numberOfExpensiveProducts> </proudCompany>

FOR $r in doc(“db.xml”)/db, $y in $r/Company/row[revenue/text()<1000000]RETURN <proudCompany> <companyName> { $y/name/text() } </companyName> <numberOfExpensiveProducts> { count($r/Product/row[maker/text()=$y/cid/text()][price/text()>100]) } </numberOfExpensiveProducts> </proudCompany>

Page 26: 1 XQuery Slides From Dr. Suciu. 2 XQuery Based on Quilt, which is based on XML-QL Uses XPath to express more complex queries

26

SQL and XQuery Side-by-sideFind companies with at least 30 products, and their average price

SELECT y.name, avg(x.price)FROM Product x, Company yWHERE x.maker=y.cidGROUP BY y.cid, y.nameHAVING count(*) > 30

SELECT y.name, avg(x.price)FROM Product x, Company yWHERE x.maker=y.cidGROUP BY y.cid, y.nameHAVING count(*) > 30

FOR $r in doc(“db.xml”)/db, $y in $r/Company/rowLET $p := $r/Product/row[maker/text()=$y/cid/text()]WHERE count($p) > 30RETURN <theCompany> <companyName> { $y/name/text() } </companyName> <avgPrice> avg($p/price/text()) </avgPrice> </theCompany>

FOR $r in doc(“db.xml”)/db, $y in $r/Company/rowLET $p := $r/Product/row[maker/text()=$y/cid/text()]WHERE count($p) > 30RETURN <theCompany> <companyName> { $y/name/text() } </companyName> <avgPrice> avg($p/price/text()) </avgPrice> </theCompany>

A collection

An element

Page 27: 1 XQuery Slides From Dr. Suciu. 2 XQuery Based on Quilt, which is based on XML-QL Uses XPath to express more complex queries

27

FOR v.s. LET

FOR

• Binds node variables iteration

LET

• Binds collection variables one value

Page 28: 1 XQuery Slides From Dr. Suciu. 2 XQuery Based on Quilt, which is based on XML-QL Uses XPath to express more complex queries

28

XQuery

Summary:

• FOR-LET-WHERE-RETURN = FLWR

FOR/LET Clauses

WHERE Clause

RETURN Clause

List of tuples

List of tuples

Instance of Xquery data model