• Save
X Query for beginner
Upcoming SlideShare
Loading in...5
×
 

Like this? Share it with your network

Share

X Query for beginner

on

  • 617 views

 

Statistics

Views

Total Views
617
Views on SlideShare
617
Embed Views
0

Actions

Likes
0
Downloads
0
Comments
0

0 Embeds 0

No embeds

Accessibility

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

X Query for beginner Presentation Transcript

  • 1. XQueryThe Thuong
  • 2. In this lecture• Summary of XQuery• Default data type mapping(http://en.wikipedia.org/wiki/XQuery_API_for_Java)• FLWOR expressions• FOR and LET expressions• Collections and sortingResourcesW3C recommendation: www.w3.org/TR/xquery/Google
  • 3. XQuery• Based on XPath• http://www.w3.org/TR/xquery/XQuery 2.0
  • 4. FLWR ExpressionsWhat does FLWR stand forF - forL - letW - whereR - return
  • 5. XQueryFind all book titles published after 1995:FOR $x IN doc ("books")/bib/bookWHERE $x/year > 1995RETURN $x/titleFOR $x IN doc ("books")/bib/bookWHERE $x/year > 1995RETURN $x/titleResult:<title> abc </title><title> def </title><title> ghi </title>
  • 6. XQueryFor each author of a book by MorganKaufmann, list all books she published:FOR $a IN distinct-values(doc ("books.xml")/bib/book[publisher=“Morgan Kaufmann”]/author)RETURN <result>$a,FOR $t IN /bib/book[author=$a]/titleRETURN $t</result>FOR $a IN distinct-values(doc ("books.xml")/bib/book[publisher=“Morgan Kaufmann”]/author)RETURN <result>$a,FOR $t IN /bib/book[author=$a]/titleRETURN $t</result>distinct = a function that eliminates duplicates
  • 7. XQueryResult:<result><author>Jones</author><title> abc </title><title> def </title></result><result><author> Smith </author><title> ghi </title></result>
  • 8. XQuery• FOR $x in expr -- binds $x to each elementin the list expr• LET $x = expr -- binds $x to the entire listexpr– Useful for common subexpressions and foraggregations
  • 9. XQuerycount = a (aggregate) function that returns the number of elms<big_publishers>FOR $p IN distinct-values(doc ("books.xml")//publisher)LET $b := doc ("books.xml")/book[publisher = $p]WHERE count($b) > 100RETURN $p</big_publishers><big_publishers>FOR $p IN distinct-values(doc ("books.xml")//publisher)LET $b := doc ("books.xml")/book[publisher = $p]WHERE count($b) > 100RETURN $p</big_publishers>
  • 10. XQueryFind books whose price is larger thanaverage:LET $a=avg(doc(“books.xml")/bib/book/@price)FOR $b in doc("books.xml")/bib/bookWHERE $b/@price > $aRETURN $bLET $a=avg(doc(“books.xml")/bib/book/@price)FOR $b in doc("books.xml")/bib/bookWHERE $b/@price > $aRETURN $b
  • 11. XQuerySummary:• FOR-LET-WHERE-RETURN = FLWRFOR/LET ClausesWHERE ClauseRETURN ClauseList of tuplesList of tuplesInstance of Xquery data model
  • 12. FOR v.s. LETFOR• Binds node variables  iterationLET• Binds collection variables  one value
  • 13. FOR v.s. LETFOR $x IN doc ("books.xml")/bib/bookRETURN <result> $x </result>FOR $x IN doc ("books.xml")/bib/bookRETURN <result> $x </result>Returns:<result> <book>...</book></result><result> <book>...</book></result><result> <book>...</book></result>...LET $x := doc ("books.xml")/bib/bookRETURN <result> $x </result>LET $x := doc ("books.xml")/bib/bookRETURN <result> $x </result>Returns:<result> <book>...</book><book>...</book><book>...</book>...</result>
  • 14. Collections in XQuery• Ordered and unordered collections– /bib/book/author = an ordered collection– Distinct-values(/bib/book/author) = an unorderedcollection• 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>
  • 15. 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!!
  • 16. Sorting in XQuery<publisher_list>FOR $p IN distinct-values(doc("books.xml")//publisher)RETURN <publisher> <name> $p/text() </name> ,FOR $b IN doc("books.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-values(doc("books.xml")//publisher)RETURN <publisher> <name> $p/text() </name> ,FOR $b IN doc("books.xml")//book[publisher = $p]RETURN <book>$b/title ,$b/@price</book> SORTBY(price DESCENDING)</publisher> SORTBY(name)</publisher_list>
  • 17. Sorting in XQuery• Sorting arguments: refer to the name spaceof the RETURN clause, not the FOR clause• To sort on an element you don’t want todisplay, first return it, then remove it withan additional query.
  • 18. If-Then-ElseFOR $h IN //holdingRETURN <holding>$h/title,IF $h/@type = "Journal"THEN $h/editorELSE $h/author</holding> SORTBY (title)FOR $h IN //holdingRETURN <holding>$h/title,IF $h/@type = "Journal"THEN $h/editorELSE $h/author</holding> SORTBY (title)
  • 19. Other Stuff in XQuery• BEFORE and AFTER– for dealing with order in the input• FILTER– deletes some edges in the result tree• Recursive functions– Currently: arbitrary recursion– Perhaps more restrictions in the future ?
  • 20. Group-By in Xquery ??• No GROUPBY currently in XQuery• A recent proposal (next)– What do YOU think ?
  • 21. Group-By in Xquery ??FOR $b IN doc ("http://www.bn.com")/bib/book,$y IN $b/@yearWHERE $b/publisher="Morgan Kaufmann"RETURN GROUPBY $yWHERE count($b) > 10IN <year> $y </year>FOR $b IN doc ("http://www.bn.com")/bib/book,$y IN $b/@yearWHERE $b/publisher="Morgan Kaufmann"RETURN GROUPBY $yWHERE count($b) > 10IN <year> $y </year>SELECT yearFROM BibWHERE Bib.publisher="Morgan Kaufmann"GROUPBY yearHAVING count(*) > 10SELECT yearFROM BibWHERE Bib.publisher="Morgan Kaufmann"GROUPBY yearHAVING count(*) > 10 with GROUPBYEquivalent SQL 
  • 22. Group-By in Xquery ??FOR $b IN doc("http://www.bn.com")/bib/book,$a IN $b/author,$y IN $b/@yearRETURN GROUPBY $a, $yIN <result> $a,<year> $y </year>,<total> count($b) </total></result>FOR $b IN doc("http://www.bn.com")/bib/book,$a IN $b/author,$y IN $b/@yearRETURN GROUPBY $a, $yIN <result> $a,<year> $y </year>,<total> count($b) </total></result>FOR $Tup IN distinct-values(FOR $b IN doc("http://www.bn.com")/bib,$a IN $b/author,$y IN $b/@yearRETURN <Tup> <a> $a </a> <y> $y </y> </Tup>),$a IN $Tup/a/node(),$y IN $Tup/y/node()LET $b = doc("http://www.bn.com")/bib/book[author=$a,@year=$y]RETURN <result> $a,<year> $y </year>,<total> count($b) </total></result>FOR $Tup IN distinct-values(FOR $b IN doc("http://www.bn.com")/bib,$a IN $b/author,$y IN $b/@yearRETURN <Tup> <a> $a </a> <y> $y </y> </Tup>),$a IN $Tup/a/node(),$y IN $Tup/y/node()LET $b = doc("http://www.bn.com")/bib/book[author=$a,@year=$y]RETURN <result> $a,<year> $y </year>,<total> count($b) </total></result> with GROUPBYWithout GROUPBY 
  • 23. Group-By in Xquery ??FOR $b IN doc("http://www.bn.com")/bib/book,$a IN $b/author,$y IN $b/@year,$t IN $b/title,$p IN $b/publisherRETURNGROUPBY $p, $yIN <result> $p,<year> $y </year>,GROUPBY $aIN <authorEntry>$a,GROUPBY $tIN $t<authorEntry></result> Nested GROUPBY’s