0% found this document useful (0 votes)
7 views24 pages

XQuery

The document provides an overview of XQuery, a query language designed for querying semi-structured data, particularly XML. It covers the history, syntax, and key components of XQuery, including FLOWR expressions, conditional expressions, and user-defined functions. Additionally, it discusses the limitations of SQL and the evolution of query languages leading to the adoption of XQuery by W3C in 2005.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views24 pages

XQuery

The document provides an overview of XQuery, a query language designed for querying semi-structured data, particularly XML. It covers the history, syntax, and key components of XQuery, including FLOWR expressions, conditional expressions, and user-defined functions. Additionally, it discusses the limitations of SQL and the evolution of query languages leading to the adoption of XQuery by W3C in 2005.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

XQuery

Ahmed ZELLOU
[email protected]
Sup-MTI, 2024-2025.
XQuery

Plan
!History
!Introduction
!XQuery Syntax
!FLOWR expression
!Conditional Expressions
!XQuery Functions
!User functions
!XQUF: XQuery Update Facility
!Workshop
A.ZELLOU
XQuery

Limitations of SQL
! SQL has 45 years
! Part of the assertion languages: QUEL (Berkeley,
INGRES 77 system), QBE 77 and SQL IBM82.
! Found these origins in relational algebra in 1970
! Standardized language [ISO89, ISO92].
! SQL2 [ISO92] is divided into three levels:
! The entry level is an improvement of SQL1.
! The intermediate levels fully support the relational
model.
! SQL3 too complex
! Object and deductive features.
! Nested queries are difficult

A.ZELLOU
XQuery

History
! Objective
! Querying semi-structured data.
! Multiple languages
! OQL, OEM-QL, XPath, XML-QL [Deutsch 1998] of
AT&T, XQL [Robie 1998] of Microsoft, QUILT [Robie
2000] of IBM, XMAS [Luascher 1999] and XQuery.
! Choice
! 2005, W3C choises XQuery with version 1.0.
! It is an extension of Quilt language, also based on the
XPath 1.0, XQL, XML-QL, SQL, and OQL languages.

A.ZELLOU
XQuery

Introduction
! XQuery
! A typed XML query language, based on path
expressions, loops, tests, and building XML document
blocks.
! Compatible with several W3C standards such as XML,
namespaces, XSLT, XPath and XML Schema.
! Xquery, XSLT and XPath share the same data model and
support the same functions and operators.
! A W3C Recommendation
! Version 1.0: January 23, 2007.
! Version 2.0: March 17, 2011.
! Version 3.0: March 17, 2014.
! Version 3.1: March 21, 2017.

A.ZELLOU
XQuery

XQuery
! An XQuery query is built on an XPath expression.
! It offers two services:
! Querying XML documents
! Building results in semi-structured format.
! Results are generated as XML fragments.
! Some basic rules:
! An XQuery variable is defined with $
! XQuery comments are delimited by
! (: XQuery Comment:)

A.ZELLOU
XQuery

XQuery
!The doc() function is used to access the
data fileà doc ("file.xml")
!Using Xpath to navigate through elements
in an XML document.
! Example : doc("library.xml")/library/book/title
! Allows to select all titles in the "library.xml"
file:
!Using predicates to test on data.
! doc("library.xml")/library/book[price<30]

A.ZELLOU
XQuery

Xquery :FLOWR
! A query is based on the FLOWR = "For-Let-
Where-Order-Return" statement.
! Each FLOWR expression is delimited by
{...}.
! The general form of a FLOWR query is:
! for$variable in Xpath_search_expression
! let $variable:=expressions_Xpath
! wherel ogical_expression
! order by $variable
! return expression

A.ZELLOU
XQuery

FLOWR
! Example :
! for $x in doc("library.xml")/library/book
where $x/price>30
order by $x/title
return $x/title
! for - (optional) binds a variable to each element
returned by the expression
! let - (optional) to give another name to the variable
! where - (optional) specifies a selection criterion
! order by - (optional) specifies the order type of the
result
! return - specifies what the result returns

A.ZELLOU
XQuery

FLOWR
! The conditions
! doc("library.xml")/library/book[price>30]/title
! The FLWOR expression is:
! for $x in doc("library.xml")/library/book
where $x/price>30
return $x/title
! for : selects all book elements from library element
into a variable called $x.
! where: filter on book elements with price>30
! order by: for sorting
! return: specifies what the query returns.

A.ZELLOU
XQuery

The FOR clause


! Allows to iterate over a list of XML fragments.
! It associates with each $variablean XML path
fragment defined by Xpath.
! Only one for clause is inserted in a FLWOR
expression.
! To loop a finite number of times with FOR, use
to:
! for $x in (1 to 5) Which gives:
<test>1</test>
return <test>{$x}</test> <test>2</test>
<test>3</test>
<test>4</test>
<test>5</test>

A.ZELLOU
XQuery

The FOR clause


!Allows multiple path expressions to be
putted in a for clause.
!Use a comma to separate path
expressions:
! for $x in (10,20), $y in (100,200)
return <test>x={$x} and y={$y}</test>
!Which gives:
! <test>x=10 and y=100</test>
<test>x=10 and y=200</test>
<test>x=20 and y=100</test>
<test>x=20 and y=200</test>

A.ZELLOU
XQuery

The FOR clause


!The keyword at is used to count the
number of iterations:
! for$x at $i in
doc("library.xml")/library/book/title
return <book>{$i}. {data($x)}</book>
!Which gives:
! <book>1.Client/Server Concepts</book>
<book>2. Web Services</book>
<book>3. Relational DataBases</book>
<book>4. Learning XML</book>

A.ZELLOU
XQuery

The let clause


!The clause let is optional, it allows to
associate the result of an Xpath expression
with a variable.
!Objective: avoid repeating the same
expression.
!let does not translate to iteration.
! let$x := (1 to 5)
return <test>{$x}</test>
!Which gives:
! <test>1 2 3 4 5</test>

A.ZELLOU
XQuery

The where clause


!The clause where allows to define a
selection condition to construct the
response.
!The selection is made by a logical
expression of elementary predicates.
! where $x/price>30 and $x/price<100

! where contains($x/title,"XML") or not


contains($x/author,"zellou")

A.ZELLOU
XQuery

The order by clause


! The clause order allows to sort the results
(ascending to descending).
! To order the result by category and title:
! for $x in doc("library.xml")/library/book
order by $x/@category, $x/title
return $x/title
! Which gives:
! <title lang="en"> Client/Server Concepts </title>
<title lang="en"> Web Services </title>
<title lang="en"> Relational DataBases </title>
<title lang="en"> Learning XML </title>

A.ZELLOU
XQuery

The return clause


! Specifies what the query returns.
! Each iteration should return a single XML fragment
(not a collection).
! for $x in doc("library.xml")/library/book
return $x/title
! Which gives:
! <title lang="en"> Client/Server Concepts </title>
<title lang="en"> Web Services </title>
<title lang="en"> Relational DataBases </title>
<title lang="en"> Learning XML </title>

A.ZELLOU
XQuery

Conditional Expressions

! With if/else.
! for $x in doc("library.xml")/library/book
return if ($x/@category="web")
then <web>{data($x/title)}</web>
else <DB>{data($x/title)}</DB>
! Which gives:
! <DB> Client/Server Concepts </DB>
<web>Web Services</web>
<DB> Relational DataBases </DB>
<web>Learning XML</web>

A.ZELLOU
XQuery

General
! Query expressions can be arbitrarily nested.
! It is possible to nest queries at for, where, and
return.
! Aggregation functions: count, min, max...
! Text search:=, contains.

A.ZELLOU
XQuery

XQuery Functions
! XQuery, XPath and XSLT share the same
functions library.
! Example 1: in an XPath expression
! doc("library.xml")/library/book[substring(title,1,3)='X
ML']
! Example 2: in a let clause
! let $name := (max($price))
! Example 3: in an element
! <name>{upper-case($booktitle)}</name>

A.ZELLOU
XQuery

User Functions
! Defining functions with XQuery.
Use the function keyword
for declaration

The function name


declare function prefix:function_name
($parameter as datatype)
as returnDatatype Parameters and return
{ type.

...function code here...


}

The function body

A.ZELLOU
XQuery

User Functions
! Example :
! declare function local:minPrice($a as xs:decimal,$b as
xs:decimal)
as xs:decimal
{
if ($a > $b) let $res := $b
else let $res := $a
return ($res)
}
! The call to the function:
! <minPrice>{local:minPrice($book/price,$book/discou
nt)}</minPrice>

A.ZELLOU
XQuery

Workshop
! Example :
! declare function local:minPrice($a as xs:decimal,$b as
xs:decimal)
as xs:decimal
{
if ($a > $b) let $res := $b
else let $res := $a
return ($res)
}
! The call to the function:
! <minPrice>{local:minPrice($book/price,$book/discou
nt)}</minPrice>

A.ZELLOU
THANKS

You might also like