xquery
DESCRIPTION
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?. - PowerPoint PPT PresentationTRANSCRIPT
XQuery
John AnnechinoSteven 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<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>
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
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
<title>Learning XML</title>
for $x in doc(‘books.xml’)/store/bookwhere $x/price>30return $x/title
<title>Learning XML</title>
FLWOR
<ul>{for $x in doc(“books.xml”)/bookstore/bookorder by $x/titlereturn <li class="{data($x/@category)}">{data($x/title)}</li>}</ul>
<ul><li class=“CHILDREN”>Harry Potter</li><li class=“WEB”>Learning XML</li></ul>
if-then-else
for $x in doc("books.xml")/bookstore/bookreturn if ($x/@category="CHILDREN")
then <child>{data($x/title)}</child>else <adult>{data($x/title)}</adult>
<adult>Learning XML</adult><child>Harry Potter</child>
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 Functionsdeclare function local:minPrice(
$price as xs:decimal, $discount as xs:decimal) AS xs:decimal
{let $disc := ($price * $discount) div 100return ($price - $disc)
};
<minPrice>{local:minPrice($book/price, $book/discount)}</minPrice>
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
• 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