Presentation is loading. Please wait.

Presentation is loading. Please wait.

XQUERY The contents of this slide are obtained from various sources including, Wikipedia, W3School, Stanford website etc. January 2011 Dr.Kwanchai Eurviriyanukul.

Similar presentations


Presentation on theme: "XQUERY The contents of this slide are obtained from various sources including, Wikipedia, W3School, Stanford website etc. January 2011 Dr.Kwanchai Eurviriyanukul."— Presentation transcript:

1 XQUERY The contents of this slide are obtained from various sources including, Wikipedia, W3School, Stanford website etc. January 2011 Dr.Kwanchai Eurviriyanukul RMUTL, ChiangMai, Thailand kwanchai@rmutl.ac.th 1

2 Books.xml=>How many books? Everyday Italian Giada De Laurentiis 2005 30.00 Harry Potter J K. Rowling 2005 29.99 XQuery Kick Start James McGovern Per Bothner Kurt Cagle James Linn Vaidyanathan Nagarajan 2003 49.99 Learning XML Erik T. Ray 2003 39.95 2

3 The structure of bookstore 3

4 DTD of this bookstore? 4

5 Error: No File EXIST 5

6 Xpath-review: /bookstore/book 6

7 Xpath-review: /bookstore/book =>How many books? $xpathExpression =/bookstore/book ---------------------------------------------------------- ---------------------- Array ( [0] => SimpleXMLElement Object ( [@attributes] => Array ( [category] => COOKING ) [title] => Everyday Italian [author] => Giada De Laurentiis [year] => 2005 [price] => 30.00 ) [1] => SimpleXMLElement Object ( [@attributes] => Array ( [category] => CHILDREN ) [title] => Harry Potter [author] => J K. Rowling [year] => 2005 [price] => 29.99 ) [2] => SimpleXMLElement Object ( [@attributes] => Array ( [category] => WEB ) [title] => XQuery Kick Start [author] => Array ( [0] => James McGovern [1] => Per Bothner [2] => Kurt Cagle [3] => James Linn [4] => Vaidyanathan Nagarajan ) [year] => 2003 [price] => 49.99 ) [3] => SimpleXMLElement Object ( [@attributes] => Array ( [category] => WEB ) [title] => Learning XML [author] => Erik T. Ray [year] => 2003 [price] => 39.95 ) 7

8 Xpath-review: /bookstore/book => books with price > 30? $xpathExpression =/bookstore/book -------------------------------------------------------------------------------- Array ( [0] => SimpleXMLElement Object ( [@attributes] => Array ( [category] => COOKING ) [title] => Everyday Italian [author] => Giada De Laurentiis [year] => 2005 [price] => 30.00 ) [1] => SimpleXMLElement Object ( [@attributes] => Array ( [category] => CHILDREN ) [title] => Harry Potter [author] => J K. Rowling [year] => 2005 [price] => 29.99 ) [2] => SimpleXMLElement Object ( [@attributes] => Array ( [category] => WEB ) [title] => XQuery Kick Start [author] => Array ( [0] => James McGovern [1] => Per Bothner [2] => Kurt Cagle [3] => James Linn [4] => Vaidyanathan Nagarajan ) [year] => 2003 [price] => 49.99 ) [3] => SimpleXMLElement Object ( [@attributes] => Array ( [category] => WEB ) [title] => Learning XML [author] => Erik T. Ray [year] => 2003 [price] => 39.95 ) 8

9 Xpath-review: /bookstore/book/title => How many titles? $xpathExpression =/bookstore/book/title -------------------------------------------------------------------------------- Array ( [0] => SimpleXMLElement Object ( [@attributes] => Array ( [lang] => en ) [0] => Everyday Italian ) [1] => SimpleXMLElement Object ( [@attributes] => Array ( [lang] => en ) [0] => Harry Potter ) [2] => SimpleXMLElement Object ( [@attributes] => Array ( [lang] => en ) [0] => XQuery Kick Start ) [3] => SimpleXMLElement Object ( [@attributes] => Array ( [lang] => en ) [0] => Learning XML ) 9

10 /bookstore/book[price>30]/title How many titles? 10

11 /bookstore/book[price>30]/title How many titles? $xpathExpression =/bookstore/book[price>30]/title ---------------------------------------------------------------- Array ( [0] => SimpleXMLElement Object ( [@attributes] => Array ( [lang] => en ) [0] => XQuery Kick Start ) [1] => SimpleXMLElement Object ( [@attributes] => Array ( [lang] => en ) [0] => Learning XML ) 11

12 EX-1: Book title 1.for $x in doc("books.xml")/bookstore/book 2.where $x/price>30 3.order by $x/title 4.return $x/title What will be returned? How many (book, title) will be returned? 12

13 EX-1: Book title 1.for $x in doc("books.xml")/bookstore/book 2.where $x/price>30 3.order by $x/title 4.return $x/title Learning XML XQuery Kick Start 13

14 Compare the results of XPath/XQuery IDENTICAL!!! /bookstore/book[price>30]/title COMPARE WITH 1.for $x in doc("books.xml")/bookstore/book 2.where $x/price>30 3.order by $x/title 4.return $x/title Learning XML XQuery Kick Start 14

15 ทำไมต้อง XQuery แทน XPath 1. คืนค่าเป็นเอกสาร XML 2. สามารถจัดการกับผลลัพธ์ได้ เช่น เรียงลำดับ การแสดงผลลัพธ์ตามตัวอักษร 3. มี Function ใกล้เคียงกับ SQL – SUM – AVG 15

16 Compare the results of XPath/XQuery IDENTICAL!!! => So why do we need XQUERY => Because it has SQL functionality and can manage the result of a query (can manage XML data) /bookstore/book[price>30]/title COMPARE WITH 1.for $x in doc("books.xml")/bookstore/book 2.where $x/price>30 3.order by $x/title 4.return $x/title Learning XML XQuery Kick Start 16

17 17 FLWR Expressions Expression for XQuery 1.One or more for and/or let clauses. 2.Then an optional where clause. 3.A return clause.

18 18 Example: FOR for $beer in document(”bars.xml”)/BARS/BEER/@name return {$beer} $beer ranges over the name attributes of all beers in our example document. Result is a sequence of BEERNAME elements: Bud Miller... “Expand the en- closed string by replacing variables and path exps. by their values.” Our example BARS document

19 ปีกกา ในการแทรก ตัวแปร / ผลลัพธ์ของ XQuery เราต้องแทรกด้วย เครื่องหมายปีกกานะครับ จึงจะคืนผลลัพธ์ได้ เช่น {for $plist in doc("bookstore.xml")/Bookstore/Book/@Price return {$plist} } จะได้ผลลัพธ์ดังนี้ Query: <>, line 0, column 0: [XQDY0025] An attribute of a constructed element has name "Price", which is not distinct from the names of all other attributes of the constructed element. 19

20 ปีกกา ในการแทรก ตัวแปร / ผลลัพธ์ของ XQuery เราต้องแทรกด้วย เครื่องหมายปีกกานะครับ จึงจะคืนผลลัพธ์ได้ เช่น {for $plist in doc("bookstore.xml")/Bookstore/Book/@Price return {$plist} } จะได้ผลลัพธ์ดังนี้ 20

21 FLWOR expressions for $x in doc("books.xml")/bookstore/book where $x/price>30 order by $x/title return $x/title for - (optional) binds a variable to each item returned by the in expression let - (optional) where - (optional) specifies a criteria order by - (optional) specifies the sort-order of the result return - specifies what to return in the result 21

22 XQuery+HTML { for $x in doc("books.xml")/bookstore/book/title order by $x return {$x} } 22

23 Xquery+HTML Results Everyday Italian Harry Potter Learning XML XQuery Kick Start 23

24 XQuery Terminology XML documents are treated as trees of nodes Seven kinds of nodes: element, attribute, text, namespace, processing-instruction, comment, and document (root) nodes. XML document example: 24

25 XML document Harry Potter J K. Rowling 2005 29.99 25

26 Nodes (document node) J K. Rowling (element node) lang="en" (attribute node) 26

27 Atomic values Atomic values are nodes with no children or parent. Example of atomic values: J K. Rowling "en" 27

28 XQuery Basic Syntax Rules 1.XQuery is case-sensitive 2.XQuery elements, attributes, and variables must be valid XML names 3.An XQuery string value can be in single or double quotes 4.An XQuery variable is defined with a $ followed by a name, e.g. $bookstore 5.XQuery comments are delimited by (: and :), e.g. (: XQuery Comment :) 28

29 29 Getting Data From Elements Suppose we want to compare the values of elements, rather than their location in documents. To extract just the value (e.g., the price itself) from an element E, use data(E ) TO CREATE NEW ELEMENTS

30 IF-Else (: XQuery main module :) for $x in doc("books.xml")/bookstore/book return if ($x/@category="CHILDREN") then {data($x/title)} else {data($x/title)} 30

31 IF-Else (: XQuery main module :) for $x in doc("books.xml")/bookstore/book return if ($x/@category="CHILDREN") then {data($x/title)} else {data($x/title)} Everyday Italian Harry Potter XQuery Kick Start Learning XML 31

32 The structure of bookstore 32

33 XQuery-HTML Test: We need this result 1. 2. Bookstore 3. Everyday Italian. Category: COOKING 4. Harry Potter. Category: CHILDREN 5. Learning XML. Category: WEB 6. XQuery Kick Start. Category: WEB 7. 33

34 XQuery-HTML (: XQuery main module :) Bookstore { for $x in doc("books.xml")/bookstore/book order by $x/title return {data($x/title)}. Category: {data($x/@category)} } 34

35 XQuery-HTML (: XQuery main module :) Bookstore { for $x in doc("books.xml")/bookstore/book order by $x/title return {data($x/title)}. Category: {data($x/@category)} } 35

36 XQuery-HTML Cont.-1 { for $x in doc("books.xml")/bookstore/book order by $x/title return {data($x/title)}. Category: {data($x/@category)} } Bookstore Everyday Italian. Category: COOKING Harry Potter. Category: CHILDREN Learning XML. Category: WEB XQuery Kick Start. Category: WEB 36

37 37 Quantifier Expressions some $x in E 1 satisfies E 2 1.Evaluate the sequence E 1. 2.Let $x (any variable) be each item in the sequence, and evaluate E 2. 3.Return TRUE if E 2 has EBV TRUE for at least one $x. Analogously: every $x in E 1 satisfies E 2

38 38 Example: Some The bars that sell at least one beer for less than $2. for $bar in doc(”bars.xml”)/BARS/BAR where some $p in $bar/PRICE satisfies $p < 2.00 return $bar/@name Notice: where $bar/PRICE < 2.00 would work as well.

39 Bookstore.dtd (obtained from CS145 Stanford) <!DOCTYPE Bookstore [ <!ATTLIST Book ISBN CDATA #REQUIRED Price CDATA #REQUIRED Edition CDATA #IMPLIED> ]> 39

40 SIMPLE FLWOR QUERY Titles of books costing less than $90 where "Ullman" is an author’s Last_Name. for $b in doc("bookstore.xml")/Bookstore/Book where $b/@Price < 90 and $b/Authors/Author/Last_Name = "Ullman" return $b/Title 40

41 SIMPLE FLWOR QUERY Titles of books costing less than $90 where "Ullman" is an author ’s Last_Name. for $b in doc("bookstore.xml")/Bookstore/Book where $b/@Price < 90 and $b/Authors/Author/Last_Name = "Ullman" return $b/Title A First Course in Database Systems Hector and Jeff's Database Hints 41

42 42 LET Clauses let :=,... Value of the variable becomes the sequence of items defined by the expression. Note let does not cause iteration; for does.

43 43 Example: LET let $d := document(”bars.xml”) let $beers := $d/BARS/BEER/@name return {$beers} Returns one element with all the names of the beers, like: Bud Miller …

44 Let example { let $plist := doc("bookstore.xml")/Bookstore/Book/@Price return $plist } 44

45 Let example { let $plist := doc("bookstore.xml")/Bookstore/Book/@Price return $plist } 45

46 A First Course in Database Systems Jeffrey Ullman Jennifer Widom Database Systems: The Complete Book Hector Garcia- Molina Jeffrey Ullman Jennifer Widom Amazon.com says: Buy this book bundled with "A First Course," it's a great deal! Hector and Jeff's Database Hints Jeffrey Ullman Hector Garcia- Molina An indispensible companion to your textbook Jennifer's Economical Database Hints Jennifer Widom National Geographic National Geographic Newsweek Hector and Jeff's Database Hints 46

47 $xpathExpression =/Bookstore/Book/@Price 47

48 Let example { let $plist := doc("bookstore.xml")/Bookstore/Book/@Price return $plist } Query: <>, line 0, column 0: [XQDY0025] An attribute of a constructed element has name "Price", which is not distinct from the names of all other attributes of the constructed element. 48

49 Let example Cont.-1 { let $plist := doc("bookstore.xml")/Bookstore/Book/@Price return data($plist) } 49

50 Let example Cont.-1 { let $plist := doc("bookstore.xml")/Bookstore/Book/@Price return data($plist) } 85 100 50 25 50

51 Let example Cont.-2 { let $plist := doc("bookstore.xml")/Bookstore/Book/@Price return avg($plist) } 51

52 Let example Cont.-2 { let $plist := doc("bookstore.xml")/Bookstore/Book/@Price return avg($plist) } 65 52

53 Let example Cont.-2 { let $plist := doc("bookstore.xml")/Bookstore/Book/@Price return avg($plist) } หรือ avg(data($plist)) ก็ ได้นะ 65 53

54 For กับ Let ต่างกันอย่างไร For จะเป็นการ Loop แต่ Let จะเป็นการกำหนดค่าตัวแปรเฉยๆ 54

55 Books whose price is below average (:************************************************************** * 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($plist) return { $b/Title } { $b/data(@Price) } 55

56 IT-Makers 56

57 XQuery-Questions-IT-makers 57

58 Battleships 58

59 XQuery-Questions-battle-ships 59

60 XQuery-Questions-battle-ships-2 60

61 XQuery was designed to query XML data XQuery 1.0 became a W3C Recommendation January 23, 2007 The doc() function is used to open the XML file, e.g. doc("books.xml") Path Expressions XQuery uses path expressions to navigate through elements in an XML document.

62 Predicates XQuery uses predicates to limit the extracted data from XML documents. XQuery can be put inside HTML or XML. To do that you need to put XQuery inside {….}

63 Example of using { … } XQuery return $laptop Result $laptop

64 Example of using { … } XQuery return {$laptop} Result $laptop 2.00 2048 240 20.1

65 Conclustion for { … } with XQuery Everytime we want to mix Xquery with XML or HTML, we need to put Xquery inside the curry bracket, i.e. { …… }. Otherwise, we will not get the correct result.

66 Laptop with price more than 2000 FLOWR LET $product := doc(“products.xml”) FOR $laptop in $product/maker/laptop WHERE $laptop/@price > 2000 ORDER BY RETURN $laptop 66

67 Laptop with price more than 2000 (: XQuery main module :) let $product := doc("products.xml") for $laptop in $product/Products/Maker/Laptop where $laptop/@price > 2000 return $laptop 67

68 Laptop with price more than 2000 2.16 1024 120 17.0 2.00 2048 240 20.1 68

69 Laptop with price more than 2000 (: XQuery main module :) let $product := doc("products.xml") for $laptop in $product//Laptop where $laptop/@price > 2000 return $laptop 69

70 Laptop with price more than 2000 order by model 1.(: XQuery main module :) 2.let $product := doc("products.xml") 3.for $laptop in $product//Laptop 4.where $laptop/@price > 2000 5.Order by $laptop/@model 6.return $laptop 70

71 Laptop with price more than 2000 order by model surrounded by 1.(: XQuery main module :) 2.let $product := doc("products.xml") 3.for $laptop in $product//Laptop 4.where $laptop/@price > 2000 5.Order by $laptop/@model 6.return $laptop 71

72 No curry bracket $laptop $laptop 72


Download ppt "XQUERY The contents of this slide are obtained from various sources including, Wikipedia, W3School, Stanford website etc. January 2011 Dr.Kwanchai Eurviriyanukul."

Similar presentations


Ads by Google