Title: Advisor: Prof. Zaniolo
1XML Query Language
- Advisor Prof. Zaniolo
- Hung-chih Yang
- Ling-Jyh Chen
2Motivation
- 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.
3Desiderata for an XML query language
- Expressive power
- Semantics
- Compositionality
- Schema
- Program manipulation
4Different 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 its the immediate ancestor
of XQuery
5Whats XQuery
SQL
Relational database
XML document /database
XQuery
6Whats 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
7Whats 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
8XQuery 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
9XQuery 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
10The 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.
11The 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.
12The 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.
13The 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.
14The 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.
15XQuery Example 1
Xquery Find all books with a price of 39.95
- document("bib.xml")/bib/bookprice 39.95
Result
ltbook year"2000"gt lttitlegtData on the
Weblt/titlegt ltauthorgtltlastgtAbiteboullt/lastgtltfir
stgtSergelt/firstgtlt/authorgt ltauthorgtltlastgtBunema
nlt/lastgtltfirstgtPeterlt/firstgtlt/authorgt
ltauthorgtltlastgtSuciult/lastgtltfirstgtDanlt/firstgtlt/auth
orgt ltpublishergtMorgan Kaufmann
Publisherslt/publishergt ltpricegt
39.95lt/pricegt lt/bookgt
16XQuery Example 2
XQuery Find the title of all books published
before 1995
- document("bib.xml")/bib/book_at_year lt 1995/title
Result
lttitlegtTCP/IP Illustratedlt/titlegt lttitlegtAdvanced
Programming in the Unix environmentlt/titlegt
17XQuery Example 3 (for loop)
XQuery List books published by Addison-Wesley
after 1991, including their year and title.
- ltbibgt
-
- for b in document("bib.xml")/bib/book
- where b/publisher "Addison-Wesley" and
b/_at_year gt 1991 - return
- ltbook year" b/_at_year "gt
- b/title
- lt/bookgt
-
- lt/bibgt
18XQuery Example 3 (for loop)
Result
- ltbibgt
- ltbook year"1994"gt
- lttitlegtTCP/IP Illustratedlt/titlegt
- lt/bookgtltbook year"1992"gt
- lttitlegtAdvanced Programming in the Unix
environmentlt/titlegt - lt/bookgt
- lt/bibgt
19XQuery 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.
- ltbooks-with-pricesgt
-
- for b in document("bib.xml")//book,
- a in document("reviews.xml")//entry
- where b/title a/title
- return
- ltbook-with-pricesgt
- b/title
- ltprice-amazongt a/price
lt/price-amazongt - ltprice-bngt b/price lt/price-bngt
- lt/book-with-pricesgt
-
- lt/books-with-pricesgt
20XQuery Example 4 (Join)
Result
- ltbooks-with-pricesgt
- ltbook-with-pricesgt
- lttitlegtTCP/IP Illustratedlt/titlegt
- ltprice-amazongtltpricegt65.95lt/pricegtlt/pr
ice-amazongt - ltprice-bngtltpricegt 65.95lt/pricegtlt/price
-bngt - lt/book-with-pricesgtltbook-with-pricesgt
- lttitlegtAdvanced Programming in the
Unix environmentlt/titlegt - ltprice-amazongtltpricegt65.95lt/pricegtlt/pr
ice-amazongt - ltprice-bngtltpricegt65.95lt/pricegtlt/price-
bngt - lt/book-with-pricesgtltbook-with-pricesgt
- lttitlegtData on the Weblt/titlegt
- ltprice-amazongtltpricegt34.95lt/pricegtlt/pr
ice-amazongt - ltprice-bngtltpricegt 39.95lt/pricegtlt/price
-bngt - lt/book-with-pricesgt
- lt/books-with-pricesgt
21XQuery 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.
- ltresultsgt
-
- for a in distinct-values(document("bib.com")/
/author) - return ltresultgt
- a
-
- for b in document("http//bib
.com")/bib/book - where some ba in b/author
satisfies deep-equal(ba,a) - return b/title
-
- lt/resultgt
-
- lt/resultsgt
22XQuery Example 5 (Grouping quantifier)
- ltresultsgt
- ltresultgt
- ltauthorgt
- ltlastgtStevenslt/lastgt
- ltfirstgtW.lt/firstgt
- lt/authorgt
- lttitlegtTCP/IP Illustratedlt/titlegt
- lttitlegtAdvanced Programming in the Unix
environmentlt/titlegt - lt/resultgt
- ltresultgt
- ltauthorgt
- ltlastgtAbiteboullt/lastgt
- ltfirstgtSergelt/firstgt
- lt/authorgt
- lttitlegtData on the Weblt/titlegt
- lt/resultgt
-
- lt/resultsgt
Result
23XQuery Example 6 (Sorting)
XQuery List the titles and years of all books
published by Addison-Wesley after 1991, in
alphabetic order.
- ltbibgt
-
- for b in document("www.bn.com/bib.xml")//book
- where b/publisher "Addison-Wesley" and
b/_at_year gt 1991 - return
- ltbookgt
- b/_at_year
- b/title
- lt/bookgt
- sortby (title)
-
- lt/bibgt
24XQuery Example 6 (Sorting)
Result
- ltbibgt
- ltbook year"1992"gt
- lttitlegtAdvanced Programming in the Unix
environmentlt/titlegt - lt/bookgt
- ltbook year"1994"gt
- lttitlegtTCP/IP Illustratedlt/titlegt
- lt/bookgt
- lt/bibgt
25XQuery Example 7 (Recursion)
XQuery Convert the sample document from
"partlist" format to "parttree" format.
- define function one_level (element p) returns
element -
- ltpart partid" p/_at_partid " name"
p/_at_name " gt -
- for s in document("partlist.xml")//pa
rt - where s/_at_partof p/_at_partid
- return one_level(s)
-
- lt/partgt
-
- ltparttreegt
-
- for p in document("partlist.xml")//partempty
(_at_partof) - return one_level(p)
-
- lt/parttreegt
26XQuery Example 7 (Recursion)
Result
- ltparttreegt
- ltpart partid"0" name"car"gt
- ltpart partid"1" name"engine"gt
- ltpart partid"3" name"piston"/gt
- lt/partgt
- ltpart partid"2" name"door"gt
- ltpart partid"4" name"window"/gt
- ltpart partid"5" name"lock"/gt
- lt/partgt
- lt/partgt
- ltpart partid"10" name"skateboard"gt
- ltpart partid"11" name"board"/gt
- ltpart partid"12" name"wheel"/gt
- lt/partgt
- ltpart partid"20" name"canoe"/gt
- lt/parttreegt
27XQuery Example 8 (Sequence)
XQuery In the Procedure section of Report1, what
Instruments were used in the second Incision?
- for s in document("report1.xml")//sectionsection
.title "Procedure" - return (s//incision)2/instrument
Result
ltinstrumentgtelectrocauterylt/instrumentgt
28XQuery practice
- XML Query Language Demo,http//131.107.228.20
- X-Hive. Another nice-looking online
demo,http//217.77.130.1898080/demos/xquery/inde
x.html - Qexo The GNU Kawa implementation of XQuery,
http//www.gnu.org/software/qexo/
29Conclusion
- 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
30Reference
- 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