XXX-xpq - Rose
Download
Report
Transcript XXX-xpq - Rose
XML Query Languages
Salman Azhar
XPATH
XQUERY
These slides use some figures, definitions, and explanations from ElmasriNavathe’s Fundamentals of Database Systems
and Molina-Ullman-Widom’s Database Systems
2/10/05
Salman Azhar: Database Systems
1
XPATH and XQUERY
Two query language to search in XML
documents
XPATH
XQUERY
2/10/05
Language for describing paths
Language for querying XML documents
Salman Azhar: Database Systems
2
XPATH
A language for describing paths in XML
documents
More precisely XPATH describes
semistructured data graph and its paths
XML documents can be described as semistructured data graphs
2/10/05
each subobject as a node of a graph
with its subobjects as its children
Salman Azhar: Database Systems
3
XQUERY
XQUERY is a full query language for
XML documents
using path expressions from XPATH
XQUERY use FLWR (pronounced
“flower”) expression
2/10/05
corresponds to SQL’s select-from-where
FLWR stands for “for-let-where-return”
Salman Azhar: Database Systems
4
Example DTD
<!DOCTYPE Rests [ RESTS have 0 or more REST & SODA
<!ELEMENT RESTS (REST*, SODA*)>
Every REST has 1 or
<!ELEMENT REST (PRICE+)>
more PRICE and also
<!ATTLIST REST name = ID> an attribute name
PRICE has
<!ELEMENT PRICE (#PCDATA)>
data for price
<!ATTLIST PRICE theSoda = IDREF> & the Soda
with that
<!ELEMENT SODA ()>
price
<!ATTLIST SODA name = ID, soldBy = IDREFS>
SODA name for ID and IDREFs
]>
for the rest that sell the soda.
2/10/05
Salman Azhar: Database Systems
5
Example Document
<RESTS>
<REST name = “JoesRest”>
<PRICE theSoda =“Dew”>1.50</PRICE>
<PRICE theSoda = “Slice”>1.75</PRICE>
</REST> …
<SODA name = “Dew”,
soldBy = “JoesRest, SuesRest,…”>
</SODA> …
</RESTS>
2/10/05
Salman Azhar: Database Systems
6
XPATH Path Descriptors
Let us study XPATH first
Queries are really path descriptors
Look like UNIX path description
with tags instead of directories and files
Tags are separated by /
Simple path descriptors are
2/10/05
sequences of tags separated by slashes (/)
Salman Azhar: Database Systems
7
Example: /RESTS/REST/PRICE
<RESTS>
<REST name = “JoesRest”>
<PRICE theSoda =“Dew”>1.50</PRICE>
<PRICE theSoda = “Slice”>1.75</PRICE>
</REST> …
<SODA name = “Dew”,
soldBy = “JoesRest, SuesRest,…”>
/RESTS/REST/PRICE describes
</SODA> …
The set with these two PRICE
</RESTS>
Objects as well as the PRICE
objects for any other restaurants.
2/10/05
Salman Azhar: Database Systems
8
XPATH Path Descriptors
If the descriptor begins with /,
then the path starts at the root and has
those tags, in order
If the descriptor begins with //,
2/10/05
then the path can start anywhere
Salman Azhar: Database Systems
9
Example: //PRICE
<RESTS>
<REST name = “JoesRest”>
<PRICE theSoda =“Dew”>1.50</PRICE>
<PRICE theSoda = “Slice”>1.75</PRICE>
</REST> …
<SODA name = “Dew”,
soldBy = “JoesRest, SuesRest,…”>
//PRICE describes the same PRICE
</SODA> …
objects, but only because the DTD
</RESTS>
forces every PRICE to appear within
a RESTS and a REST.
2/10/05
Salman Azhar: Database Systems
10
Wild-Card *
A star (*) in place of a tag represents
any one tag
acts as a “wildcard”
Example: /*/*/PRICE represents
2/10/05
all price objects at the third level of nesting
Salman Azhar: Database Systems
11
Example: /RESTS/*
<RESTS>
<REST name = “JoesRest”>
<PRICE theSoda =“Dew”>1.50</PRICE>
<PRICE theSoda = “Slice”>1.75</PRICE>
</REST> …
<SODA name = “Dew”, soldBy = “JoesRest,
SuesRest,…”>
</SODA> …
/RESTS/* captures all REST
</RESTS>
and SODA objects, such
as these.
2/10/05
Salman Azhar: Database Systems
12
Attributes
We may refer to attributes in addition to
tags
In XPATH, we refer to attributes by prepending @ to their name
Attributes of a tag may appear in paths
2/10/05
as if they were nested within that tag
Salman Azhar: Database Systems
13
Example: /RESTS/*/@name
<RESTS>
<REST name = “JoesRest”>
<PRICE theSoda =“Dew”>1.50</PRICE>
<PRICE theSoda = “Slice”>1.75</PRICE>
</REST> …
<SODA name = “Dew”, soldBy = “JoesRest,
SuesRest,…”>
/RESTS/*/@name selects all
</SODA> …
name attributes of immediate
</RESTS>
subobjects of the RESTS object.
2/10/05
Salman Azhar: Database Systems
14
Selection Conditions
A condition inside […] may follow a tag
If so, the only paths included in the
result of a path expression are ones
that
2/10/05
have that tag and
also satisfy the condition
Salman Azhar: Database Systems
15
Example: Selection Condition
/RESTS/REST/PRICE[PRICE < 1.60]
<RESTS>
<REST name = “JoesRest”>
<PRICE theSoda = “Dew”>1.50</PRICE>
<PRICE theSoda = “Slice”>1.75</PRICE>
</REST> …
The condition that the PRICE be
< $1.60 makes this price but not
the price of Slice satisfy the path
descriptor.
2/10/05
Salman Azhar: Database Systems
16
Example: Attribute in Selection
/RESTS/REST/PRICE[@theSoda = “Slice”]
<RESTS>
<REST name = “JoesRest”>
<PRICE theSoda = “Dew”>1.50</PRICE>
<PRICE theSoda = “Slice”>1.75</PRICE>
</REST> …
Now, this PRICE object is
selected, along with any
other prices for Slice.
2/10/05
Salman Azhar: Database Systems
17
Optional: Axes
In general, path expressions allow us
to start at the root
and execute a sequence of steps
to find a set of nodes
at each step, we may follow any one of several
axes
The default axis is child::
go to any child of the current set of nodes
2/10/05
Salman Azhar: Database Systems
18
Optional: Example of Axes
/RESTS/SODA is really shorthand for
/RESTS/child::SODA
@ is really shorthand for the attribute::
axis. Thus,
2/10/05
/RESTS/SODA[@name = “Dew” ] is
shorthand for
/RESTS/SODA[attribute::name = “Dew”]
Salman Azhar: Database Systems
19
Optional: More Axes
Some other useful axes are:
1.
2.
parent:: = parent(s) of the current
node(s)
descendant-or-self:: = the current
node(s) and all descendants
3.
2/10/05
Note: // is really a shorthand for this axis.
ancestor::, ancestor-or-self, etc.
Salman Azhar: Database Systems
20
XQUERY
XQUERY allows us to query XML
documents
using path expressions from XPATH to
describe important sets
XQUERY use FLWR (pronounced
“flower”) expressions
2/10/05
stands for “for-let-where-return”
corresponds to SQL’s select-from-where
Salman Azhar: Database Systems
21
XQUERY SQL Mapping
where WHERE
return SELECT
for/let FROM
2/10/05
Salman Azhar: Database Systems
22
FLWR (FLoWeR) Expressions
FLWR expressions are made up of
1.
2.
3.
2/10/05
One or more FOR and/or LET clauses.
Then an optional WHERE clause.
A RETURN clause.
Salman Azhar: Database Systems
23
FOR Clauses
FOR <variable> IN <path expression>,…
Variables begin with $.
A FOR variable takes on
each object in the set denoted by the path
expression (in turn)
Whatever follows this FOR is
2/10/05
executed once for each value of the variable
creates a loop
Salman Azhar: Database Systems
24
Example: FOR
FOR $soda IN /RESTS/SODA/@name
RETURN <SODANAME>$soda</SODANAME>
$soda ranges over the name attributes
of all sodas in our example document
Result is a list of tagged names, like
<SODANAME>Dew</SODANAME>
<SODANAME>Slice</SODANAME>…
2/10/05
Salman Azhar: Database Systems
25
LET Clauses
LET <variable> := <path expression>,…
Value of the variable becomes the set
of objects defined by the path
expression
Note:
2/10/05
LET does not cause iteration
FOR does cause iteration
Salman Azhar: Database Systems
26
Example: LET
LET $sodas := /RESTS/SODA/@name
RETURN <SODANAMES>$sodas</SODANAMES>
Returns one object with all the names of
the sodas, like:
<SODANAMES>Dew, Slice,…</SODANAMES>
2/10/05
Salman Azhar: Database Systems
27
Following IDREFs
XQUERY (but not XPATH) allows us
to use paths that follow attributes that are
IDREFs
If x denotes a set of IDREFs,
2/10/05
then x =>y denotes all the objects with
tag y whose IDs are one of these IDREFs
Salman Azhar: Database Systems
28
Example
Find all the soda objects where the soda
is sold by Joe’s Rest for less than 1.60
Strategy:
1.
2.
$soda will for-loop over all soda objects
For each $soda,
a.
b.
3.
let $joe be either the Joe’s-Rest object, if Joe
sells the soda,
or the empty set of rest objects.
Test whether $joe sells the soda for < 1.60
2/10/05
Salman Azhar: Database Systems
29
Example: The Query
Attribute soldBy is of type
IDREFS. Follow each ref
to a REST and check if its
name is Joe’s Rest.
FOR $soda IN /RESTS/SODA
LET $joe := $soda/@soldBy=>REST[@name=“JoesRest”]
LET $joePrice := $joe/PRICE[@theSoda=$soda/@name]
WHERE $joePrice < 1.60
RETURN <CHEAPSODA>$soda</CHEAPSODA>
Only pass the values of
$soda, $joe, $joePrice to
the RETURN clause if the
string inside the PRICE
object $joePrice is < 1. 60
2/10/05
Find that PRICE subobject
of the Joe’s Rest object that
represents whatever soda is
currently $soda.
Salman Azhar: Database Systems
30
Summary
Two query language to search in XML
documents
XPATH
XQUERY
2/10/05
Queries are really path descriptors
SQL like language for querying XML documents
Salman Azhar: Database Systems
31