XQUERY
BY
N.RAVIKUMAR
Xquery
XQuery is designed to query XML data - not just XML
files, but anything that can appear as XML, including
databases.
Xquery is to Xml while SQL is to database tables.
"The mission of the XML Query project is to provide
flexible query facilities to extract data from real and
virtual documents on the World Wide Web, therefore
finally providing the needed interaction between the
Web world and the database world. Ultimately,
collections of XML files will be accessed like
databases"
XML
Extensible Markup Language is a text-based
Markup language that enables you to store data
in structured format by using meaningful tags.
The term 'extensible' implies that you can
extend your ability to describe a document by
defining meaningful tags for your applications.
XML is Cross-platform, software independent
markup language.
XML can be used to transfer structured data
between heterogeneous systems.
XML advantages
Domain Specific Library
Ability to define meaningful tags.
Data interchange.
Smart searches.
Granular updates
When the Data in XML document is to be updated no
need to reload the entire page from the server.
Only the Changed contents need to be downloaded.
User selected view of Data
> CSS
> XSL
XML structure
Processing Instruction
XML document usually begins with the declaration
statement called as processing instruction.
<?xml version=”1.0” encoding=”UTF-8”?>
Elements
Elements are basic units that are used to identify and describe
data in XML.
<author>Bjarne Stroustroup</author>
Attributes
Additional information about the element for which they are
declared.
<student sid=”10mx01”>
Comments
<!-- comment text -->
Sample XML program
<?xml version="1.0" ?>
<!-- sample comments -->
<MARKSSUMMARY>
<STUDENT ID="S001">
<NAME>Anandh</NAME>
<MATHS>90</MATHS>
<SCIENCE>71</SCIENCE>
<SOCIALSTUDIES>90</SOCIALSTUDIES>
</STUDENT>
</MARKSSUMMARY>
OUTPUT
XML databases
Managing:
> large XML documents
> many Xml documents
Manage
> Query (Insert, Update, Delete).
> Multiple users
> Complex queries
> ACID properties
Xquery Origin
XQuery 1.0 was developed by the XML Query
working group of the W3C.
The work was closely coordinated with the
development of XSLT 2.0 by the XSL Working
Group; the two groups shared responsibility for
XPath 2.0, which is a subset of XQuery 1.0.
XQuery 1.0 became a W3C Recommendation
on January 23, 2007.
XQUERY Overview
Path Expressions
Element constructors
FLWOR expressions
Path expressions
Path expressions are used to navigate input
documents to select elements and attributes of
interest.
A path expression is made up of one or more
steps that are separated by a slash(/) or double
slashes(//).
e.g
doc(“marks.xml”)/markssummary/@student
(Returns all attributes of element student)
Element Construction
An XQuery expression can construct new
values or structures
Example: Consider the path expressions.
It returns a newly constructed sequence of
elements.
Key point is that we don’t just return
existing structures or atomic values; we can
re-arrange them as we wish into new
structures
FLWOR
Xquery syntax is like SQL-like 'FLWOR'
expression.
F - For
L - Let
W- Where
O - Order BY
R - Return
FOR vs. LET
FOR $x IN expression
Binds $x in turn to each value in the list expr.
LET $x = expression
Binds $x to the entire list expr
Useful for common sub-expressions and for
aggregations
Cont..
FOR
FOR$x
$xIN
INdocument("bib.xml")/bib/book
document("bib.xml")/bib/book
RETURN
RETURN<result>
<result>$x
$x</result>
</result>
Returns:
<result> <book>...</book></result>
<result> <book>...</book></result>
<result> <book>...</book></result>
...
LET
LET$x
$xIN
INdocument("bib.xml")/bib/book
document("bib.xml")/bib/book
RETURN
RETURN<result>
<result>$x
$x</result>
</result>
Returns:
<result> <book>...</book>
<book>...</book>
<book>...</book>
...
</result>
XQuery Example
Find all book titles published after 1995:
FOR
FOR$x
$xIN
INdocument("bib.xml")/bib/book
document("bib.xml")/bib/book
WHERE
WHERE$x/year
$x/year>>1995
1995
RETURN
RETURN$x/title
$x/title
Result:
<title> abc </title>
<title> def </title>
<title> ghi </title>
XML and Relational
Data
name phone row row row
John 3634
phone phone phone
name name name
Sue 6343
Dick 6363 “John” 3634 “Sue” 6343 “Dick” 6363
{ row: { name: “John”, phone: 3634 },
Relation row: { name: “Sue”, phone: 6343 },
row: { name: “Dick”, phone: 6363 }
… in XML
}
Cont..
• XML stores data in sequence of bytes whereas
in RDBMS we need to deal with complexity of
decomposing the data into a large number of
tables.
• Xquery has relational operators like(<,>,<=,>=,!
=,=) .
• BETWEEN is not directly supported but we can
use two comparisons.
• XQUERY supports Regular Expressions much
powerful than SQL’s LIKE.
Capabilities of Xquery
• Selecting information based on specific criteria.
• Filtering out unwanted information.
• Searching of information within a document or
set of documents.
• Joining data from multiple documents.
• Sorting and grouping.
• Transforming and restructuring XML data into
another structure.
Applications
Extracting information from a database for a
use in web service.
Generating summary reports on data stored in
an XML database.
Searching textual documents on the Web for
relevant information and compiling the results.
Selecting and transforming XML data to
XHTML to be published on the Web.
Reference
Xquery – Priscilla Walmsley
www.w3.org/TR/xquery
http://www.xml.com/pub/a/2002/10/16/xquery.html
www.brics.dk/~amoeller/XML/querying
THANK YOU
The un-queried life is not worth living
— Socrates (Plato, The Apology, 38a)