1 / 17

XQuery

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 Functions Examples Future of XQuery References. What is XQuery?.

kamala
Download Presentation

XQuery

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. XQuery John Annechino Steven Pow

  2. Agenda • What is XQuery? • Uses of XQuery • XQuery vs. XSLT • Syntax • Built-In Functions • FLWOR • if-then-else • User-Defined Functions • Examples • Future of XQuery • References

  3. What is XQuery? • The best way to explain XQuery is to say that XQuery is to XML what SQL is to database tables. • It is the language for querying XML data. • XQuery is a language for finding and extracting elements and attributes from XML documents. • XQuery is designed to query XML data – not just XML files, but anything that can appear as XML.

  4. Uses of XQuery • Extract information to use in a Web Service • Query XML documents • Read data from databases and generate reports • Transform XML data • Search Web documents for relevant information

  5. XQuery vs. XSLT • XSLT has a “processing engine” that automatically goes through the document tree and applies templates as it finds nodes • With XQuery, the the programmer is responsible for directing the process.

  6. Syntax • Elements, attributes, and variables must be valid XML names • XQuery is built up with XPath expressions • XML Schema datatypes are used • XQuery variable is defined with a $ followed by a name • Comments are delimited by (: and :) (: this is a comment :)

  7. books.xml <bookstore> <book category=“WEB”> <title>Learning XML</title> <author>Erik T. Ray</author> <year>2003</year> <price>39.95</price> </book> <book category=“CHILDREN”> <title>Harry Potter</title> <author>J K. Rowling</author> <year>2005</year> <price>29.99</price> </book> </bookstore>

  8. Built-In Functions doc(‘books.xml’)/bookstore/book[price>30] <book category=“WEB”> <title>Learning XML</title> <author>Erik T. Ray</author> <year>2003</year> <price>39.95</price> </book> XQuery includes over 100 built-in functions

  9. FLWOR • For – binds a variable to each item returned by the in expression • Let – allows variable assignments • Where – used to specify criteria for result • Order by – defines the sort-order • Return – specifies what is to be returned

  10. FLWOR doc(‘books.xml’)/bookstore/book[price>30]/title <title>Learning XML</title> for $x in doc(‘books.xml’)/store/book where $x/price>30 return $x/title <title>Learning XML</title>

  11. FLWOR <ul> { for $x in doc(“books.xml”)/bookstore/book order by $x/title return <li class="{data($x/@category)}">{data($x/title)}</li> } </ul> <ul> <li class=“CHILDREN”>Harry Potter</li> <li class=“WEB”>Learning XML</li> </ul>

  12. if-then-else for $x in doc("books.xml")/bookstore/book return if ($x/@category="CHILDREN") then <child>{data($x/title)}</child> else <adult>{data($x/title)}</adult> <adult>Learning XML</adult> <child>Harry Potter</child>

  13. User-Defined Functions declare function prefix:function_name($parameter AS datatype) AS returnDatatype { (: ...function code here... :) }; XQuery shares the same datatypes as XML Schema, including Date, String, Numeric, and other Misc types

  14. 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) }; <minPrice> { local:minPrice($book/price, $book/discount) } </minPrice>

  15. XQuery Examples

  16. Future of XQuery • XQuery is currently a Working Draft • XQuery is compatible with several W3C standards, such as XML, Namespaces, XSLT, XPath, and XML Schema • XQuery 1.0 is not yet a W3C Recommendation. Hopefully it will be a recommendation in the near future.

  17. References • http://www.w3c.org/XML/Query.html • http://www.w3schools.com/xquery/default.asp • http://www.w3schools.com/xpath/xpath_functions.asp • http://www.xmlmind.com/qizxopen.html • XQuery: The XML Query Language, by Michael Brundage

More Related