Presentation is loading. Please wait.

Presentation is loading. Please wait.

XQuery Or, what about REAL databases?. XQuery - its place in the XML team XLink XSLT XQuery XPath XPointer.

Similar presentations


Presentation on theme: "XQuery Or, what about REAL databases?. XQuery - its place in the XML team XLink XSLT XQuery XPath XPointer."— Presentation transcript:

1 XQuery Or, what about REAL databases?

2 XQuery - its place in the XML team XLink XSLT XQuery XPath XPointer

3 Path expressions in XQuery The following path expression is used to select all the title elements in the "books.xml" file:books.xml doc("books.xml")/bookstore/book/title (/bookstore selects the bookstore element, /book selects all the book elements under the bookstore element, and /title selects all the title elements under each book element) The XQuery above will extract the following: Everyday Italian Harry Potter XQuery Kick Start Learning XML

4 Some basic syntax rules of XQuery XQuery is case-sensitive XQuery elements, attributes, and variables must be valid XML names An XQuery string value can be in single or double quotes An XQuery variable is defined with a $ followed by a name, e.g. $bookstore XQuery comments are delimited by (: and :), e.g. (: XQuery Comment :)

5 Predicates XQuery uses predicates to limit the extracted data from XML documents. The following predicate is used to select all the book elements under the bookstore element that have a price element with a value that is less than 30: doc("books.xml")/bookstore/book[price<30] The XQuery above will extract the following: Harry Potter J K. Rowling 2005 29.99

6 Using FLWOR Look at the following path expression: doc("books.xml")/bookstore/book[price>30]/title The expression above will select all the title elements under the book elements that is under the bookstore element that have a price element with a value that is higher than 30. The following FLWOR expression will select exactly the same as the path expression above: for $x in doc("books.xml")/bookstore/book where $x/price>30 return $x/title The result will be: XQuery Kick Start Learning XML

7 FLWOR FLWOR is the main engine of XQuery: For-Let-Where-Order-Return (pronounced "flower") generalizes SELECT-FROM-HAVING-WHERE from SQL for $d in document("depts.xml")//deptno let $e := document("emps.xml")//employee[deptno = $d] where count($e) >= 10 order by avg($e/salary) descending return { $d, {count($e)}, {avg($e/salary)} } for generates an ordered list of bindings of deptno values to $d let associates to each binding a further binding of the list of emp elements with that department number to $e. A t this stage, we have an ordered list of tuples of bindings: ($d,$e) where filters that list to retain only the desired tuples order sorts that list by the given criteria return constructs for each tuple a resulting value The combined result is in this case a list of departments with at least 10 employees, sorted by average salaries.

8 Sorting with FLWOR With FLWOR you can sort the result: for $x in doc("books.xml")/bookstore/book where $x/price>30 order by $x/title return $x/title The for clause selects all book elements under the bookstore element into a variable called $x. The where clause selects only book elements with a price element with a value greater than 30. The order by clause defines the sort-order. Will be sorted by the title element. The return clause specifies what should be returned. Here it returns the title elements. The result of the XQuery expression above will be: Learning XML XQuery Kick Start

9 FLWOR and HTML Now we want to list all the book-titles in our bookstore in an HTML list. We add and tags to the FLWOR expression, and we want to eliminate the title element, and show only the data inside the title element: { for $x in doc("books.xml")/bookstore/book/title order by $x return {data($x}) } The result of the above will be: Everyday Italian Harry Potter Learning XML XQuery Kick Start

10 Extracting attributes Bookstore { for $x in doc("books.xml")/bookstore/book order by $x/title return {data($x/title)}. Category: {data($x/@category)} } The XQuery expression above will generate the following result: Bookstore Everyday Italian. Category: COOKING Harry Potter. Category: CHILDREN Learning XML. Category: WEB XQuery Kick Start. Category: WEB

11 XQuery Conditional Expressions "If-Then-Else" expressions are allowed in XQuery. Look at the following example: for $x in doc("books.xml")/bookstore/book return if ($x/@category="CHILDREN") then {data($x/title)} else {data($x/title)} The result of the example above will be: Everyday Italian Harry Potter Learning XML XQuery Kick Start


Download ppt "XQuery Or, what about REAL databases?. XQuery - its place in the XML team XLink XSLT XQuery XPath XPointer."

Similar presentations


Ads by Google