End of XMLFLWR (“Flower”) ExpressionsXQuerySlide 4SQL and XQuery Side-by-sideXQuery: NestingSlide 7Slide 8Slide 9Slide 10Slide 11FOR v.s. LETSlide 13Collections in XQuerySlide 15Sorting in XQueryIf-Then-ElseExistential QuantifiersUniversal QuantifiersThe Role of XML DataXML from/to Relational DataXML PublishingSlide 23XML StorageSlide 25End of XMLFebruary 19th, 2003FLWR (“Flower”) ExpressionsFOR ... LET... WHERE...RETURN...FOR ... LET... WHERE...RETURN...XQueryFind book titles by the coauthors of “Database Theory”:FOR $x IN bib/book[title/text() = “Database Theory”]/author $y IN bib/book[author/text() = $x/text()]/titleRETURN <answer> { $y/text() } </answer>FOR $x IN bib/book[title/text() = “Database Theory”]/author $y IN bib/book[author/text() = $x/text()]/titleRETURN <answer> { $y/text() } </answer>Result: <answer> abc </ answer > < answer > def </ answer > < answer > ghi </ answer >The answer willcontain duplicates !XQuerySame as before, but eliminate duplicates:FOR $x IN bib/book[title/text() = “Database Theory”]/author $y IN distinct(bib/book[author/text() = $x/text()]/title)RETURN <answer> { $y/text() } </answer>FOR $x IN bib/book[title/text() = “Database Theory”]/author $y IN distinct(bib/book[author/text() = $x/text()]/title)RETURN <answer> { $y/text() } </answer>Result: <answer> abc </ answer > < answer > def </ answer > < answer > ghi </ answer >distinct = a function that eliminates duplicatesSQL and XQuery Side-by-sideProduct(pid, name, maker)Company(cid, name, city)Find all products made in SeattleSELECT 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”FOR $x in /db/Product/row $y in /db/Company/rowWHERE $x/maker/text()=$y/cid/text() and $y/city/text() = “Seattle”RETURN { $x/name }FOR $x in /db/Product/row $y in /db/Company/rowWHERE $x/maker/text()=$y/cid/text() and $y/city/text() = “Seattle”RETURN { $x/name }SQLXQueryFOR $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 }CoolXQueryXQuery: NestingFor each author of a book by Morgan Kaufmann, list all books she published:FOR $a IN distinct(document("bib.xml") /bib/book[publisher=“Morgan Kaufmann”]/author)RETURN <result> { $a, FOR $t IN /bib/book[author=$a]/title RETURN $t } </result>FOR $a IN distinct(document("bib.xml") /bib/book[publisher=“Morgan Kaufmann”]/author)RETURN <result> { $a, FOR $t IN /bib/book[author=$a]/title RETURN $t } </result>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:XQuery•FOR $x in expr -- binds $x to each value in the list expr•LET $x = expr -- binds $x to the entire list expr–Useful for common subexpressions and for aggregationsXQuerycount = a (aggregate) function that returns the number of elms<big_publishers> FOR $p IN distinct(document("bib.xml")//publisher) LET $b := document("bib.xml")/book[publisher = $p] WHERE count($b) > 100 RETURN { $p }</big_publishers><big_publishers> FOR $p IN distinct(document("bib.xml")//publisher) LET $b := document("bib.xml")/book[publisher = $p] WHERE count($b) > 100 RETURN { $p }</big_publishers>XQueryFind books whose price is larger than average:LET $a=avg(document("bib.xml")/bib/book/price)FOR $b in document("bib.xml")/bib/bookWHERE $b/price > $aRETURN { $b }LET $a=avg(document("bib.xml")/bib/book/price)FOR $b in document("bib.xml")/bib/bookWHERE $b/price > $aRETURN { $b }Let’s try to write this in SQL…XQuerySummary:•FOR-LET-WHERE-RETURN = FLWRFOR/LET ClausesWHERE ClauseRETURN ClauseList of tuplesList of tuplesInstance of Xquery data modelFOR v.s. LETFOR•Binds node variables iterationLET•Binds collection variables one valueFOR v.s. LETFOR $x IN document("bib.xml")/bib/bookRETURN <result> { $x } </result>FOR $x IN document("bib.xml")/bib/bookRETURN <result> { $x } </result>Returns: <result> <book>...</book></result> <result> <book>...</book></result> <result> <book>...</book></result> ...LET $x IN document("bib.xml")/bib/bookRETURN <result> { $x } </result>LET $x IN document("bib.xml")/bib/bookRETURN <result> { $x } </result>Returns: <result> <book>...</book> <book>...</book> <book>...</book> ...</result>Collections in XQuery•Ordered and unordered collections–/bib/book/author = an ordered collection–Distinct(/bib/book/author) = an unordered collection•LET $a = /bib/book $a is a collection•$b/author a collection (several authors...)RETURN <result> { $b/author } </result>RETURN <result> { $b/author } </result>Returns: <result> <author>...</author> <author>...</author> <author>...</author> ...</result>Collections in XQueryWhat about collections in expressions ?•$b/price list of n prices•$b/price * 0.7 list of n numbers•$b/price * $b/quantity list of n x m numbers ??•$b/price * ($b/quant1 + $b/quant2) $b/price * $b/quant1 + $b/price * $b/quant2 !!Sorting in XQuery<publisher_list> FOR $p IN distinct(document("bib.xml")//publisher) RETURN <publisher> <name> { $p/text() } </name> , FOR $b IN document("bib.xml")//book[publisher = $p] RETURN <book> { $b/title , $b/price } </book> SORTBY(price DESCENDING) </publisher> SORTBY(name) </publisher_list><publisher_list> FOR $p IN distinct(document("bib.xml")//publisher) RETURN <publisher> <name> { $p/text() } </name> , FOR $b IN document("bib.xml")//book[publisher = $p] RETURN <book> { $b/title ,
View Full Document