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

Slides:



Advertisements
Similar presentations
Ali Alshowaish w3schools. XML documents use a self-describing and simple syntax: The first line is the XML declaration. It defines the XML version (1.0)
Advertisements

Dr. Alexandra I. Cristea CS 253: Topics in Database Systems: XPath, NameSpaces.
XML, XML Schema, Xpath and XQuery Slides collated from various sources, many from Dan Suciu at Univ. of Washington.
XQuery John Annechino Steven Pow. Agenda What is XQuery? Uses of XQuery XQuery vs. XSLT Syntax –Built-In Functions –FLWOR –if-then-else –User-Defined.
XQuery Or, what about REAL databases?. XQuery - its place in the XML team XLink XSLT XQuery XPath XPointer.
XQUERY. What is XQuery? XQuery is the language for querying XML data The best way to explain XQuery is to say that XQuery is to XML what SQL is to database.
1 XQuery Web and Database Management System. 2 XQuery XQuery is to XML what SQL is to database tables XQuery is designed to query XML data What is XQuery?
Lecture 10. Back to the table example We can now return to considering in detail the first XSLT style-sheet we saw –The one which, when applied to the.
Friday, September 4 th, 2009 The Systems Group at ETH Zurich XML and Databases Exercise Session 6 courtesy of Ghislain Fourny/ETH © Department of Computer.
IS432: Semi-Structured Data Dr. Azeddine Chikh. 7. XQuery.
CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 357 Database Systems I Query Languages for XML.
1 COS 425: Database and Information Management Systems XML and information exchange.
Winter 2002Arthur Keller – CS 18018–1 Schedule Today: Mar. 12 (T) u Semistructured Data, XML, XQuery. u Read Sections Assignment 8 due. Mar. 14.
XML and The Relational Data Model
1 Before The Class What’re the roles of us in this subject? –A researcher –A developer –A problem solver –IT related technologies What’s really confused.
Fall 2001Arthur Keller – CS 18017–1 Schedule Nov. 27 (T) Semistructured Data, XML. u Read Sections Assignment 8 due. Nov. 29 (TH) The Real World,
1 XQuery Values FLWR Expressions Other Expressions.
Querying XML February 12 th, Querying XML Data XPath = simple navigation through the tree XQuery = the SQL of XML XSLT = recursive traversal –will.
Manohar – Why XML is Required Problem: We want to save the data and retrieve it further or to transfer over the network. This.
Introduction to XPath Bun Yue Professor, CS/CIS UHCL.
XP ATH - XML Path Language. W HAT IS XP ATH ? XPath, the XML Path Language, is a query language for selecting nodes from an XML document.query languagenodesXML.
XML Document Type Definitions XML Schema. Motivation for Semistructured data Serves as a model suitable for integration of databases Notations such as.
COP4540 Database Management System Final Review Reviewed by Ramakrishna. Parts of this are taken from Fernando Farfan’s presentation.
Introduction to XML and XQuery Guangjun (Kevin) Xie.
TDDD43 XML and RDF Slides based on slides by Lena Strömbäck and Fang Wei-Kleiner 1.
IS432 Semi-Structured Data Lecture 5: XSLT Dr. Gamal Al-Shorbagy.
XML TECHNOLOGY FAMILY XML XML stands for EXtensible Markup Language XML is a markup language much like HTML XML was designed to describe data[carry data],
XPath XPath is used to navigate through elements and attributes in an XML document. XPath is a major element in W3C's XSLT standard - and XQuery and XPointer.
1 Querying XML Documents. 2 Objectives How XML generalizes relational databases The XQuery language How XML may be supported in databases.
1 XPath XPath became a W3C Recommendation 16. November 1999 XPath is a language for finding information in an XML document XPath is used to navigate through.
VICTORIA UNIVERSITY OF WELLINGTON Te Whare Wananga o te Upoko o te Ika a Maui SWEN 432 Advanced Database Design and Implementation An Introduction to XQuery.
CISC 3140 (CIS 20.2) Design & Implementation of Software Application II Instructor : M. Meyer Address: Course Page:
Advance Database S Week-7 Dr.Kwanchai Eurviriyanukul
Lecture 22 XML querying. 2 Example 31.5 – XQuery FLWOR Expressions ‘=’ operator is a general comparison operator. XQuery also defines value comparison.
Winter 2006Keller, Ullman, Cushing18–1 Plan 1.Information integration: important new application that motivates what follows. 2.Semistructured data: a.
Unit 04 : XQuery COMP 5323 Web Database Technologies and Applications 2014.
1 XQuery uXQuery extends XPath to a query language that has power similar to SQL. uUses the same sequence-of-items data model. uXQuery is an expression.
Database Systems Part VII: XML Querying Software School of Hunan University
Namespace information are represented as namespace node which maps in scope on an element Attach to every element node where namespace is declared root.
[ Part III of The XML seminar ] Presenter: Xiaogeng Zhao A Introduction of XQL.
XML query. introduction An XML document can represent almost anything, and users of an XML query language expect it to perform useful queries on whatever.
XPath Presented by Kushan Athukorala. 2 Agenda XPath XPath Terminology Selecting Nodes Predicates.
Submitted To: Ms. Poonam Saini, Asst. Prof., NITTTR Submitted By: Rohit Handa ME (Modular) CSE 2011 Batch.
XML Query: xQuery Reference: Xquery By Priscilla Walmsley, Published by O’Reilly.
DTD ]>. Internal DTD Declaration ]> Faculty Director Reminder Please be regular in your classes.
Session II Chapter 3 – Chapter 3 – XPath Patterns & Expressions Chapter 4 – XPath Functions Chapter 15 – XPath 2.0http://
What is XLink? XLink is short for XML Linking Language XLink is used to create hyperlinks in XML documents Any element in an XML document can behave as.
IS432 Semi-Structured Data Lecture 6: XQuery Dr. Gamal Al-Shorbagy.
IS432 Semi-Structured Data Lecture 4: XPath Dr. Gamal Al-Shorbagy.
XML Query Languages XPATH XQUERY Zaki Malik November 11, 2008.
Friday, September 4 th, 2009 The Systems Group at ETH Zurich XML and Databases Exercise Session 5 courtesy of Ghislain Fourny/ETH © Department of Computer.
AJAX. Ajax  $.get  $.post  $.getJSON  $.ajax  json and xml  Looping over data results, success and error callbacks.
XPath --XML Path Language Motivation of XPath Data Model and Data Types Node Types Location Steps Functions XPath 2.0 Additional Functionality and its.
XML TECHNOLOGY FAMILY UNIT - I XML XML - Introduction.
CSE 6331 © Leonidas Fegaras XQuery 1 XQuery Leonidas Fegaras.
Lecture 17: XPath and XQuery Wednesday, Nov. 7, 2001.
1 XPath. 2 Agenda XPath Introduction XPath Nodes XPath Syntax XPath Operators XPath Q&A.
XML Introduction to XML Extensible Markup Language.
XML. What is XML? XML stands for EXtensible Markup Language XML is a markup language much like HTML XML was designed to carry data, not to display data.
XML Query languages--XPath. Objectives Understand XPath, and be able to use XPath expressions to find fragments of an XML document Understand tree patterns,
Query Languages for XML
XML: Extensible Markup Language
XML and XPath.
Querying and Transforming XML Data
Session IV Chapter 16 – XQuery
{ XML Technologies } BY: DR. M’HAMED MATAOUI
XQuery Leonidas Fegaras.
Session II Chapter 16 – XQuery
Query Languages for XML
Presentation transcript:

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