xquery

17
XQuery John Annechino Steven Pow

Upload: kamala

Post on 07-Jan-2016

77 views

Category:

Documents


3 download

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 Presentation

TRANSCRIPT

Page 1: XQuery

XQuery

John AnnechinoSteven Pow

Page 2: XQuery

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

Page 3: XQuery

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.

Page 4: XQuery

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

Page 5: XQuery

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.

Page 6: XQuery

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 :)

Page 7: XQuery

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>

Page 8: XQuery

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

Page 9: XQuery

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

Page 10: XQuery

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>

Page 11: XQuery

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>

Page 12: XQuery

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>

Page 13: XQuery

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

Page 14: XQuery

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>

Page 15: XQuery

XQuery Examples

Page 16: XQuery

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.

Page 17: XQuery

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