xquery language l.
Skip this Video
Loading SlideShow in 5 Seconds..
XQuery language PowerPoint Presentation
Download Presentation
XQuery language

Loading in 2 Seconds...

play fullscreen
1 / 45

XQuery language - PowerPoint PPT Presentation

  • 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

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
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

XQuery language

Presented by: Tayeb sbihi

supervised by: Dr. H. Haddouti

why xquery
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
Why not other languages ??
  • Document-oriented
    • XQL, XPath, XSLT
  • Relational
    • SQL
  • Object
    • OQL
  • Semi-structured
    • XML-QL, Lorel, YATL, UnQL
who made it
Who made it ??
  • Quilt
    • 3 people
  • World Wide Web Consortium (W3C)
  • Query Language Working Group
  • XML Query Working Group
who cont
Who (cont.) ??
  • 5 stages to W3C “Recommendation”
    • Working Draft
    • Last Call Working Draft
    • Candidate Recommendation
    • Proposed Recommendation
    • W3C Recommendation
when where
When/Where ??
  • W3C Query Language Workshop: Boston, December 1998
  • First Draft: April 2001
  • Working Draft: Draft 7, June 2001
  • Final: ?




XSL patterns




XQL - 99



xml query requirements
XML Query Requirements


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



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.


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


xquery expressions
XQuery Expressions
  • Path Expressions
  • Element Constructors
  • FLWR Expressions
  • Expressions with Operators/Functions
  • Conditional Expressions
  • Quantified Expressions

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
XQuery Element Constructor
  • Start/End Tag + Enclosed List of Expressions
    • Generate an element with a computed name that

contains nested elements:


<description> $d </description>

<price> $p </price>


xquery for let where return flwr
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
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
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
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)



<name> {$p/text()} </name>

<avgprice> {$a} </avgprice>


distinct = a function that eliminates duplicates

xquery operators and functions
XQuery Operators and Functions
  • Infix/Prefix Operators
    • e.g., Infix Operators BEFORE and AFTER
  • Parenthesized Expressions
  • Arithmetic/Logical Operators
  • Collection Operators
  • Functions Can Be Defined in XQuery
conditional expressions
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




IF ($h/@type = "Journal")

THEN $h/editor

ELSE $h/author



SORTBY (title)

if then else

FOR$h IN //holding

RETURN <holding>


IF$h/@type = "Journal"



</holding> SORTBY (title)

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

  • 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
Sorting in XQuery



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

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

RETURN <book>

$b/title ,


</book> SORTBY(price DESCENDING)

</publisher> SORTBY(name)


collections in xquery
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...)


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





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

collections in xquery29
Collections in XQuery

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

group by in xquery
Group-By in Xquery ??

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


WHERE$b/publisher="Morgan Kaufmann"


WHERE count($b) > 10

IN <year> $y </year>

 with GROUPBY

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



WHERE Bib.publisher="Morgan Kaufmann"


HAVING count(*) > 10

Equivalent SQL 

joins in relation
JOINS in Relation

P (part)

P_no descrip qnty

S (supplier)

s_no name locat

SP (Supplies)

p_no s_no price

xml documents
XML documents











































joins in xquery
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
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

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



Xml document
xquery example 1
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 }




Xquery example 1
expected result
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>
xquery example 2
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>



XQuery Example 2
expected results

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




Expected Results
xquery example 3
For each book found at both bn.com and amazon.com, list the title of the book and its price from each source:


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


XQuery example 3
xquery example 4
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.


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


XQuery Example 4

<title>Data on the Web</title>

<author> <last>Abiteboul</last> <first>Serge</first>


<author> <last>Buneman</last> <first>Peter</first>




xquery example 5
Xquery example 5
  • 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 }


sort by (title) }


  • W3C XQuery


  • W3C XML Query Use Cases


  • W3C XML Query Requirements


  • W3C XML Query Data Model


  • W3C XML Query Algebra


q a session
Q/A Session

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