Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 XQUERY

2 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 tables. XQuery uses XPath expressions to extract XML data. XQuery is defined by the W3C. XQuery is supported by all the major database engines (IBM, Oracle, Microsoft, etc.) XQuery 1.0 is not yet a W3C Recommendation (XQuery is a Working Draft). Hopefully it will be a recommendation in the near future.

3 XPath As we learned in previous lectures, XPath is a language for finding information in an XML document. XPath is used to navigate through elements and attributes in an XML document. For example /html/body/h1. //book[author = "Hunter"] //book/title |

4 XQuery Example doc("books.xml")/bookstore/book/title The doc() function is used to open the "books.xml" file /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 Everyday Italian Harry Potter XQuery Kick Start Learning XML

5 FLWOR Expressions For variables that range over the results of XPath expressions. If more than 1 variable, Cartesian product of values taken by the variables. Let allows complicated expressions to be assigned to variable names (for simplicity) Where test on tuples given by the ‘for’ clause. Order by sorts the result (ascending or descending) Return construction of results (in XML)

6 FLWOR Expressions(cont’d) 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 sort by the title element. The return clause specifies what should be returned. Here it returns the title elements.

7 XQuery FLWOR + 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: { for $x in doc("books.xml")/bookstore/book/title order by $x return {$x} } The result of the above will be: Everyday Italian Harry Potter Learning XML XQuery Kick Start

8 XQuery FLWOR + HTML(Cont) Now 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 will be (an HTML list): Everyday Italian Harry Potter Learning XML XQuery Kick Start

9 XQuery Syntax Some basic syntax rules: 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 :)

10 XQuery Conditional Expressions If-Then-Else" expressions are allowed in XQuery. for $x in doc("books.xml")/bookstore/book return if 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

11 Additional Syntax&Expressions To loop a specific number of times in a for clause, you may use the to keyword: for $x in (1 to 5) return {$x} Result: The at keyword can be used to count the iteration: for $x at $i in doc("books.xml")/bookstore/book/title return {$i}. {data($x)} Result: 1. Everyday Italian 2. Harry Potter 3. XQuery Kick Start 4. Learning XML

12 Performing Join in XQuery We can perform join operations just like in SQL. Multiple XML files are extracted by ‘doc()’ function. { for $b in document("http://www.bn.com/bib.xml")//book, $a in document("http://www.amazon.com/reviews.xml")//entry where $b/title = $a/title return { $b/title } { $a/price/text() } { $b/price/text() } }

13 XQuery Functions Built-in Functions XQuery includes over 100 built-in functions. There are functions for string values, numeric values, date and time comparison, node and QName manipulation, sequence manipulation, Boolean values, and more. {uppercase($booktitle)} doc("books.xml")/bookstore/book[substring(title,1,5='Harry')] let $name := (substring($booktitle,1,4)) You can also define your own functions in XQuery.

14 XQuery Functions (cont’d) User Defined Functions declare function local:minPrice( $price as xs:decimal?, $discount as xs:decimal?) AS xs:decimal? { let $disc := ($price * $discount) div 100 return ($price - $disc) }; (: Below is an example of how to call the function above :) {local:minPrice($book/price, $book/discount)}

15 Conclusion XQuery is used to query XML files. XQuery is similar to XSLT, but is different in several ways: XSLT is written in a XSL file which is later connected to a XML file. XQuery gets the XML file dynamically. It allows many XML files to be included in a single query. The join operations can be performed in XQuery whereas thay cannot be performed in XSLT.


Download ppt "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."

Similar presentations


Ads by Google