XML QUERY LANGUAGE Prepared by Prof. Zaniolo, Hung-chih Yang, Ling-Jyh Chen Modified by Fernando Farfán
Motivation Increasing amounts of information stored, exchanged, and presented as XML. Ability to intelligently query XML data sources. XML strength: Flexibility representing many kinds of information from diverse sources. XML query language must retrieve and interpret information from these diverse sources. 2 XML Query Language Tutorial
Desiderata for an XML Query Language Expressive power Semantics Compositionality Schema Program manipulation 3 XML Query Language Tutorial
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 4 XML Query Language Tutorial
What is XQuery 5 XML Query Language Tutorial
What is XQuery (cont.) Designed to meet the requirements identified by the W3C XML Query Working Group “XML Query 1.0 Requirements” “XML Query Use Cases”. Designed to be a small, easily implementable language. Flexible enough to query a broad spectrum of XML sources (both databases and documents). Defines a human-readable syntax for that language. 6 XML Query Language Tutorial
What is XQuery (cont.) Expression: Basic building block. Functional language (at least claimed by the spec.) Strongly-typed language. 7 XML Query Language Tutorial
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. 8 XML Query Language Tutorial
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 9 XML Query Language Tutorial
The Principal Forms of XQuery Expressions Primary Literals, variables, function calls and parentheses (for control precedence). 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. 10 XML Query Language Tutorial
The Principal Forms of XQuery Expressions (Cont.) Arithmetic Arithmetic operators for addition, subtraction, multiplication, division, and modulus. Comparison 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 a Boolean value. 11 XML Query Language Tutorial
The Principal Forms of XQuery Expressions (Cont.) Constructor Constructors can create XML structures within a query. There are constructors for elements, attributes, CDATA sections, processing instructions, and comments. FLWR Expression for iteration and for binding variables to intermediate results. Useful for computing joins between two or more documents and for restructuring data. Pronounced "flower", stands for the keywords FOR, LET, WHERE, and RETURN, the four clauses found in a FLWR expression. 12 XML Query Language Tutorial
The Principal Forms of XQuery Expressions (Cont.) Sorting expressions Provides a way to control the order of items in a sequence. Conditional expressions Based on the keywords IF, THEN, and ELSE. Quantified expressions support existential and universal quantification. The value of a quantified expression is always true or false. 13 XML Query Language Tutorial
The Principal Forms of XQuery Expressions (Cont.) Data types 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. 14 XML Query Language Tutorial
XQuery Example 1 Find all books with a price of $39.95 XQuery: document("bib.xml")/bib/book[price = 39.95] Result: Data on the Web Abiteboul Serge Buneman Peter Suciu Dan Morgan Kaufmann Publishers XML Query Language Tutorial
XQuery Example 2 Find the title of all books published before 1995 XQuery: < 1995]/title Result: TCP/IP Illustrated Advanced Programming in the Unix environment 16 XML Query Language Tutorial
XQuery Example 3 (For Loop) List books published by Addison-Wesley after 1991, including their year and title. XQuery: { for $b in document("bib.xml")/bib/book where $b/publisher = "Addison-Wesley" and > 1991 return { $b/title } } 17 XML Query Language Tutorial
XQuery Example 3 (For Loop) List books published by Addison-Wesley after 1991, including their year and title… Result: TCP/IP Illustrated Advanced Programming in the Unix environment 18 XML Query Language Tutorial
XQuery Example 4 (Join) For each book found at both bn.com and amazon.com, list the title of the book and its price from each source. XQuery: { for $b in document("bib.xml")//book, $a in document("reviews.xml")//entry where $b/title = $a/title return { $b/title } { $a/price } { $b/price } } 19 XML Query Language Tutorial
XQuery Example 4 (Join) For each book found at both bn.com and amazon.com, list the title of the book and its price from each source. Result: TCP/IP Illustrated Advanced Programming in the Unix environment Data on the Web XML Query Language Tutorial
XQuery Example 5 ( Grouping + quantifier ) 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. XQuery: { for $a in distinct-values(document("bib.com")//author) return { $a } { for $b in document(" where some $ba in $b/author satisfies deep-equal($ba,$a) return $b/title } } 21 XML Query Language Tutorial
XQuery Example 5 ( Grouping + quantifier ) 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. Result: Stevens W. TCP/IP Illustrated Advanced Programming in the Unix environment Abiteboul Serge Data on the Web …… 22 XML Query Language Tutorial
XQuery Example 6 (Sorting) List the titles and years of all books published by Addison-Wesley after 1991, in alphabetic order. XQuery: { for $b in document(" where $b/publisher = "Addison-Wesley" and > 1991 return { } { $b/title } sortby (title) } 23 XML Query Language Tutorial
XQuery Example 6 (Sorting) List the titles and years of all books published by Addison-Wesley after 1991, in alphabetic order. Result: Advanced Programming in the Unix environment TCP/IP Illustrated 24 XML Query Language Tutorial
XQuery Example 7 (Recursion) Convert the sample document from "partlist" format to "parttree" format. XQuery: define function one_level (element $p) returns element { { for $s in document("partlist.xml")//part where = return one_level($s) } } { for $p in return one_level($p) } 25 XML Query Language Tutorial
XQuery Example 7 (Recursion) Convert the sample document from "partlist" format to "parttree" format. Result: 26 XML Query Language Tutorial
XQuery Example 8 (Sequence) In the Procedure section of Report1, what Instruments were used in the second Incision? XQuery: for $s in document("report1.xml")//section[section.title = "Procedure"] return ($s//incision)[2]/instrument Result: electrocautery 27 XML Query Language Tutorial
XQuery Support on RDBMSs XML Query Language Tutorial 28 Oracle XQuery Engine Introduction to XQuery in SQL Server 2005 us/library/ms345122(SQL.90).aspx Query DB2 XML data with XQuery dm-0604saracco/ DataDirect: Data Integration Suite – MySQL Database Support integration/datasources/databases/mysql/index.ssp
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 simpler 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 29 XML Query Language Tutorial
References Jonathan Pinnock, et al. “Professional XML, 2nd edition”, ISBN: , WROX Publishers, 2001 Serge Abiteboul, Peter Buneman and Dan Suciu, “Data on the Web: from Relations to Semistructured Data and XML”, ISBN 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, Don Chamberlin, Jonathan Robie, and Daniela Florescu, “Quilt: An XML Query Language for Heterogeneous Data Sources”, WebDB 2000, Dallas, May XML Query Language Tutorial