(:=== XQuery Demo ===:) (:*************************************************************** SIMPLE FLWOR QUERY Titles of books costing less than $90 where "Ullman" is an author ****************************************************************:) for $b in doc("bookstore.xml")/Bookstore/Book where $b/@Price < 90 and $b/Authors/Author/Last_Name = "Ullman" return $b/Title (:*************************************************************** EXISTENTIAL QUANTIFICATION AND CONSTRUCTED RESULT Titles and authors of books whose title contains one of the author's first names [[ Then return only first names in title ]] ****************************************************************:) for $b in doc("bookstore.xml")/Bookstore/Book where some $fn in $b/Authors/Author/First_Name satisfies contains($b/Title, $fn) return { $b/Title } { $b/Authors/Author/First_Name } (:*************************************************************** LET CLAUSE, AGGREGATION, QUERY IN XML Average book price ****************************************************************:) { let $plist := doc("bookstore.xml")/Bookstore/Book/@Price return avg(data($plist)) } (:*************************************************************** LET AND FOR TOGETHER, ATTRIBUTES TURNED INTO ELEMENTS Books whose price is below average ****************************************************************:) let $plist := doc("bookstore.xml")/Bookstore/Book/@Price for $b in doc("bookstore.xml")/Bookstore/Book where $b/@Price < avg(data($plist)) return { $b/Title } { $b/data(@Price) } (:*************************************************************** ORDERING Titles and prices sorted by price [[ Not quite right, fix using xs:int ]] ****************************************************************:) for $b in doc("bookstore.xml")/Bookstore/Book order by $b/@Price return { $b/Title } { $b/data(@Price) } (:*************************************************************** DUPLICATE ELIMINATION [[ Duplicates, then eliminate using distinct-values and more ]] ****************************************************************:) let $lnlist := doc("bookstore.xml")//Last_Name return $lnlist (:*************************************************************** UNIVERSAL QUANTIFICATION (FOR-ALL) Books where every author's first name includes "J" ****************************************************************:) for $b in doc("bookstore.xml")/Bookstore/Book where every $fn in $b/Authors/Author/First_Name satisfies contains($fn, "J") return $b (:*************************************************************** "SELF-JOIN", ALSO IMPLICIT EXISTENTIAL IN = All pairs of titles containing a shared author [[ Oversight, then fix ]] ****************************************************************:) for $b1 in doc("bookstore.xml")/Bookstore/Book for $b2 in doc("bookstore.xml")/Bookstore/Book where $b1/Authors/Author/Last_Name = $b2/Authors/Author/Last_Name return { data($b1/Title) } { data($b2/Title) } (:*************************************************************** GRAND FINALE Invert data: Authors with the books they've written [[ Add Edition and Remark ]] ****************************************************************:) { let $lnlist := distinct-values( doc("bookstore.xml")//Author/Last_Name) for $ln in $lnlist let $fn := distinct-values(doc("bookstore.xml")//Author[ Last_Name=$ln]/First_Name) return { $fn } { $ln } { for $b in doc("bookstore.xml")/Bookstore/Book[ Authors/Author/Last_Name = $ln] return { $b/@ISBN } { $b/@Price } { $b/Title } } }