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 1
Books.xml=>How many books? Everyday Italian Giada De Laurentiis Harry Potter J K. Rowling XQuery Kick Start James McGovern Per Bothner Kurt Cagle James Linn Vaidyanathan Nagarajan Learning XML Erik T. Ray
The structure of bookstore 3
DTD of this bookstore? 4
Error: No File EXIST 5
Xpath-review: /bookstore/book 6
Xpath-review: /bookstore/book =>How many books? $xpathExpression =/bookstore/book Array ( [0] => SimpleXMLElement Object ( => Array ( [category] => COOKING ) [title] => Everyday Italian [author] => Giada De Laurentiis [year] => 2005 [price] => ) [1] => SimpleXMLElement Object ( => Array ( [category] => CHILDREN ) [title] => Harry Potter [author] => J K. Rowling [year] => 2005 [price] => ) [2] => SimpleXMLElement Object ( => 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] => ) [3] => SimpleXMLElement Object ( => Array ( [category] => WEB ) [title] => Learning XML [author] => Erik T. Ray [year] => 2003 [price] => ) 7
Xpath-review: /bookstore/book => books with price > 30? $xpathExpression =/bookstore/book Array ( [0] => SimpleXMLElement Object ( => Array ( [category] => COOKING ) [title] => Everyday Italian [author] => Giada De Laurentiis [year] => 2005 [price] => ) [1] => SimpleXMLElement Object ( => Array ( [category] => CHILDREN ) [title] => Harry Potter [author] => J K. Rowling [year] => 2005 [price] => ) [2] => SimpleXMLElement Object ( => 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] => ) [3] => SimpleXMLElement Object ( => Array ( [category] => WEB ) [title] => Learning XML [author] => Erik T. Ray [year] => 2003 [price] => ) 8
Xpath-review: /bookstore/book/title => How many titles? $xpathExpression =/bookstore/book/title Array ( [0] => SimpleXMLElement Object ( => Array ( [lang] => en ) [0] => Everyday Italian ) [1] => SimpleXMLElement Object ( => Array ( [lang] => en ) [0] => Harry Potter ) [2] => SimpleXMLElement Object ( => Array ( [lang] => en ) [0] => XQuery Kick Start ) [3] => SimpleXMLElement Object ( => Array ( [lang] => en ) [0] => Learning XML ) 9
/bookstore/book[price>30]/title How many titles? 10
/bookstore/book[price>30]/title How many titles? $xpathExpression =/bookstore/book[price>30]/title Array ( [0] => SimpleXMLElement Object ( => Array ( [lang] => en ) [0] => XQuery Kick Start ) [1] => SimpleXMLElement Object ( => Array ( [lang] => en ) [0] => Learning XML ) 11
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
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
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
ทำไมต้อง XQuery แทน XPath 1. คืนค่าเป็นเอกสาร XML 2. สามารถจัดการกับผลลัพธ์ได้ เช่น เรียงลำดับ การแสดงผลลัพธ์ตามตัวอักษร 3. มี Function ใกล้เคียงกับ SQL – SUM – AVG 15
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 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 Example: FOR for $beer in 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
ปีกกา ในการแทรก ตัวแปร / ผลลัพธ์ของ XQuery เราต้องแทรกด้วย เครื่องหมายปีกกานะครับ จึงจะคืนผลลัพธ์ได้ เช่น {for $plist in 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
ปีกกา ในการแทรก ตัวแปร / ผลลัพธ์ของ XQuery เราต้องแทรกด้วย เครื่องหมายปีกกานะครับ จึงจะคืนผลลัพธ์ได้ เช่น {for $plist in return {$plist} } จะได้ผลลัพธ์ดังนี้ 20
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
XQuery+HTML { for $x in doc("books.xml")/bookstore/book/title order by $x return {$x} } 22
Xquery+HTML Results Everyday Italian Harry Potter Learning XML XQuery Kick Start 23
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
XML document Harry Potter J K. Rowling
Nodes (document node) J K. Rowling (element node) lang="en" (attribute node) 26
Atomic values Atomic values are nodes with no children or parent. Example of atomic values: J K. Rowling "en" 27
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 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
IF-Else (: XQuery main module :) for $x in doc("books.xml")/bookstore/book return if then {data($x/title)} else {data($x/title)} 30
IF-Else (: XQuery main module :) for $x in doc("books.xml")/bookstore/book return if then {data($x/title)} else {data($x/title)} Everyday Italian Harry Potter XQuery Kick Start Learning XML 31
The structure of bookstore 32
XQuery-HTML Test: We need this result Bookstore 3. Everyday Italian. Category: COOKING 4. Harry Potter. Category: CHILDREN 5. Learning XML. Category: WEB 6. XQuery Kick Start. Category: WEB 7. 33
XQuery-HTML (: XQuery main module :) Bookstore { for $x in doc("books.xml")/bookstore/book order by $x/title return {data($x/title)}. Category: } 34
XQuery-HTML (: XQuery main module :) Bookstore { for $x in doc("books.xml")/bookstore/book order by $x/title return {data($x/title)}. Category: } 35
XQuery-HTML Cont.-1 { for $x in doc("books.xml")/bookstore/book order by $x/title return {data($x/title)}. Category: } Bookstore Everyday Italian. Category: COOKING Harry Potter. Category: CHILDREN Learning XML. Category: WEB XQuery Kick Start. Category: WEB 36
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 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 Notice: where $bar/PRICE < 2.00 would work as well.
Bookstore.dtd (obtained from CS145 Stanford) <!DOCTYPE Bookstore [ <!ATTLIST Book ISBN CDATA #REQUIRED Price CDATA #REQUIRED Edition CDATA #IMPLIED> ]> 39
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 < 90 and $b/Authors/Author/Last_Name = "Ullman" return $b/Title 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 < 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 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 Example: LET let $d := document(”bars.xml”) let $beers := return {$beers} Returns one element with all the names of the beers, like: Bud Miller …
Let example { let $plist := return $plist } 44
Let example { let $plist := return $plist } 45
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
$xpathExpression 47
Let example { let $plist := 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
Let example Cont.-1 { let $plist := return data($plist) } 49
Let example Cont.-1 { let $plist := return data($plist) }
Let example Cont.-2 { let $plist := return avg($plist) } 51
Let example Cont.-2 { let $plist := return avg($plist) } 65 52
Let example Cont.-2 { let $plist := return avg($plist) } หรือ avg(data($plist)) ก็ ได้นะ 65 53
For กับ Let ต่างกันอย่างไร For จะเป็นการ Loop แต่ Let จะเป็นการกำหนดค่าตัวแปรเฉยๆ 54
Books whose price is below average (:************************************************************** * LET AND FOR TOGETHER, ATTRIBUTES TURNED INTO ELEMENTS Books whose price is below average *************************************************************** *:) let $plist := for $b in doc("bookstore.xml")/Bookstore/Book where < avg($plist) return { $b/Title } { } 55
IT-Makers 56
XQuery-Questions-IT-makers 57
Battleships 58
XQuery-Questions-battle-ships 59
XQuery-Questions-battle-ships-2 60
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.
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 {….}
Example of using { … } XQuery return $laptop Result $laptop
Example of using { … } XQuery return {$laptop} Result $laptop
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.
Laptop with price more than 2000 FLOWR LET $product := doc(“products.xml”) FOR $laptop in $product/maker/laptop WHERE > 2000 ORDER BY RETURN $laptop 66
Laptop with price more than 2000 (: XQuery main module :) let $product := doc("products.xml") for $laptop in $product/Products/Maker/Laptop where > 2000 return $laptop 67
Laptop with price more than
Laptop with price more than 2000 (: XQuery main module :) let $product := doc("products.xml") for $laptop in $product//Laptop where > 2000 return $laptop 69
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 > Order by 6.return $laptop 70
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 > Order by 6.return $laptop 71
No curry bracket $laptop $laptop 72