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? 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.
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
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.
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 :)
books.xml Learning XML Erik T. Ray Harry Potter J K. Rowling
Built-In Functions doc(‘books.xml’)/bookstore/book[price>30] Learning XML Erik T. Ray XQuery includes over 100 built-in functions
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
FLWOR doc(‘books.xml’)/bookstore/book[price>30]/title Learning XML for $x in doc(‘books.xml’)/store/book where $x/price>30 return $x/title Learning XML
FLWOR { for $x in doc(“books.xml”)/bookstore/book order by $x/title return {data($x/title)} } Harry Potter Learning XML
if-then-else for $x in doc("books.xml")/bookstore/book return if then {data($x/title)} else {data($x/title)} Learning XML Harry Potter
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
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) }; { local:minPrice($book/price, $book/discount) }
XQuery Examples
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.
References XQuery: The XML Query Language, by Michael Brundage