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?

  • Published on
    16-Dec-2015

  • View
    214

  • Download
    0

Transcript

  • Slide 1
  • 1 XQuery Web and Database Management System
  • Slide 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
  • Slide 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
  • Slide 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
  • 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
  • Slide 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
  • Slide 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)} }
  • Slide 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 :)
  • Slide 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
  • Slide 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
  • Slide 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
  • Slide 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
  • Slide 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
  • Slide 15
  • Slide 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
  • Slide 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
  • Slide 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

Recommended

View more >