Title: XQuery
1XQuery
Source http//www.srdc.metu.edu.tr/webpage/cours
es/ceng520
2What is XQuery?
A language for querying XML A human-readable
syntax for an XML query language A functional
programming language The product of the W3Cs
XML Query Working Group
3History
4Resources
5XQuery 1.0 XPath 2.0
XQueryX 1.0
XQuery 1.0
XPath 2.0
XQuery 1.0 Xpath 2.0Formal Semantics
XQuery 1.0 Xpath 2.0Functions Operators
XQuery 1.0 Xpath 2.0Data Model
XQuery 1.0 Xpath 2.0Requirements Use Cases
XML Schema 1.0
XML 1.0
Namespaces 1.0
6Does the world need a new query language?
7Nesting
8Metadata
9Ordering
10Missing Information
11General XQuery Rules
12XQuery Expressions
13Functions
14Path Expressions
15Predicates
16Expressions
17Logical Expressions
18Constructors
19FLWOR Expressions
20An Example Query
21Ways of qualifying a node ("Kind tests")
22Testing Types
23Structure of an XQuery
24Namespace Declarations
25Schema Imports
26Function Definitions
27Modules
28Modules (continued)
29Two Phases in Query Processing
30Summary and Prognosis
31Some Examples(1)
FOR - LET - WHERE - RETURN Similar to SQLs
SELECT - FROM - WHERE for book in
document("http//www.bn.com/bib.xml")//book
where book/publisher "Addison-Wesley" return
ltbookgt book/title,
book/author lt/bookgt
32Some Examples(2)
_at_ indicates, attribute for bk in
document("http//www.bn.com/bib.xml")/bib/book
where bk/_at_year lt "1997 return bk/title
33Some Examples(3)
Nested fors can be used, for a in
document("http//www.bn.com/bib.xml") for b
in a/bib for c in b/book
return ltauthorlistgt c/author lt/authorlistgt
34Some Examples(4)
More than one bindings b, t, a ltresultsgt
for b in document("http//www.bn.com/bib.xml")/bi
b/book, t in b/title, a in b/author
return ltresultgt t a
lt/resultgt lt/resultsgt
35Some Examples(5)
Join operation between two XML files ltbooks-with-
pricesgt for b in document("http//www.bn.com
/bib.xml")//book, a in document("http//www
.amazon.com/reviews.xml")//entry where
b/title a/title return
ltbook-with-pricesgt b/title
ltprice-amazongt a/price/text() lt/price-amazongt
ltprice-bngt b/price/text() lt/price-bngt
lt/book-with-pricesgt lt/books-with-pricesgt
36Some Examples(6)
SORTBY can be used, ltbibliographygt FOR
book IN document("bib.xml")//book RETURN
ltbookgt book/author,
book/title lt/bookgt SORTBY
(author, title) lt/bibliographygt
37Some Examples(7)
A function example FUNCTION depth(ELEMENT e)
RETURNS integer -- An empty element has
depth 1 -- Otherwise, add 1 to max depth of
children IF empty(e/) THEN 1
ELSE max(depth(e/)) 1 depth(document("partl
ist.xml"))
38Some Examples(8)
min(), max(), count(), avg(), sum() ltresultsgt L
ET doc document("prices.xml")/prices/book RETU
RN ltcount-of-pricegt count(doc/price)
lt/count-of-pricegt ltavgpricegt
avg(doc/price) lt/avgpricegt ltmaxpricegt
max(doc/price) lt/maxpricegt ltminpricegt
min(doc/price) lt/minpricegt ltsumpricegt
sum(doc/price) lt/sumpricegt lt/resultsgt
39Some Examples(9)
Another example FOR item IN document("news_item
.xml")//news_item WHERE contains(
string(item/content), "Gorilla Corporation"
) RETURN ltitem_summarygt
item/title/text() ".
item/date/text() ". string(
(item//par)1 ) lt/item_summary
40Some Examples(10)
Insert, Replace, Delete FOR e IN /empINSERT
ltn_skillsgt count(e/skill) lt/n_skillsgt BEFORE
e/skill1 FOR e IN /empWHERE e/empno
"1234REPLACE e/job WITH ltjobgt "Broom Tester"
lt/jobgt FOR e IN /emp/job "Programmer",
s IN e/skillWHERE s/rating lt 4 OR
s/cert_date lt date("1995-01-01")DELETE s
41Thank you for your attention