XQuery
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
A.ZELLOU
XQuery
A.ZELLOU
XQuery
A.ZELLOU
XQuery
A.ZELLOU
XQuery
A.ZELLOU
XQuery
A.ZELLOU
XQuery
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
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