1 introduction to database systems cse 444 lecture 11 xpath/xquery april 23, 2008
Post on 21-Dec-2015
236 views
TRANSCRIPT
![Page 1: 1 Introduction to Database Systems CSE 444 Lecture 11 Xpath/XQuery April 23, 2008](https://reader035.vdocuments.net/reader035/viewer/2022062221/56649d5d5503460f94a3b7fb/html5/thumbnails/1.jpg)
1
Introduction to Database SystemsCSE 444
Lecture 11Xpath/XQuery
April 23, 2008
![Page 2: 1 Introduction to Database Systems CSE 444 Lecture 11 Xpath/XQuery April 23, 2008](https://reader035.vdocuments.net/reader035/viewer/2022062221/56649d5d5503460f94a3b7fb/html5/thumbnails/2.jpg)
2
Outline
• XPath• XQuery
• See recommend readings in previous lecture
![Page 3: 1 Introduction to Database Systems CSE 444 Lecture 11 Xpath/XQuery April 23, 2008](https://reader035.vdocuments.net/reader035/viewer/2022062221/56649d5d5503460f94a3b7fb/html5/thumbnails/3.jpg)
3
Querying XML Data• XPath = simple navigation through the tree
• XQuery = the SQL of XML
• XSLT = recursive traversal– will not discuss in class
![Page 4: 1 Introduction to Database Systems CSE 444 Lecture 11 Xpath/XQuery April 23, 2008](https://reader035.vdocuments.net/reader035/viewer/2022062221/56649d5d5503460f94a3b7fb/html5/thumbnails/4.jpg)
4
Sample Data for Queries<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>
![Page 5: 1 Introduction to Database Systems CSE 444 Lecture 11 Xpath/XQuery April 23, 2008](https://reader035.vdocuments.net/reader035/viewer/2022062221/56649d5d5503460f94a3b7fb/html5/thumbnails/5.jpg)
5
Data Model for XPath
bib
book book
publisher author . . . .
Addison-Wesley Serge Abiteboul
The root
The root element
![Page 6: 1 Introduction to Database Systems CSE 444 Lecture 11 Xpath/XQuery April 23, 2008](https://reader035.vdocuments.net/reader035/viewer/2022062221/56649d5d5503460f94a3b7fb/html5/thumbnails/6.jpg)
6
XPath: Simple Expressions
Result: <year> 1995 </year>
<year> 1998 </year>
Result: empty (there were no papers)
/bib/book/year/bib/book/year
/bib/paper/year/bib/paper/year
/bib/bib //What’s the difference ?
![Page 7: 1 Introduction to Database Systems CSE 444 Lecture 11 Xpath/XQuery April 23, 2008](https://reader035.vdocuments.net/reader035/viewer/2022062221/56649d5d5503460f94a3b7fb/html5/thumbnails/7.jpg)
7
XPath: Restricted Kleene Closure
Result:<author> Serge Abiteboul </author> <author> <first-name> Rick </first-name> <last-name> Hull </last-name> </author> <author> Victor Vianu </author> <author> Jeffrey D. Ullman </author>
Result: <first-name> Rick </first-name>
//author//author
/bib//first-name/bib//first-name
![Page 8: 1 Introduction to Database Systems CSE 444 Lecture 11 Xpath/XQuery April 23, 2008](https://reader035.vdocuments.net/reader035/viewer/2022062221/56649d5d5503460f94a3b7fb/html5/thumbnails/8.jpg)
8
Xpath: Attribute Nodes
Result: “55”
@price means that price is has to be an attribute
/bib/book/@price/bib/book/@price
![Page 9: 1 Introduction to Database Systems CSE 444 Lecture 11 Xpath/XQuery April 23, 2008](https://reader035.vdocuments.net/reader035/viewer/2022062221/56649d5d5503460f94a3b7fb/html5/thumbnails/9.jpg)
9
Xpath: Wildcard
Result: <first-name> Rick </first-name>
<last-name> Hull </last-name>
* Matches any element
@* Matches any attribute
//author/*//author/*
![Page 10: 1 Introduction to Database Systems CSE 444 Lecture 11 Xpath/XQuery April 23, 2008](https://reader035.vdocuments.net/reader035/viewer/2022062221/56649d5d5503460f94a3b7fb/html5/thumbnails/10.jpg)
10
Xpath: Text Nodes
Result: Serge Abiteboul
Victor Vianu Jeffrey D. Ullman
Rick Hull doesn’t appear because he has firstname, lastname
Functions in XPath:– text() = matches the text value– node() = matches any node (= * or @* or text())– name() = returns the name of the current tag
/bib/book/author/text()/bib/book/author/text()
![Page 11: 1 Introduction to Database Systems CSE 444 Lecture 11 Xpath/XQuery April 23, 2008](https://reader035.vdocuments.net/reader035/viewer/2022062221/56649d5d5503460f94a3b7fb/html5/thumbnails/11.jpg)
11
Xpath: Predicates
Result: <author> <first-name> Rick </first-name>
<last-name> Hull </last-name>
</author>
/bib/book/author[firstname]/bib/book/author[firstname]
![Page 12: 1 Introduction to Database Systems CSE 444 Lecture 11 Xpath/XQuery April 23, 2008](https://reader035.vdocuments.net/reader035/viewer/2022062221/56649d5d5503460f94a3b7fb/html5/thumbnails/12.jpg)
12
Xpath: More Predicates
Result: <lastname> … </lastname>
<lastname> … </lastname>
/bib/book/author[firstname][address[.//zip][city]]/lastname/bib/book/author[firstname][address[.//zip][city]]/lastname
How do we read this ?First remove all qualifiers (predicates):
/bib/book/author/lastname/bib/book/author/lastname
Then add them one by one:
/bib/book/author[firstname][address]/lastname/bib/book/author[firstname][address]/lastname etc
![Page 13: 1 Introduction to Database Systems CSE 444 Lecture 11 Xpath/XQuery April 23, 2008](https://reader035.vdocuments.net/reader035/viewer/2022062221/56649d5d5503460f94a3b7fb/html5/thumbnails/13.jpg)
13
/bib/book[@price < 60]/bib/book[@price < 60]
/bib/book[author/@age < 25]/bib/book[author/@age < 25]
/bib/book[author/text()]/bib/book[author/text()]
Xpath: More Predicates
![Page 14: 1 Introduction to Database Systems CSE 444 Lecture 11 Xpath/XQuery April 23, 2008](https://reader035.vdocuments.net/reader035/viewer/2022062221/56649d5d5503460f94a3b7fb/html5/thumbnails/14.jpg)
14
/bib/book[2]/bib/book[2]
/bib/book[@year = 1998] [2]/bib/book[@year = 1998] [2]
/bib/book[2][@year = 1998]/bib/book[2][@year = 1998]
The 2nd book
/bib/book[last()]/bib/book[last()] The last book
The 2nd of allbooks in 1998
2nd book IF itis in 1998
Xpath: Position Predicates
![Page 15: 1 Introduction to Database Systems CSE 444 Lecture 11 Xpath/XQuery April 23, 2008](https://reader035.vdocuments.net/reader035/viewer/2022062221/56649d5d5503460f94a3b7fb/html5/thumbnails/15.jpg)
15
Xpath: More Axes
/bib/book[.//review]/bib/book[.//review]. means current node
/bib/book[./review]/bib/book[./review] Same as /bib/book[review]/bib/book[review]
/bib/author/. /firstname/bib/author/. /firstname Same as /bib/author/firstname/bib/author/firstname
![Page 16: 1 Introduction to Database Systems CSE 444 Lecture 11 Xpath/XQuery April 23, 2008](https://reader035.vdocuments.net/reader035/viewer/2022062221/56649d5d5503460f94a3b7fb/html5/thumbnails/16.jpg)
16
Xpath: More Axes
/bib/book[.//review/../comments]/bib/book[.//review/../comments]
.. means parent node
Same as
/bib/author/.. /author/zip/bib/author/.. /author/zip Same as /bib/author/zip/bib/author/zip
/bib/book[.//*[comments][review]]/bib/book[.//*[comments][review]]
Hint: don’t use ..
![Page 17: 1 Introduction to Database Systems CSE 444 Lecture 11 Xpath/XQuery April 23, 2008](https://reader035.vdocuments.net/reader035/viewer/2022062221/56649d5d5503460f94a3b7fb/html5/thumbnails/17.jpg)
17
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
bib/book[@price<“55”]/author/lastname matches…
![Page 18: 1 Introduction to Database Systems CSE 444 Lecture 11 Xpath/XQuery April 23, 2008](https://reader035.vdocuments.net/reader035/viewer/2022062221/56649d5d5503460f94a3b7fb/html5/thumbnails/18.jpg)
18
XQuery
• Based on Quilt, which is based on XML-QL
• Uses XPath to express more complex queries
![Page 19: 1 Introduction to Database Systems CSE 444 Lecture 11 Xpath/XQuery April 23, 2008](https://reader035.vdocuments.net/reader035/viewer/2022062221/56649d5d5503460f94a3b7fb/html5/thumbnails/19.jpg)
19
FLWR (“Flower”) Expressions
FOR ...
LET...
WHERE...
RETURN...
FOR ...
LET...
WHERE...
RETURN...
![Page 20: 1 Introduction to Database Systems CSE 444 Lecture 11 Xpath/XQuery April 23, 2008](https://reader035.vdocuments.net/reader035/viewer/2022062221/56649d5d5503460f94a3b7fb/html5/thumbnails/20.jpg)
20
FOR-WHERE-RETURN
Find all book titles published after 1995:
FOR $x IN document("bib.xml")/bib/book
WHERE $x/year/text() > 1995
RETURN $x/title
FOR $x IN document("bib.xml")/bib/book
WHERE $x/year/text() > 1995
RETURN $x/title
Result: <title> abc </title> <title> def </title> <title> ghi </title>
![Page 21: 1 Introduction to Database Systems CSE 444 Lecture 11 Xpath/XQuery April 23, 2008](https://reader035.vdocuments.net/reader035/viewer/2022062221/56649d5d5503460f94a3b7fb/html5/thumbnails/21.jpg)
21
FOR-WHERE-RETURN
Equivalently (perhaps more geekish)
FOR $x IN document("bib.xml")/bib/book[year/text() > 1995] /title
RETURN $x
FOR $x IN document("bib.xml")/bib/book[year/text() > 1995] /title
RETURN $x
And even shorter:
document("bib.xml")/bib/book[year/text() > 1995] /title document("bib.xml")/bib/book[year/text() > 1995] /title
![Page 22: 1 Introduction to Database Systems CSE 444 Lecture 11 Xpath/XQuery April 23, 2008](https://reader035.vdocuments.net/reader035/viewer/2022062221/56649d5d5503460f94a3b7fb/html5/thumbnails/22.jpg)
22
COERCION
The query:
FOR $x IN document("bib.xml")/bib/book[year > 1995] /title
RETURN $x
FOR $x IN document("bib.xml")/bib/book[year > 1995] /title
RETURN $x
Is rewritten by the system into:
FOR $x IN document("bib.xml")/bib/book[year/text() > 1995] /title
RETURN $x
FOR $x IN document("bib.xml")/bib/book[year/text() > 1995] /title
RETURN $x
![Page 23: 1 Introduction to Database Systems CSE 444 Lecture 11 Xpath/XQuery April 23, 2008](https://reader035.vdocuments.net/reader035/viewer/2022062221/56649d5d5503460f94a3b7fb/html5/thumbnails/23.jpg)
23
FOR-WHERE-RETURN
• Find all book titles and the year when they were published:
FOR $x IN document("bib.xml")/ bib/bookRETURN <answer> <title>{ $x/title/text() } </title> <year>{ $x/year/text() } </year> </answer>
FOR $x IN document("bib.xml")/ bib/bookRETURN <answer> <title>{ $x/title/text() } </title> <year>{ $x/year/text() } </year> </answer>
Result: <answer> <title> abc </title> <year> 1995 </ year > </answer> <answer> <title> def </title> < year > 2002 </ year > </answer> <answer> <title> ghk </title> < year > 1980 </ year > </answer>
![Page 24: 1 Introduction to Database Systems CSE 444 Lecture 11 Xpath/XQuery April 23, 2008](https://reader035.vdocuments.net/reader035/viewer/2022062221/56649d5d5503460f94a3b7fb/html5/thumbnails/24.jpg)
24
FOR-WHERE-RETURN
• Notice the use of “{“ and “}”
• What is the result without them ?FOR $x IN document("bib.xml")/ bib/bookRETURN <answer> <title> $x/title/text() </title> <year> $x/year/text() </year> </answer>
FOR $x IN document("bib.xml")/ bib/bookRETURN <answer> <title> $x/title/text() </title> <year> $x/year/text() </year> </answer>
<answer> <title> $x/title/text() </title> <year> $x/year/text() </year> </answer>
<answer> <title> $x/title/text() </title> <year> $x/year/text() </year> </answer>
<answer> <title> $x/title/text() </title> <year> $x/year/text() </year> </answer>
![Page 25: 1 Introduction to Database Systems CSE 444 Lecture 11 Xpath/XQuery April 23, 2008](https://reader035.vdocuments.net/reader035/viewer/2022062221/56649d5d5503460f94a3b7fb/html5/thumbnails/25.jpg)
25
NestingFor each author of a book by Morgan
Kaufmann, list all books she published:
FOR $b IN document(“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 document(“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>
In the RETURN clause comma concatenates XML fragments
![Page 26: 1 Introduction to Database Systems CSE 444 Lecture 11 Xpath/XQuery April 23, 2008](https://reader035.vdocuments.net/reader035/viewer/2022062221/56649d5d5503460f94a3b7fb/html5/thumbnails/26.jpg)
26
Result
<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>
![Page 27: 1 Introduction to Database Systems CSE 444 Lecture 11 Xpath/XQuery April 23, 2008](https://reader035.vdocuments.net/reader035/viewer/2022062221/56649d5d5503460f94a3b7fb/html5/thumbnails/27.jpg)
27
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 document("bib.xml")/bib/bookWHERE count($x/author)>3 RETURN $x
FOR $x IN document("bib.xml")/bib/bookWHERE count($x/author)>3 RETURN $x
![Page 28: 1 Introduction to Database Systems CSE 444 Lecture 11 Xpath/XQuery April 23, 2008](https://reader035.vdocuments.net/reader035/viewer/2022062221/56649d5d5503460f94a3b7fb/html5/thumbnails/28.jpg)
28
Aggregates
Same thing:
FOR $x IN document("bib.xml")/bib/book[count(author)>3] RETURN $x
FOR $x IN document("bib.xml")/bib/book[count(author)>3] RETURN $x
![Page 29: 1 Introduction to Database Systems CSE 444 Lecture 11 Xpath/XQuery April 23, 2008](https://reader035.vdocuments.net/reader035/viewer/2022062221/56649d5d5503460f94a3b7fb/html5/thumbnails/29.jpg)
29
Eliminating Duplicates
Print all authors:
FOR $a IN distinct-values($b/book/author/text())RETURN <author> { $a } </author>
FOR $a IN distinct-values($b/book/author/text())RETURN <author> { $a } </author>
Note: distinct-values applies ONLY to values, NOT elements
![Page 30: 1 Introduction to Database Systems CSE 444 Lecture 11 Xpath/XQuery April 23, 2008](https://reader035.vdocuments.net/reader035/viewer/2022062221/56649d5d5503460f94a3b7fb/html5/thumbnails/30.jpg)
30
The LET Clause
Find books whose price is larger than average:
FOR $b in document(“bib.xml”)/bibLET $a:=avg($b/book/price/text())FOR $x in $b/bookWHERE $x/price/text() > $aRETURN $x
FOR $b in document(“bib.xml”)/bibLET $a:=avg($b/book/price/text())FOR $x in $b/bookWHERE $x/price/text() > $aRETURN $x
![Page 31: 1 Introduction to Database Systems CSE 444 Lecture 11 Xpath/XQuery April 23, 2008](https://reader035.vdocuments.net/reader035/viewer/2022062221/56649d5d5503460f94a3b7fb/html5/thumbnails/31.jpg)
31
Flattening• Compute a list of (author, title)
pairs
FOR $b IN document("bib.xml")/bib/book, $x IN $b/title/text(), $y IN $b/author/text()RETURN <answer> <title> { $x } </title> <author> { $y } </author> </answer>
FOR $b IN document("bib.xml")/bib/book, $x IN $b/title/text(), $y IN $b/author/text()RETURN <answer> <title> { $x } </title> <author> { $y } </author> </answer>
Output:<answer> <title> Databases </title> <author> Widom </author></answer><answer> <title> Databases </title> <author> Ullman </author></answer>
Input:<book> <title> Databases </title> <author> Widom </author> <author> Ullman </author></answer>
![Page 32: 1 Introduction to Database Systems CSE 444 Lecture 11 Xpath/XQuery April 23, 2008](https://reader035.vdocuments.net/reader035/viewer/2022062221/56649d5d5503460f94a3b7fb/html5/thumbnails/32.jpg)
32
Re-grouping
• For each author, return all titles of her/his books
FOR $b IN document("bib.xml")/bib, $x IN $b/book/author/text()RETURN <answer> <author> { $x } </author> { FOR $y IN $b/book[author/text()=$x]/title RETURN $y } </answer>
FOR $b IN document("bib.xml")/bib, $x IN $b/book/author/text()RETURN <answer> <author> { $x } </author> { FOR $y IN $b/book[author/text()=$x]/title RETURN $y } </answer>
What aboutduplicateauthors ?
Result:<answer> <author> efg </author> <title> abc </title> <title> klm </title> . . . .</answer>
![Page 33: 1 Introduction to Database Systems CSE 444 Lecture 11 Xpath/XQuery April 23, 2008](https://reader035.vdocuments.net/reader035/viewer/2022062221/56649d5d5503460f94a3b7fb/html5/thumbnails/33.jpg)
33
Re-grouping
• Same, but eliminate duplicate authors:
FOR $b IN document("bib.xml")/bibLET $a := distinct-values($b/book/author/text())FOR $x IN $aRETURN <answer> <author> $x </author> { FOR $y IN $b/book[author/text()=$x]/title RETURN $y } </answer>
FOR $b IN document("bib.xml")/bibLET $a := distinct-values($b/book/author/text())FOR $x IN $aRETURN <answer> <author> $x </author> { FOR $y IN $b/book[author/text()=$x]/title RETURN $y } </answer>
![Page 34: 1 Introduction to Database Systems CSE 444 Lecture 11 Xpath/XQuery April 23, 2008](https://reader035.vdocuments.net/reader035/viewer/2022062221/56649d5d5503460f94a3b7fb/html5/thumbnails/34.jpg)
34
Re-grouping
• Same thing:
FOR $b IN document("bib.xml")/bib, $x IN distinct-values($b/book/author/text())RETURN <answer> <author> $x </author> { FOR $y IN $b/book[author/text()=$x]/title RETURN $y } </answer>
FOR $b IN document("bib.xml")/bib, $x IN distinct-values($b/book/author/text())RETURN <answer> <author> $x </author> { FOR $y IN $b/book[author/text()=$x]/title RETURN $y } </answer>
![Page 35: 1 Introduction to Database Systems CSE 444 Lecture 11 Xpath/XQuery April 23, 2008](https://reader035.vdocuments.net/reader035/viewer/2022062221/56649d5d5503460f94a3b7fb/html5/thumbnails/35.jpg)
35
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 document(“db.xml”)/db/Product/rowORDER BY $x/price/text()RETURN <answer> { $x/name, $x/price } </answer>
FOR $x in document(“db.xml”)/db/Product/rowORDER BY $x/price/text()RETURN <answer> { $x/name, $x/price } </answer>
XQuery
![Page 36: 1 Introduction to Database Systems CSE 444 Lecture 11 Xpath/XQuery April 23, 2008](https://reader035.vdocuments.net/reader035/viewer/2022062221/56649d5d5503460f94a3b7fb/html5/thumbnails/36.jpg)
36
<answer> <name> abc </name> <price> 7 </price></answer> <answer> <name> def </name> <price> 23 </price></answer> . . . .
Xquery’s Answer
Notice: this is NOT awell-formed document !(WHY ???)
![Page 37: 1 Introduction to Database Systems CSE 444 Lecture 11 Xpath/XQuery April 23, 2008](https://reader035.vdocuments.net/reader035/viewer/2022062221/56649d5d5503460f94a3b7fb/html5/thumbnails/37.jpg)
37
Producing a Well-Formed Answer
<aQuery> { FOR $x in document(“db.xml”)/db/Product/row ORDER BY $x/price/text() RETURN <answer> { $x/name, $x/price } </answer> }</aQuery>
<aQuery> { FOR $x in document(“db.xml”)/db/Product/row ORDER BY $x/price/text() RETURN <answer> { $x/name, $x/price } </answer> }</aQuery>
![Page 38: 1 Introduction to Database Systems CSE 444 Lecture 11 Xpath/XQuery April 23, 2008](https://reader035.vdocuments.net/reader035/viewer/2022062221/56649d5d5503460f94a3b7fb/html5/thumbnails/38.jpg)
38
<aQuery> <answer> <name> abc </name> <price> 7 </price> </answer> <answer> <name> def </name> <price> 23 </price> </answer> . . . .</aQuery>
Xquery’s Answer
Now it is well-formed !
![Page 39: 1 Introduction to Database Systems CSE 444 Lecture 11 Xpath/XQuery April 23, 2008](https://reader035.vdocuments.net/reader035/viewer/2022062221/56649d5d5503460f94a3b7fb/html5/thumbnails/39.jpg)
39
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 document(“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 document(“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 40: 1 Introduction to Database Systems CSE 444 Lecture 11 Xpath/XQuery April 23, 2008](https://reader035.vdocuments.net/reader035/viewer/2022062221/56649d5d5503460f94a3b7fb/html5/thumbnails/40.jpg)
40
<product> <row> <pid> 123 </pid> <name> abc </name> <maker> efg </maker> </row> <row> …. </row> …</product><product> . . .</product>. . . .
![Page 41: 1 Introduction to Database Systems CSE 444 Lecture 11 Xpath/XQuery April 23, 2008](https://reader035.vdocuments.net/reader035/viewer/2022062221/56649d5d5503460f94a3b7fb/html5/thumbnails/41.jpg)
41
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 document(“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 document(“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 42: 1 Introduction to Database Systems CSE 444 Lecture 11 Xpath/XQuery April 23, 2008](https://reader035.vdocuments.net/reader035/viewer/2022062221/56649d5d5503460f94a3b7fb/html5/thumbnails/42.jpg)
42
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 document(“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 document(“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