Download presentation
Presentation is loading. Please wait.
Published byRosalind Carson Modified over 9 years ago
1
1 XQuery Slides From Dr. Suciu
2
2 XQuery Based on Quilt, which is based on XML-QL Uses XPath to express more complex queries
3
3 Xpath: Summary bibmatches a bib element *matches any element /matches the root element /bibmatches a bib element under root bib/papermatches a paper in bib bib//papermatches a paper in bib, at any depth //papermatches a paper at any depth paper|bookmatches a paper or a book @pricematches a price attribute bib/book/@pricematches price attribute in book, in bib
4
4 FLWR (“Flower”) Expressions FOR... LET... WHERE... RETURN... FOR... LET... WHERE... RETURN...
5
5 Addison-Wesley Serge Abiteboul Rick Hull Victor Vianu Foundations of Databases 1995 Freeman Jeffrey D. Ullman Principles of Database and Knowledge Base Systems 1998 Addison-Wesley Serge Abiteboul Rick Hull Victor Vianu Foundations of Databases 1995 Freeman Jeffrey D. Ullman Principles of Database and Knowledge Base Systems 1998 Sample Data for Queries (more or less)
6
6 FOR-WHERE-RETURN Find all book titles published after 1995: for $x in doc("bib.xml")/bib/book where data($x/@year) > 1999 return $x/title for $x in doc("bib.xml")/bib/book where data($x/@year) > 1999 return $x/title Result: abc def ghi
7
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
8
8 FOR-WHERE-RETURN Find all book titles and the year when they were published: FOR $x IN doc("bib.xml")/bib/book RETURN { $x/title/text() } { $x/year/text() } We can construct whatever XML results we want !
9
9 Answer How to cook a Turkey 2003 Cooking While Watching TV 2004 Turkeys on TV 2002.....
10
10 FOR-WHERE-RETURN Notice the use of “{“ and “}” What is the result without them ? FOR $x IN doc("bib.xml")/bib/book RETURN $x/title/text() $x/year/text()
11
11 XQuery: Nesting For 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”]/author RETURN { $a, FOR $t IN $b/book[author/text()=$a/text()]/title RETURN $t } FOR $b IN doc(“bib.xml”)/bib, $a IN $b/book[publisher /text()=“Morgan Kaufmann”]/author RETURN { $a, FOR $t IN $b/book[author/text()=$a/text()]/title RETURN $t }
12
12 XQuery Jones abc def Smith ghi Jones abc def Smith ghi Result:
13
13 Inverting Hierarchy { for $p in distinct-values(doc("bib.xml")//publisher) order by $p return { $p } { for $b in doc("bib.xml")/bib/book where $b/publisher = $p order by $b/title return $b/title } }
14
14 Aggregates Find all books with more than 3 authors: count = a function that counts avg = computes the average sum = computes the sum distinct-values = eliminates duplicates FOR $x IN doc("bib.xml")/bib/book WHERE count($x/author)>3 RETURN $x for $l in distinct-values(doc("bib.xml")//author/last) return { $l } for $l in distinct-values(doc("bib.xml")//author/last) return { $l }
15
15 Aggregates { for $b in doc("books.xml")//book let $c := $b/author return { $b/title, { count($c) } } }
16
16 XQuery Find books whose price is larger than average: { for $b in doc("bib.xml")/bib let $a := avg($b/book/price/text()) for $x in $b/book where ($x/price/text() > $a) return $x } { for $b in doc("bib.xml")/bib let $a := avg($b/book/price/text()) for $x in $b/book where ($x/price/text() > $a) return $x } LET binds a variable to one value; FOR iterates a variable over a list of values
17
17 FOR v.s. LET FOR $x IN /bib/book RETURN { $x } FOR $x IN /bib/book RETURN { $x } Returns:... LET $x := /bib/book RETURN { $x } LET $x := /bib/book RETURN { $x } Returns:...
18
18 For vs Let (cont’d) for $i in (1, 2, 3) let $j := "a" return { $i } { $j } for $i in (1, 2, 3), $j in (4, 5, 6) return { $i } { $j }
19
19 SQL and XQuery Side-by-side Product(pid, name, maker, price) Find all product names, prices, sort by price SELECT x.name, x.price FROM Product x ORDER BY x.price SQL FOR $x in doc(“db.xml”)/db/Product/row ORDER BY $x/price/text() RETURN { $x/name, $x/price } XQuery
20
20 abc 7 def 23.... Answers Notice: this is NOT a well-formed document ! (WHY ???) NamePrice abc7 def23...
21
21 Producing a Well-Formed Answer { FOR $x in doc(“db.xml”)/db/Product/row ORDER BY $x/price/text() RETURN { $x/name, $x/price } }
22
22 abc 7 def 23.... Xquery’s Answer Now it is well-formed !
23
23 SQL and XQuery Side-by-side Product(pid, name, maker, price) Company(cid, name, city, revenues) Find all products made in Seattle SELECT x.name FROM Product x, Company y WHERE 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/row WHERE $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 } Cool XQuery
24
24 123 abc efg …. ….......
25
25 SQL and XQuery Side-by-side For each company with revenues < 1M count the products over $100 SELECT y.name, count(*) FROM Product x, Company y WHERE x.price > 100 and x.maker=y.cid and y.revenue < 1000000 GROUP BY y.cid, y.name FOR $r in doc(“db.xml”)/db, $y in $r/Company/row[revenue/text() { $y/name/text() } { count($r/Product/row[maker/text()=$y/cid/text()][price/text()>100]) }
26
26 SQL and XQuery Side-by-side Find companies with at least 30 products, and their average price SELECT y.name, avg(x.price) FROM Product x, Company y WHERE x.maker=y.cid GROUP BY y.cid, y.name HAVING count(*) > 30 FOR $r in doc(“db.xml”)/db, $y in $r/Company/row LET $p := $r/Product/row[maker/text()=$y/cid/text()] WHERE count($p) > 30 RETURN { $y/name/text() } avg($p/price/text()) A collection An element
27
27 FOR v.s. LET FOR Binds node variables iteration LET Binds collection variables one value
28
28 XQuery Summary: FOR-LET-WHERE-RETURN = FLWR FOR/LET Clauses WHERE Clause RETURN Clause List of tuples Instance of Xquery data model
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.