xquery. summary of xquery flwr expressions for and let expressions collections and sorting resource...

49
Xquery

Upload: joy-logan

Post on 26-Dec-2015

223 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Xquery. Summary of XQuery FLWR expressions FOR and LET expressions Collections and sorting Resource W3C recommendation:

Xquery

Page 2: Xquery. Summary of XQuery FLWR expressions FOR and LET expressions Collections and sorting Resource W3C recommendation:

Summary of XQuery FLWR expressions FOR and LET expressions Collections and sorting

ResourceW3C recommendation: www.w3.org/TR/xquery/

Page 3: Xquery. Summary of XQuery FLWR expressions FOR and LET expressions Collections and sorting Resource W3C recommendation:

Symbols. Denotes the current node.

.. Denotes the parent of the current node.

/Denotes the root node, or a separator between steps in a path.

// Denotes descendants of the current node.

@ Denotes attributes of the current node.

* Denotes "any" (node with unrestricted name).

[ ]Brackets enclose a Boolean expression that serves as a predicate for a given step.

[n]

When a predicate consists of an integer, it serves to select the element with the given ordinal number from a list of elements.

Page 4: Xquery. Summary of XQuery FLWR expressions FOR and LET expressions Collections and sorting Resource W3C recommendation:

FLWR (“Flower”) Expressions

FOR ... LET... FOR... LET...

WHERE...

RETURN...

Page 5: Xquery. Summary of XQuery FLWR expressions FOR and LET expressions Collections and sorting Resource W3C recommendation:

FOR v.s. LET

FOR Binds node variables iteration

LET Binds collection variables one value

Page 6: Xquery. Summary of XQuery FLWR expressions FOR and LET expressions Collections and sorting Resource W3C recommendation:

WHERE - continued

AND, OR, and NOT usually contain references to bound variables

Variables bound in FOR clause usually contain scalar predicates

$p/color = "Red" Variables bound in LET clause usually

used in list predicates avg($p/price) > 100

Page 7: Xquery. Summary of XQuery FLWR expressions FOR and LET expressions Collections and sorting Resource W3C recommendation:

Operators

Allows expressions to be constructed using prefix and infix operators

Standard arithmetic and logical operators “=“ “!=“ “<“ “>” “+” “-” “*” Many built-in functions

Page 8: Xquery. Summary of XQuery FLWR expressions FOR and LET expressions Collections and sorting Resource W3C recommendation:

Operators in Expressions

XQuery allows expressions to be constructed using prefix and infix operators (BEFORE, AFTER

XQuery contains usual logical and arithmetic operators

Also operators like UNION, INTERSECT, and EXCEPT

Page 9: Xquery. Summary of XQuery FLWR expressions FOR and LET expressions Collections and sorting Resource W3C recommendation:

Quantifiers

Tests for existence of some elements that satisfy a condition

Also used to test whether all elements in a collection satisfy a condition

Key words satisfies and contains

Page 10: Xquery. Summary of XQuery FLWR expressions FOR and LET expressions Collections and sorting Resource W3C recommendation:

XQuery

Find all book titles published after 1995:

FOR $x IN document("bib.xml")/bib/book

WHERE $x/year > 1995

RETURN $x/title

FOR $x IN document("bib.xml")/bib/book

WHERE $x/year > 1995

RETURN $x/title

Result: <title> abc </title> <title> def </title> <title> ghi </title>

Page 11: Xquery. Summary of XQuery FLWR expressions FOR and LET expressions Collections and sorting Resource W3C recommendation:

XQuery

For each author of a book by Morgan Kaufmann, list all books she published:

FOR $a IN distinct(document("bib.xml") /bib/book[publisher=“Morgan Kaufmann”]/author)

RETURN <result>

$a,

FOR $t IN /bib/book[author=$a]/title

RETURN $t

</result>

FOR $a IN distinct(document("bib.xml") /bib/book[publisher=“Morgan Kaufmann”]/author)

RETURN <result>

$a,

FOR $t IN /bib/book[author=$a]/title

RETURN $t

</result>

distinct = a function that eliminates duplicates

Page 12: Xquery. Summary of XQuery FLWR expressions FOR and LET expressions Collections and sorting Resource W3C recommendation:

XQuery

Result: <result> <author>Jones</author> <title> abc </title> <title> def </title> </result> <result> <author> Smith </author> <title> ghi </title> </result>

Page 13: Xquery. Summary of XQuery FLWR expressions FOR and LET expressions Collections and sorting Resource W3C recommendation:

XQuery

FOR $x in expr -- binds $x to each element in the list expr

LET $x = expr -- binds $x to the entire list expr Useful for common subexpressions and for

aggregations

Page 14: Xquery. Summary of XQuery FLWR expressions FOR and LET expressions Collections and sorting Resource W3C recommendation:

XQuery

count = a (aggregate) function that returns the number of elms

<big_publishers>

FOR $p IN distinct(document("bib.xml")//publisher)

LET $b := document("bib.xml")/book[publisher = $p]

WHERE count($b) > 100

RETURN $p

</big_publishers>

<big_publishers>

FOR $p IN distinct(document("bib.xml")//publisher)

LET $b := document("bib.xml")/book[publisher = $p]

WHERE count($b) > 100

RETURN $p

</big_publishers>

Page 15: Xquery. Summary of XQuery FLWR expressions FOR and LET expressions Collections and sorting Resource W3C recommendation:

XQuery

Find books whose price is larger than average:

LET $a=avg(document("bib.xml")/bib/book/@price)

FOR $b in document("bib.xml")/bib/book

WHERE $b/@price > $a

RETURN $b

LET $a=avg(document("bib.xml")/bib/book/@price)

FOR $b in document("bib.xml")/bib/book

WHERE $b/@price > $a

RETURN $b

Page 16: Xquery. Summary of XQuery FLWR expressions FOR and LET expressions Collections and sorting Resource W3C recommendation:

FOR v.s. LET

FOR $x IN document("bib.xml")/bib/book

RETURN <result> $x </result>

FOR $x IN document("bib.xml")/bib/book

RETURN <result> $x </result>

Returns: <result> <book>...</book></result> <result> <book>...</book></result> <result> <book>...</book></result> ...

LET $x := document("bib.xml")/bib/book

RETURN <result> $x </result>

LET $x := document("bib.xml")/bib/book

RETURN <result> $x </result>

Returns: <result> <book>...</book> <book>...</book> <book>...</book> ...</result>

Page 17: Xquery. Summary of XQuery FLWR expressions FOR and LET expressions Collections and sorting Resource W3C recommendation:

Collections in XQuery

Ordered and unordered collections /bib/book/author = an ordered collection Distinct(/bib/book/author) = an unordered collection

LET $a = /bib/book $a is a collection $b/author a collection (several authors...)

RETURN <result> $b/author </result>RETURN <result> $b/author </result>Returns: <result> <author>...</author> <author>...</author> <author>...</author> ...</result>

Page 18: Xquery. Summary of XQuery FLWR expressions FOR and LET expressions Collections and sorting Resource W3C recommendation:

Collections in XQueryWhat about collections in expressions ?

$b/@price list of n prices

$b/@price * 0.7 list of n numbers

$b/@price * $b/@quantity list of n x m numbers ??

$b/@price * ($b/@quant1 + $b/@quant2) $b/@price * $b/@quant1 + $b/@price * $b/@quant2 !!

Page 19: Xquery. Summary of XQuery FLWR expressions FOR and LET expressions Collections and sorting Resource W3C recommendation:

Sorting in XQuery

<publisher_list> FOR $p IN distinct(document("bib.xml")//publisher) RETURN <publisher> <name> $p/text() </name> , FOR $b IN document("bib.xml")//book[publisher = $p] RETURN <book> $b/title , $b/@price </book> SORTBY(price DESCENDING) </publisher> SORTBY(name) </publisher_list>

<publisher_list> FOR $p IN distinct(document("bib.xml")//publisher) RETURN <publisher> <name> $p/text() </name> , FOR $b IN document("bib.xml")//book[publisher = $p] RETURN <book> $b/title , $b/@price </book> SORTBY(price DESCENDING) </publisher> SORTBY(name) </publisher_list>

Page 20: Xquery. Summary of XQuery FLWR expressions FOR and LET expressions Collections and sorting Resource W3C recommendation:

If-Then-Else

FOR $h IN //holding

RETURN <holding>

$h/title,

IF $h/@type = "Journal"

THEN $h/editor

ELSE $h/author

</holding> SORTBY (title)

FOR $h IN //holding

RETURN <holding>

$h/title,

IF $h/@type = "Journal"

THEN $h/editor

ELSE $h/author

</holding> SORTBY (title)

Page 21: Xquery. Summary of XQuery FLWR expressions FOR and LET expressions Collections and sorting Resource W3C recommendation:

Existential Quantifiers

FOR $b IN //book

WHERE SOME $p IN $b//para SATISFIES

contains($p, "sailing")

AND contains($p, "windsurfing")

RETURN $b/title

FOR $b IN //book

WHERE SOME $p IN $b//para SATISFIES

contains($p, "sailing")

AND contains($p, "windsurfing")

RETURN $b/title

Page 22: Xquery. Summary of XQuery FLWR expressions FOR and LET expressions Collections and sorting Resource W3C recommendation:

Universal Quantifiers

FOR $b IN //book

WHERE EVERY $p IN $b//para SATISFIES

contains($p, "sailing")

RETURN $b/title

FOR $b IN //book

WHERE EVERY $p IN $b//para SATISFIES

contains($p, "sailing")

RETURN $b/title

Page 23: Xquery. Summary of XQuery FLWR expressions FOR and LET expressions Collections and sorting Resource W3C recommendation:

Group-By in Xquery ??

FOR $b IN document("http://www.bn.com")/bib/book,

$y IN $b/@year

WHERE $b/publisher="Morgan Kaufmann"

RETURN GROUPBY $y

WHERE count($b) > 10

IN <year> $y </year>

FOR $b IN document("http://www.bn.com")/bib/book,

$y IN $b/@year

WHERE $b/publisher="Morgan Kaufmann"

RETURN GROUPBY $y

WHERE count($b) > 10

IN <year> $y </year>

SELECT year

FROM Bib

WHERE Bib.publisher="Morgan Kaufmann"

GROUPBY year

HAVING count(*) > 10

SELECT year

FROM Bib

WHERE Bib.publisher="Morgan Kaufmann"

GROUPBY year

HAVING count(*) > 10

with GROUPBY

Equivalent SQL

Page 24: Xquery. Summary of XQuery FLWR expressions FOR and LET expressions Collections and sorting Resource W3C recommendation:

Query

Example: Return a flat list of supplier names and their part descriptions for the parts that are actually supplied, in alphabetic order.

Page 25: Xquery. Summary of XQuery FLWR expressions FOR and LET expressions Collections and sorting Resource W3C recommendation:

JOINS in Relation1 ABC 100

2 DEF 75

3 GHI 36

4 JKL 2

5 MNO

0

P (part)pno descrip qnty

27 IBM NY

35 MSFT

WSH

8 LSN JAX

14 AMD CA

51 AJR BNA

24 UF GNV

S (supplier) sno name locat

2 24 5.00

3 35 6.50

2 14 4.00

4 24 10.00

1 27 2.25

SP (Supplies) pno sno price

Page 26: Xquery. Summary of XQuery FLWR expressions FOR and LET expressions Collections and sorting Resource W3C recommendation:

XML documentsP.XML

<parts> <p_tuple> <p_no>

1 </p_no> <descrip>

ABC </descrip> <qty>

100 </qty> </p_tuple></parts>

S.XML<supplier> <s_tuple> <s_no>

27 </s_no> <name>

IBM </name> <locat>

NY </locat> </s_tuple></supplier>

SP.XML<supplies_part> <sp_tuple> <p_no>

2 </p_no> <s_no>

24 </s_no> <price>

5.00 </price> </p_tuple></supplies_part>

Page 27: Xquery. Summary of XQuery FLWR expressions FOR and LET expressions Collections and sorting Resource W3C recommendation:

JOINS in XQuery

For $sp in document(“sp.xml”)//sp_tuple,

$p in document(“p.xml”)//p_tuple[

pno = $sp/pno]

$s in document(“s.xml”)//s_tuple

[sno = $sp/sno]

Return <sp_pair> {

$s/name, $p/descrip }

</sp_pair> sortby(sname, descrip)

Page 28: Xquery. Summary of XQuery FLWR expressions FOR and LET expressions Collections and sorting Resource W3C recommendation:

Binding of Joins in Xquery$sp

sp_tuple sp_tuple sp_tuple sp_tuple sp_tuple

2 24 5.00 3 35 6.50 2 14 4.00 4 24 1.00 1 27 2.25 $p

p_tuple p_tuple p_tuple p_tuple

1 ABC 100 2 DEF 75 3 GHI 36 4 JKL 2

$s

s_tuple s_tuple s_tuple s_tuple

27 IBM NY 35 MSFT WSH 14 AMD CA 24 UF GNV

Page 29: Xquery. Summary of XQuery FLWR expressions FOR and LET expressions Collections and sorting Resource W3C recommendation:

Additional Issues

Updates : not implemented Case sensitivity: all keywords are case sensitive Type checking: When being processed a query

goes through two phases: Analysis Phase & Evaluation Phase. During the Analysis phase type checking occurs to

provide early detection of type errors and to compute the type of the result.

Querying XML documents with cycles

Page 30: Xquery. Summary of XQuery FLWR expressions FOR and LET expressions Collections and sorting Resource W3C recommendation:

<bib> <book year="1994"> <title>TCP/IP Illustrated</title>

<author><last>Stevens</last><first>W.</first></author> <publisher>Addison-Wesley</publisher>

<price> 65.95</price> </book> <book year="1992"> <title>Advanced Programming in

the Unix environment</title> <author><last>Stevens</last><first>W.</first></author> <publisher>Addison-Wesley</publisher>

<price>65.95</price> </book> <book year="1999"> <title>The Economics of Technology and Content for

Digital TV</title> <editor> <last>Gerbarg</last><first>Darcy</first> <affiliation>CITI</affiliation> </editor>

<publisher>Kluwer Academic Publishers</publisher> <price>129.95</price>

</book> </bib>

Page 31: Xquery. Summary of XQuery FLWR expressions FOR and LET expressions Collections and sorting Resource W3C recommendation:

List books published by Addison-Wesley after 1991, including their year and title.

<bib> { for $b in

document("http://www.bn.com")/bib/book where $b/publisher = "Addison-Wesley" and $b/@year > 1991

return <book year="{ $b/@year }"> { $b/title } </book> } </bib>

Page 32: Xquery. Summary of XQuery FLWR expressions FOR and LET expressions Collections and sorting Resource W3C recommendation:

Expected Result

<bib> <book year="1994"> <title>TCP/IP Illustrated</title> </book> <book year="1992"> <title>Advanced Programming in the

Unix environment</title> </book> </bib>

Page 33: Xquery. Summary of XQuery FLWR expressions FOR and LET expressions Collections and sorting Resource W3C recommendation:

Create a flat list of all the title-author pairs, with each pair enclosed in a "result" element.

<results> { for $b in

document("http://www.bn.com")/bib/book, $t in $b/title,

$a in $b/author return <result> { $t } { $a } </result> } </results>

Page 34: Xquery. Summary of XQuery FLWR expressions FOR and LET expressions Collections and sorting Resource W3C recommendation:

Expected Results

<results> <result> <title>TCP/IP

Illustrated</title> <author> <last>Stevens</last> <first>W.</first> </author>

Page 35: Xquery. Summary of XQuery FLWR expressions FOR and LET expressions Collections and sorting Resource W3C recommendation:

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("www.bn.com/bib.xml")//book, $a in

document("www.amazon.com/reviews.xml")//entry

where $b/title = $a/title return <book-with-prices> { $b/title } <price-amazon>{ $a/price/text() }</price-amazon>

<price-bn>{ $b/price/text() }</price-bn> </book-with-prices> } </books-with-prices>

Page 36: Xquery. Summary of XQuery FLWR expressions FOR and LET expressions Collections and sorting Resource W3C recommendation:

For each book that has at least one author, list the title and first two authors, and an empty "et-al" element if the book has additional authors.

<bib> { for $b in

document("www.bn.com/bib.xml")//book where count($b/author) > 0

return <book> { $b/title } { for $a in $b/author[position()<=2] return $a } { if (count($b/author) > 2) then <et-al/> else () } </book> } </bib>

Page 37: Xquery. Summary of XQuery FLWR expressions FOR and LET expressions Collections and sorting Resource W3C recommendation:

<book> <title>Data on the Web</title> <author> <last>Abiteboul</last>

<first>Serge</first> </author> <author> <last>Buneman</last>

<first>Peter</first> </author> <et-al/> </book>

Page 38: Xquery. Summary of XQuery FLWR expressions FOR and LET expressions Collections and sorting Resource W3C recommendation:

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> sort by (title) } </bib>

Page 39: Xquery. Summary of XQuery FLWR expressions FOR and LET expressions Collections and sorting Resource W3C recommendation:

Path Expression(Q) In the second chapter of the document named "zoo.xml",

find the figure(s) with caption "Tree Frogs".

document("zoo.xml")//chapter[2]//figure[caption = "Tree Frogs"]

First step locates the root node of a document. Second step locates the second chapter of the

document (ordinal number)

Third step finds figure elements occurring anywhere within the chapter, but retains those figure elements that have a caption with the value "Tree Frogs."

Page 40: Xquery. Summary of XQuery FLWR expressions FOR and LET expressions Collections and sorting Resource W3C recommendation:

Element Constructors To generate a new element is to embed the

element directly in a query using XML notation.

(Q) Generate an <emp> element that has an "empid" attribute and nested <name> and <job> elements.

<emp empid = "12345"> <name>John Smith</name><job>Anthropologist</job>

</emp>

Page 41: Xquery. Summary of XQuery FLWR expressions FOR and LET expressions Collections and sorting Resource W3C recommendation:

Element Constructors

(Q) Generate an <emp> element that has an "empid" attribute. The value of the attribute and the content of the element are specified by variables that are bound in other parts of the query.

<emp empid = {$id}> {$name} {$job}

</emp>

Page 42: Xquery. Summary of XQuery FLWR expressions FOR and LET expressions Collections and sorting Resource W3C recommendation:

FLWR Expressions A FLWR (pronounced "flower")

expression is constructed from FOR, LET, WHERE, and RETURN clauses, which must appear in a specific order.

A FLWR expression binds values to one or more variables and then uses these variables to construct a result.

Page 43: Xquery. Summary of XQuery FLWR expressions FOR and LET expressions Collections and sorting Resource W3C recommendation:

FLWR Expressions (Q) List each publisher and the average price of its

books. FOR $p IN distinct(document("bib.xml")//publisher)

LET $a := avg(document("bib.xml")//book[publisher = $p]/price)RETURN

<publisher> <name> {$p/text()} </name> <avgprice> {$a} </avgprice>

</publisher>

Page 44: Xquery. Summary of XQuery FLWR expressions FOR and LET expressions Collections and sorting Resource W3C recommendation:

Sorting

A sequence can be ordered by means of a SORTBY clause that contains one or more "ordering expressions."

(Q)List all books with price greater than $100, in order by first author; within each group of books with the same first author, list the books in order by title.

document("bib.xml")//book[price > 100] SORTBY (author[1], title)

Page 45: Xquery. Summary of XQuery FLWR expressions FOR and LET expressions Collections and sorting Resource W3C recommendation:

Operators and Expressions (Q) Prepare a "critical sequence" report consisting of all

elements that occur between the first and second incision in the first procedure.

<critical_sequence>{LET $p := //procedure[1]FOR $e IN //* AFTER ($p//incision)[1]BEFORE ($p//incision)[2] RETURN shallow($e)}</critical_sequence>

The shallow function makes a shallow copy of a node, including attributes but not including subelements.

Page 46: Xquery. Summary of XQuery FLWR expressions FOR and LET expressions Collections and sorting Resource W3C recommendation:

Conditional Expressions

(Q) Make a list of holdings, ordered by title. For journals, include the editor, and for all other holdings, include the author.

FOR $h IN //holding RETURN

<holding>{$h/title,

IF ($h/@type = "Journal") THEN $h/editor ELSE $h/author

}</holding>

SORTBY (title)

Page 47: Xquery. Summary of XQuery FLWR expressions FOR and LET expressions Collections and sorting Resource W3C recommendation:

Quantified Expressions

(Q) Find titles of books in which both sailing and windsurfing are mentioned in the same paragraph.

FOR $b IN //book WHERE SOME $p IN $b//para SATISFIES

(contains($p, "sailing") AND contains($p, "windsurfing"))

RETURN $b/title

Page 48: Xquery. Summary of XQuery FLWR expressions FOR and LET expressions Collections and sorting Resource W3C recommendation:

More Examples

http://www-106.ibm.com/developerworks/xml/library/x-xquery.html

Page 49: Xquery. Summary of XQuery FLWR expressions FOR and LET expressions Collections and sorting Resource W3C recommendation:

<!ELEMENT bib (book* )> <!ELEMENT book (title, (author+ | editor+

), publisher, price )> <!ATTLIST book year CDATA #REQUIRED

> <!ELEMENT author (last, first )> <!ELEMENT editor (last, first, affiliation )>

<!ELEMENT title (#PCDATA )> <!ELEMENT last (#PCDATA )> <!ELEMENT first (#PCDATA )> <!ELEMENT affiliation (#PCDATA )> <!

ELEMENT publisher (#PCDATA )> <!ELEMENT price (#PCDATA )>