Presentation is loading. Please wait.

Presentation is loading. Please wait.

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?

Similar presentations


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

1 1 XQuery Web and Database Management System

2 2 XQuery XQuery is to XML what SQL is to database tables XQuery is designed to query XML data What is XQuery? – XQuery is the language for querying XML data – XQuery for XML is like SQL for databases – XQuery is built on XPath expressions – XQuery is supported by all major databases – XQuery is a W3C Recommendation

3 3 XQuery - Examples of Use XQuery can be used to: –Extract information to use in a Web Service –Generate summary reports –Transform XML data to XHTML –Search Web documents for relevant information

4 4 How to Select Nodes From "books.xml"? Functions –XQuery uses functions to extract data from XML documents. –The doc() function is used to open the "books.xml" file: 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 docs doc("books.xml") doc("books.xml")/bookstore/book/title doc("books.xml")/bookstore/book[price<30]

5 5 FLWOR Expressions FLWOR is an acronym for "For, Let, Where, Order by, Return" –for clause selects all book elements under the bookstore element into a variable called $x. –where clause selects only book elements with a price element with a value greater than 30. –order by clause defines the sort-order. Will be sort by the title element. –return clause specifies what should be returned. Here it returns the title elements. for $x in doc("books.xml")/bookstore/book where $x/price>30 order by $x/title return $x/title

6 6 FLWOR Expressions (cont) The above XQuery expression will yield the result: Which is equivalent to the path expression: doc("books.xml")/bookstore/book[price>30]/title Learning XML XQuery Kick Start

7 7 Present Result In HTML Adding HTML tags to generate neatly result Result { for $x in doc("books.xml")/bookstore/book/title order by $x return {$x} } Everyday Italian Harry Potter Learning XML XQuery Kick Start

8 8 Present Result In HTML (cont) Using data() to strip out XML tags Result Everyday Italian Harry Potter Learning XML XQuery Kick Start { for $x in doc("books.xml")/bookstore/book/title order by $x return {data($x)} }

9 9 XQuery Basic Syntax Rules 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 10 XQuery Conditional Expressions "If-Then-Else" expressions are allowed in XQuery The result will be: for $x in doc("books.xml")/bookstore/book return if ($x/@category="CHILDREN") then {data($x/title)} else {data($x/title)} Notes on the "if-then-else" syntax: parentheses around the if expression are required. else is required, but it can be just else(). Everyday Italian Harry Potter Learning XML XQuery Kick Start

11 11 XQuery Comparisons In XQuery there are two ways of comparing values: 1. General comparisons: =, !=,, >= 2. Value comparisons: eq, ne, lt, le, gt, ge Difference between two comparison methods are: –The following expression returns true if any q attributes have a value greater than 10: –The following expression returns true if there is only one q attribute returned by the expression, and its value is greater than 10. If more than one q is returned, an error occurs: $bookstore//book/@q > 10 $bookstore//book/@q gt 10

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

13 13 Adding Attributes Add Attributes to HTML Elements XQuery expression above will generate: { for $x in doc("books.xml")/bookstore/book order by $x/title return {data($x/title)} } Everyday Italian Harry Potter Learning XML XQuery Kick Start

14 14 Selecting and Filtering for Clause –for clause binds a variable to each item returned by in expression. –for clause results in iteration. –There can be multiple for clauses in the same FLWOR expression. –To loop a specific number of times in for clause, we may use to keyword: –Result: for $x in (1 to 5) return {$x} 1 2 3 4 5

15 15 for Clause (cont) The at keyword can be used to count the iteration: Result: for $x at $i in doc("books.xml")/bookstore/book/title return {$i}. {data($x)} 1. Everyday Italian 2. Harry Potter 3. XQuery Kick Start 4. Learning XML

16 16 for Clause (cont) It is also allowed with more than one in expression in the for clause. Use comma to separate each in expression: Result: for $x in (10,20), $y in (100,200) return x={$x} and y={$y} x=10 and y=100 x=10 and y=200 x=20 and y=100 x=20 and y=200

17 17 let Clause let clause allows variable assignments It avoids repeating the same expression many times. The let clause does not result in iteration Return let $x := (1 to 5) return {$x} 1 2 3 4 5

18 18 where Clause where clause is used to specify one or more criteria for the result: Only the books with the price between 31 and 99 will be selected: where $x/price>30 and $x/price<100

19 19 order by Clause order by clause is used to specify the sort order of the result. The result will be sorted by category and title as: for $x in doc("books.xml")/bookstore/book order by $x/@category, $x/title return $x/title Harry Potter Everyday Italian Learning XML XQuery Kick Start

20 20 return clause return clause specifies what is to be returned. Result: for $x in doc("books.xml")/bookstore/book return $x/title Everyday Italian Harry Potter XQuery Kick Start Learning XML

21 21 XQuery Functions XQuery 1.0, XPath 2.0, and XSLT 2.0 share the same functions library. A call to function can appear where expression may appear 1: In an element 2: In the predicate of a path expression 3: In a let clause {uppercase($booktitle)} doc("books.xml")/bookstore/book[substring(title,1,5)='Harry'] let $name := (substring($booktitle,1,4))

22 22 User-Defined Functions User-defined functions can be defined in the query or in a separate library Notes on user-defined functions: –Use the declare function keyword –The name of the function must be prefixed –The data type of the parameters are mostly the same as the data types defined in XML Schema –The body of the function must be surrounded by curly braces declare function prefix:function_name($parameter as datatype, …) as returnDatatype {...function code here... } Syntax:

23 23 Example of User-defined Function This is an example of a User-defined Function Declared in the Query And this is an example of how to call the function above: declare function local:minPrice($p as xs:float, $d as xs:floatl) as xs:float { let $disc := ($p * $d) div 100 return ($p - $disc) } ; {local:minPrice($book/price, $book/discount)}


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

Similar presentations


Ads by Google