300 likes | 503 Views
XML Query Language. Advisor: Prof. Zaniolo Hung-chih Yang Ling-Jyh Chen. Motivation. As increasing amounts of information are stored, exchanged, and presented using XML, the ability to intelligently query XML data sources becomes increasingly important.
E N D
XML Query Language Advisor: Prof. Zaniolo Hung-chih Yang Ling-Jyh Chen
Motivation • As increasing amounts of information are stored, exchanged, and presented using XML, the ability to intelligently query XML data sources becomes increasingly important. • One of the great strengths of XML is its flexibility in representing many different kinds of information from diverse sources. • To exploit this flexibility, an XML query language must provide features for retrieving and interpreting information from these diverse sources.
Desiderata for an XML query language • Expressive power • Semantics • Compositionality • Schema • Program manipulation
Different query languages for XML • XPath & XQL: path expression syntax suitable for hierarchical documents • XML-QL: binding variables and using bound variables to create new structures • SQL: SELECT-FROM-WHERE pattern for restructuring data • OQL: ODMG • Quilt: accept a lot of advantages from above XML query languages, and it’s the immediate ancestor of XQuery
What’s XQuery SQL Relational database XML document /database XQuery
What’s XQuery (cont.) • XQuery is designed to meet the requirements identified by the W3C XML Query Working Group “XML Query 1.0 Requirements” and the use cases in “XML Query Use Cases”. • XQuery is designed to be a small, easily implementable language. • XQuery is flexible enough to query a broad spectrum of XML information sources, including both databases and documents. • XQuery defines a human-readable syntax for that language
What’s Xquery (cont.) • The basic building block of XQuery is the expression • XQuery is a functional language (at least the spec claimed it to be) • XQuery is a strongly-typed language
XQuery vs XSLT Reinventing the Wheel? • XSLT is document-driven; XQuery is program driven • XSLT is written in XML; XQuery is not • An assertion (unproven): XSLT 2.0 can do everything XQuery can do
XQuery concepts A query in XQuery is an expression that: • Reads a number of XML documents or fragments • Returns a sequence of well-formed XML fragments
The principal forms of XQuery expressions • Primary • The basic primitives of the language including literals, variables, function calls and the use of parentheses to control precedence of operation • Path • Locates nodes within a tree, and returns a sequence of distinct nodes in document order • Sequence • An ordered collection of zero or more items, where an item may be an atomic value or a node. An item is identical to a sequence of length one containing that item. Sequences are never nested.
The principal forms of XQuery expressions (cont.) • Arithmetic • Xquery provides arithmetic operators for addition, subtraction, multiplication, division, and modulus. • Comparison • Xquery provides four kinds of comparisons: value, general, node, and order comparisons. • Logical • A logical expression is either an and-expression or an or-expression. The value of a logical expression is always one of the boolean values: true or false.
The principal forms of XQuery expressions (cont.) • Contructor • Constructors can create XML structures within a query. There are constructors for elements, attributes, CDATA sections, processing instructions, and comments. • FLWR • XQuery provides a FLWR expression for iteration and for binding variables to intermediate results. This kind of expression is often useful for computing joins between two or more documents and for restructuring data. The name "FLWR", pronounced "flower", stands for the keywords for, let, where, and return, the four clauses found in a FLWR expression.
The principal forms of XQuery expressions (cont.) • Sorting • A sorting expression provides a way to control the order of items in a sequence. • Conditional • XQuery supports a conditional expression based on the keywords if, then, and else. • Quantified • Quantified expressions support existential and universal quantification. The value of a quantified expression is always true or false.
The principal forms of XQuery expressions (cont.) • Datatypes • Runtime type checking and manipulation • Validate • A validate expression validates its argument with respect to the in-scope schema definitions, using the schema validation process described in XML Schema.
XQuery Example 1 Xquery:Find all books with a price of $39.95 document("bib.xml")/bib/book[price = 39.95] Result: <book year="2000"> <title>Data on the Web</title> <author><last>Abiteboul</last><first>Serge</first></author> <author><last>Buneman</last><first>Peter</first></author> <author><last>Suciu</last><first>Dan</first></author> <publisher>Morgan Kaufmann Publishers</publisher> <price> 39.95</price> </book>
XQuery Example 2 XQuery:Find the title of all books published before 1995 document("bib.xml")/bib/book[@year < 1995]/title Result: <title>TCP/IP Illustrated</title> <title>Advanced Programming in the Unix environment</title>
XQuery Example 3 (for loop) XQuery:List books published by Addison-Wesley after 1991, including their year and title. <bib> { for $b in document("bib.xml")/bib/book where $b/publisher = "Addison-Wesley" and $b/@year > 1991 return <book year="{ $b/@year }"> { $b/title } </book> } </bib>
XQuery Example 3 (for loop) Result: <bib> <book year="1994"> <title>TCP/IP Illustrated</title> </book><book year="1992"> <title>Advanced Programming in the Unix environment</title> </book> </bib>
XQuery Example 4 (Join) XQuery:For each book found at both bn.com and amazon.com, list the title of the book and its price from each source. <books-with-prices> { for $b in document("bib.xml")//book, $a in document("reviews.xml")//entry where $b/title = $a/title return <book-with-prices> { $b/title } <price-amazon>{ $a/price }</price-amazon> <price-bn>{ $b/price }</price-bn> </book-with-prices> } </books-with-prices>
XQuery Example 4 (Join) Result: <books-with-prices> <book-with-prices> <title>TCP/IP Illustrated</title> <price-amazon><price>65.95</price></price-amazon> <price-bn><price> 65.95</price></price-bn> </book-with-prices><book-with-prices> <title>Advanced Programming in the Unix environment</title> <price-amazon><price>65.95</price></price-amazon> <price-bn><price>65.95</price></price-bn> </book-with-prices><book-with-prices> <title>Data on the Web</title> <price-amazon><price>34.95</price></price-amazon> <price-bn><price> 39.95</price></price-bn> </book-with-prices> </books-with-prices>
XQuery Example 5 (Grouping + quantifier) XQuery:For each author in the bibliography, list the author's name and the titles of all books by that author, grouped inside a "result" element. <results> { for $a in distinct-values(document("bib.com")//author) return <result> { $a } { for $b in document("http://bib.com")/bib/book where some $ba in $b/author satisfies deep-equal($ba,$a) return $b/title } </result> } </results>
XQuery Example 5 (Grouping + quantifier) Result: <results> <result> <author> <last>Stevens</last> <first>W.</first> </author> <title>TCP/IP Illustrated</title> <title>Advanced Programming in the Unix environment</title> </result> <result> <author> <last>Abiteboul</last> <first>Serge</first> </author> <title>Data on the Web</title> </result> …… </results>
XQuery Example 6 (Sorting) XQuery:List the titles and years of all books published by Addison-Wesley after 1991, in alphabetic order. <bib> { for $b in document("www.bn.com/bib.xml")//book where $b/publisher = "Addison-Wesley" and $b/@year > 1991 return <book> { $b/@year } { $b/title } </book> sortby (title) } </bib>
XQuery Example 6 (Sorting) Result: <bib> <book year="1992"> <title>Advanced Programming in the Unix environment</title> </book> <book year="1994"> <title>TCP/IP Illustrated</title> </book> </bib>
XQuery Example 7 (Recursion) XQuery:Convert the sample document from "partlist" format to "parttree" format. define function one_level (element $p) returns element { <part partid="{ $p/@partid }" name="{ $p/@name }" > { for $s in document("partlist.xml")//part where $s/@partof = $p/@partid return one_level($s) } </part> } <parttree> { for $p in document("partlist.xml")//part[empty(@partof)] return one_level($p) } </parttree>
XQuery Example 7 (Recursion) Result: <parttree> <part partid="0" name="car"> <part partid="1" name="engine"> <part partid="3" name="piston"/> </part> <part partid="2" name="door"> <part partid="4" name="window"/> <part partid="5" name="lock"/> </part> </part> <part partid="10" name="skateboard"> <part partid="11" name="board"/> <part partid="12" name="wheel"/> </part> <part partid="20" name="canoe"/> </parttree>
XQuery Example 8 (Sequence) XQuery:In the Procedure section of Report1, what Instruments were used in the second Incision? for $s in document("report1.xml")//section[section.title = "Procedure"] return ($s//incision)[2]/instrument Result: <instrument>electrocautery</instrument>
XQuery practice • XML Query Language Demo,http://131.107.228.20 • X-Hive. Another nice-looking online demo,http://217.77.130.189:8080/demos/xquery/index.html • Qexo: The GNU Kawa implementation of XQuery, http://www.gnu.org/software/qexo/
Conclusion • XQuery is a simple substitution of XSLT, JSP, ASP, Servlet, CGI, PHP, etc. • XQuery programs can accomplish most tasks of other tools aforementioned, and yet is much simplier to learn and easier to write. • Possible direction is to extend XQuery for UPDATE and INSERT to an XML database • Still lack of support from industry till now
Reference • Jonathan Pinnock, et al. “Professional XML, 2nd edition”, ISBN: 1861005059, WROX Publishers, 2001 • Serge Abiteboul, Peter Buneman and Dan Suciu, “Data on the Web: from Relations to Semistructured Data and XML”, ISBN 1-55860-622-X, Morgan Kaufmann Publishers, 2000 • World Wide Web Consortium, “XQuery 1.0. An XML Query Language”, W3C Working Draft, Apr. 30, 2002 • World Wide Web Consortium, “XML Path Language (XPath) Version 1.0”, W3C Recommendation, Nov. 16, 1999 • Qexo: The GNU Kawa implementation of XQuery, http://www.gnu.org/software/qexo/ • XQuery Tutorialhttp://www.research.avayalabs.com/user/wadler/papers/xquery-tutorial/xquery-tutorial.pdf • Don Chamberlin, Jonathan Robie, and Daniela Florescu, “Quilt: An XML Query Language for Heterogeneous Data Sources”, WebDB 2000, Dallas, May 2000