Xquery language
Download
1 / 45

XQuery language - PowerPoint PPT Presentation


  • 273 Views
  • Uploaded on

XQuery language . Presented by: Tayeb sbihi supervised by: Dr. H. Haddouti . Why XQuery ??. Other query languages are too specific XML covers many facets of data modeling Need a query language to reflect that. Why not other languages ??. Document-oriented XQL, XPath, XSLT Relational

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'XQuery language' - elina


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
Xquery language l.jpg

XQuery language

Presented by: Tayeb sbihi

supervised by: Dr. H. Haddouti


Why xquery l.jpg
Why XQuery ??

  • Other query languages are too specific

  • XML covers many facets of data modeling

  • Need a query language to reflect that


Why not other languages l.jpg
Why not other languages ??

  • Document-oriented

    • XQL, XPath, XSLT

  • Relational

    • SQL

  • Object

    • OQL

  • Semi-structured

    • XML-QL, Lorel, YATL, UnQL


Who made it l.jpg
Who made it ??

  • Quilt

    • 3 people

  • World Wide Web Consortium (W3C)

  • Query Language Working Group

  • XML Query Working Group


Who cont l.jpg
Who (cont.) ??

  • 5 stages to W3C “Recommendation”

    • Working Draft

    • Last Call Working Draft

    • Candidate Recommendation

    • Proposed Recommendation

    • W3C Recommendation


When where l.jpg
When/Where ??

  • W3C Query Language Workshop: Boston, December 1998

  • First Draft: April 2001

  • Working Draft: Draft 7, June 2001

  • Final: ?


Origins l.jpg
Origins

XQL

SQL

Xpointer

XSL patterns

OQL

XML - QL

Xpath

XQL - 99

Quilt

XQuery


Xml query requirements l.jpg
XML Query Requirements

General:

  • Declarative Language

  • Readable XML Syntax

  • Protocol Independence

  • Standard Error Conditions

  • Support for Future Updates

    Data Model:

  • Based on XML Infosets

  • Namespace Aware

  • Support for XML Schema Data Types

  • Support Inter/Intra Document References


Xml query requirements cont l.jpg
XML Query Requirements (cont.)

Query Functionality:

  • Operators on All Data Types

  • Text Operators Across Element Boundaries

  • Hierarchies and Sequences

  • Combination of Data from Various Locations

  • Aggregation and Sorting

  • Combination of Operators (Queries as Operands)

  • Support NULL values

  • Preservation of Structure/Identity

  • Operations on Names/Schemas

  • Extensibility & Closure


What is xquery l.jpg
What is XQuery

  • A language for querying XML

  • A human-readable syntax for an XML query language

  • A functional programming language

  • The product of the W3C’s XML Query Working Group


Xquery l.jpg
XQuery

  • Functional Language

    • Query Represented as an Expression

  • Expressions can be Nested without Restriction

  • Input/Output of an XQuery are Instances of the XML

  • Query Data Model

  • Based on OQL, SQL, XML-QL, XPath

  • Readable XML Syntax


Xquery 1 0 xpath 2 0 l.jpg
XQuery 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


Symbols l.jpg

.

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.

Symbols


Xquery expressions l.jpg
XQuery Expressions

  • Path Expressions

  • Element Constructors

  • FLWR Expressions

  • Expressions with Operators/Functions

  • Conditional Expressions

  • Quantified Expressions


Slide15 l.jpg

  • 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."


Xquery element constructor l.jpg
XQuery Element Constructor

  • Start/End Tag + Enclosed List of Expressions

    • Generate an element with a computed name that

      contains nested elements:

      <$tagname>

      <description> $d </description>

      <price> $p </price>

      </$tagname>


Xquery for let where return flwr l.jpg
XQuery For Let Where Return (FLWR)

  • FOR and LET Clause

    • Generate a List of Tuples that Preserves Doc Order

  • WHERE Clause

    • Applies a Predicate to Eliminate Some Tuples

  • RETURN Clause

    • Executed on Resulting Tuples -> Ordered Output List

  • Syntax:

    FOR var IN expr WHERE expr RETURN expr

    LET var := expr


Flwr expressions l.jpg
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.


Xml document l.jpg
XML Document

  • <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>


Flwr expressions20 l.jpg
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>

    distinct = a function that eliminates duplicates


Xquery operators and functions l.jpg
XQuery Operators and Functions

  • Infix/Prefix Operators

    • e.g., Infix Operators BEFORE and AFTER

  • Parenthesized Expressions

  • Arithmetic/Logical Operators

  • Collection Operators

    • e.g., UNION, INTERSECT, EXCEPT

  • Functions Can Be Defined in XQuery


Conditional expressions l.jpg
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 ([email protected] = "Journal")

THEN $h/editor

ELSE $h/author

}

</holding>

SORTBY (title)


If then else l.jpg
If-Then-Else

FOR$h IN //holding

RETURN <holding>

$h/title,

[email protected] = "Journal"

THEN$h/editor

ELSE$h/author

</holding> SORTBY (title)


Quantifiers l.jpg
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


Xquery quantified expressions l.jpg
XQuery 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


Sorting l.jpg
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)


Sorting in xquery l.jpg
Sorting in XQuery

<publisher_list>

FOR$pINdistinct(document("bib.xml")//publisher)

RETURN <publisher> <name> $p/text() </name> ,

FOR$bIN document("bib.xml")//book[publisher = $p]

RETURN <book>

$b/title ,

[email protected]

</book> SORTBY(price DESCENDING)

</publisher> SORTBY(name)

</publisher_list>


Collections in xquery l.jpg
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...)

Returns:

<result> <author>...</author>

<author>...</author>

<author>...</author>

...

</result>

RETURN <result> $b/author </result>


Collections in xquery29 l.jpg
Collections in XQuery

What about collections in expressions [email protected] list of n [email protected] * 0.7  list of n [email protected] * [email protected]  list of n x m numbers [email protected] * ([email protected] + [email protected])  [email protected] * [email protected] + [email protected] * [email protected] !!


Group by in xquery l.jpg
Group-By in Xquery ??

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

[email protected]

WHERE$b/publisher="Morgan Kaufmann"

RETURNGROUPBY $y

WHERE count($b) > 10

IN <year> $y </year>

 with GROUPBY

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

SELECT year

FROM Bib

WHERE Bib.publisher="Morgan Kaufmann"

GROUPBY year

HAVING count(*) > 10

Equivalent SQL 


Joins in relation l.jpg
JOINS in Relation

P (part)

P_no descrip qnty

S (supplier)

s_no name locat

SP (Supplies)

p_no s_no price


Xml documents l.jpg
XML documents

P.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>

</sp_tuple>

</supplies_part>


Joins in xquery l.jpg
JOINS in XQuery

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

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

p_no = $sp/p_no]

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

[s_no = $sp/s_no]

Return <sp_pair> {

$s/name, $p/descrip }

</sp_pair> sortby(name, descrip)


Additional issues l.jpg
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


Xml document35 l.jpg

<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>

Xml document


Xquery example 1 l.jpg

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 [email protected] > 1991

return <book year="{ [email protected] }">

{ $b/title }

</book>

}

</bib>

Xquery example 1


Expected result l.jpg
Expected Result their year and title.

  • <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 2 l.jpg

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>

XQuery Example 2


Expected results l.jpg

<results> pair enclosed in a "result" element.

<result> <title>TCP/IP Illustrated</title> <author>

<last>Stevens</last>

<first>W.</first>

</author>

Expected Results


Xquery example 3 l.jpg

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>

XQuery example 3


Xquery example 4 l.jpg

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>

XQuery Example 4


Result l.jpg

<book> and first two authors, and an empty "et-al" element if the book has additional authors.

<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>

Result


Xquery example 5 l.jpg
Xquery example 5 and first two authors, and an empty "et-al" element if the book has additional authors.

  • 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 [email protected] > 1991

    return <book>

    { [email protected] }

    { $b/title }

    </book>

    sort by (title) }

    </bib>


References l.jpg
References and first two authors, and an empty "et-al" element if the book has additional authors.

  • W3C XQuery

    http://www.w3.org/TR/xquery.html

  • W3C XML Query Use Cases

    http://www.w3.org/TR/xmlquery-use-cases.html

  • W3C XML Query Requirements

    http://www.w3.org/TR/xmlquery-req.html

  • W3C XML Query Data Model

    http://www.w3.org/TR/query-datamodel.html

  • W3C XML Query Algebra

    http://www.w3.org/TR/query-algebra.html


Q a session l.jpg
Q/A Session and first two authors, and an empty "et-al" element if the book has additional authors.

Your questions and feedback are most welcome......


ad