2/10/05salman azhar: database systems1 xml query languages salman azhar xpath xquery these slides...

31
2/10/05 Salman Azhar: Database Sy stems 1 XML Query Languages Salman Azhar XPATH XQUERY These slides use some figures, definitions, and explanations from Elmasri-Navathe’s Fundamentals of Database Systems and Molina-Ullman-Widom’s Database Systems

Upload: arely-frizell

Post on 28-Mar-2015

216 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: 2/10/05Salman Azhar: Database Systems1 XML Query Languages Salman Azhar XPATH XQUERY These slides use some figures, definitions, and explanations from

2/10/05 Salman Azhar: Database Systems

1

XML Query Languages Salman Azhar

XPATHXQUERY

These slides use some figures, definitions, and explanations from Elmasri-Navathe’s Fundamentals of Database Systems

and Molina-Ullman-Widom’s Database Systems

Page 2: 2/10/05Salman Azhar: Database Systems1 XML Query Languages Salman Azhar XPATH XQUERY These slides use some figures, definitions, and explanations from

2/10/05 Salman Azhar: Database Systems

2

XPATH and XQUERY

Two query language to search in XML documents XPATH

Language for describing paths XQUERY

Language for querying XML documents

Page 3: 2/10/05Salman Azhar: Database Systems1 XML Query Languages Salman Azhar XPATH XQUERY These slides use some figures, definitions, and explanations from

2/10/05 Salman Azhar: Database Systems

3

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 semi-structured data graphs

each subobject as a node of a graph with its subobjects as its children

Page 4: 2/10/05Salman Azhar: Database Systems1 XML Query Languages Salman Azhar XPATH XQUERY These slides use some figures, definitions, and explanations from

2/10/05 Salman Azhar: Database Systems

4

XQUERY

XQUERY is a full query language for XML documents using path expressions from XPATH

XQUERY use FLWR (pronounced “flower”) expression corresponds to SQL’s select-from-where FLWR stands for “for-let-where-return”

Page 5: 2/10/05Salman Azhar: Database Systems1 XML Query Languages Salman Azhar XPATH XQUERY These slides use some figures, definitions, and explanations from

2/10/05 Salman Azhar: Database Systems

5

Example DTD

<!DOCTYPE Rests [<!ELEMENT RESTS (REST*, SODA*)><!ELEMENT REST (PRICE+)>

<!ATTLIST REST name = ID><!ELEMENT PRICE (#PCDATA)>

<!ATTLIST PRICE theSoda = IDREF><!ELEMENT SODA ()>

<!ATTLIST SODA name = ID, soldBy = IDREFS>

]>

RESTS have 0 or more REST & SODA

Every REST has 1 or more PRICE and also an attribute name

PRICE has data for price

& the Soda with that

price

SODA name for ID and IDREFs for the rest that sell the soda.

Page 6: 2/10/05Salman Azhar: Database Systems1 XML Query Languages Salman Azhar XPATH XQUERY These slides use some figures, definitions, and explanations from

2/10/05 Salman Azhar: Database Systems

6

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>

Page 7: 2/10/05Salman Azhar: Database Systems1 XML Query Languages Salman Azhar XPATH XQUERY These slides use some figures, definitions, and explanations from

2/10/05 Salman Azhar: Database Systems

7

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

sequences of tags separated by slashes (/)

Page 8: 2/10/05Salman Azhar: Database Systems1 XML Query Languages Salman Azhar XPATH XQUERY These slides use some figures, definitions, and explanations from

2/10/05 Salman Azhar: Database Systems

8

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,…”></SODA> …

</RESTS> /RESTS/REST/PRICE describesThe set with these two PRICEObjects as well as the PRICE

objects for any other restaurants.

Page 9: 2/10/05Salman Azhar: Database Systems1 XML Query Languages Salman Azhar XPATH XQUERY These slides use some figures, definitions, and explanations from

2/10/05 Salman Azhar: Database Systems

9

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

then the path can start anywhere

Page 10: 2/10/05Salman Azhar: Database Systems1 XML Query Languages Salman Azhar XPATH XQUERY These slides use some figures, definitions, and explanations from

2/10/05 Salman Azhar: Database Systems

10

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,…”></SODA> …

</RESTS> //PRICE describes the same PRICEobjects, but only because the DTD

forces every PRICE to appear withina RESTS and a REST.

Page 11: 2/10/05Salman Azhar: Database Systems1 XML Query Languages Salman Azhar XPATH XQUERY These slides use some figures, definitions, and explanations from

2/10/05 Salman Azhar: Database Systems

11

Wild-Card *

A star (*) in place of a tag represents any one tag acts as a “wildcard”

Example: /*/*/PRICE represents all price objects at the third level of

nesting

Page 12: 2/10/05Salman Azhar: Database Systems1 XML Query Languages Salman Azhar XPATH XQUERY These slides use some figures, definitions, and explanations from

2/10/05 Salman Azhar: Database Systems

12

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> /RESTS/* captures all RESTand SODA objects, such

as these.

Page 13: 2/10/05Salman Azhar: Database Systems1 XML Query Languages Salman Azhar XPATH XQUERY These slides use some figures, definitions, and explanations from

2/10/05 Salman Azhar: Database Systems

13

Attributes

We may refer to attributes in addition to tags In XPATH, we refer to attributes by

pre-pending @ to their name Attributes of a tag may appear in

paths as if they were nested within that tag

Page 14: 2/10/05Salman Azhar: Database Systems1 XML Query Languages Salman Azhar XPATH XQUERY These slides use some figures, definitions, and explanations from

2/10/05 Salman Azhar: Database Systems

14

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,…”></SODA> …

</RESTS> /RESTS/*/@name selects allname attributes of immediate

subobjects of the RESTS object.

Page 15: 2/10/05Salman Azhar: Database Systems1 XML Query Languages Salman Azhar XPATH XQUERY These slides use some figures, definitions, and explanations from

2/10/05 Salman Azhar: Database Systems

15

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 have that tag and also satisfy the condition

Page 16: 2/10/05Salman Azhar: Database Systems1 XML Query Languages Salman Azhar XPATH XQUERY These slides use some figures, definitions, and explanations from

2/10/05 Salman Azhar: Database Systems

16

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 notthe price of Slice satisfy the path

descriptor.

Page 17: 2/10/05Salman Azhar: Database Systems1 XML Query Languages Salman Azhar XPATH XQUERY These slides use some figures, definitions, and explanations from

2/10/05 Salman Azhar: Database Systems

17

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 isselected, along with any

other prices for Slice.

Page 18: 2/10/05Salman Azhar: Database Systems1 XML Query Languages Salman Azhar XPATH XQUERY These slides use some figures, definitions, and explanations from

2/10/05 Salman Azhar: Database Systems

18

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

Page 19: 2/10/05Salman Azhar: Database Systems1 XML Query Languages Salman Azhar XPATH XQUERY These slides use some figures, definitions, and explanations from

2/10/05 Salman Azhar: Database Systems

19

Optional: Example of Axes

/RESTS/SODA is really shorthand for /RESTS/child::SODA

@ is really shorthand for the attribute:: axis. Thus, /RESTS/SODA[@name = “Dew” ] is

shorthand for /RESTS/SODA[attribute::name = “Dew”]

Page 20: 2/10/05Salman Azhar: Database Systems1 XML Query Languages Salman Azhar XPATH XQUERY These slides use some figures, definitions, and explanations from

2/10/05 Salman Azhar: Database Systems

20

Optional: More Axes

Some other useful axes are:1. parent:: = parent(s) of the current

node(s)2. descendant-or-self:: = the current

node(s) and all descendants Note: // is really a shorthand for this

axis.

3. ancestor::, ancestor-or-self, etc.

Page 21: 2/10/05Salman Azhar: Database Systems1 XML Query Languages Salman Azhar XPATH XQUERY These slides use some figures, definitions, and explanations from

2/10/05 Salman Azhar: Database Systems

21

XQUERY

XQUERY allows us to query XML documents using path expressions from XPATH to

describe important sets XQUERY use FLWR (pronounced

“flower”) expressions stands for “for-let-where-return” corresponds to SQL’s select-from-where

Page 22: 2/10/05Salman Azhar: Database Systems1 XML Query Languages Salman Azhar XPATH XQUERY These slides use some figures, definitions, and explanations from

2/10/05 Salman Azhar: Database Systems

22

XQUERY SQL Mapping

where WHERE return SELECT for/let FROM

Page 23: 2/10/05Salman Azhar: Database Systems1 XML Query Languages Salman Azhar XPATH XQUERY These slides use some figures, definitions, and explanations from

2/10/05 Salman Azhar: Database Systems

23

FLWR (FLoWeR) Expressions

FLWR expressions are made up of1. One or more FOR and/or LET

clauses.2. Then an optional WHERE clause.3. A RETURN clause.

Page 24: 2/10/05Salman Azhar: Database Systems1 XML Query Languages Salman Azhar XPATH XQUERY These slides use some figures, definitions, and explanations from

2/10/05 Salman Azhar: Database Systems

24

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 executed once for each value of the variable creates a loop

Page 25: 2/10/05Salman Azhar: Database Systems1 XML Query Languages Salman Azhar XPATH XQUERY These slides use some figures, definitions, and explanations from

2/10/05 Salman Azhar: Database Systems

25

Example: FOR

FOR $soda IN /RESTS/SODA/@nameRETURN

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

Page 26: 2/10/05Salman Azhar: Database Systems1 XML Query Languages Salman Azhar XPATH XQUERY These slides use some figures, definitions, and explanations from

2/10/05 Salman Azhar: Database Systems

26

LET Clauses

LET <variable> := <path expression>,…

Value of the variable becomes the set of objects defined by the path expression

Note: LET does not cause iteration FOR does cause iteration

Page 27: 2/10/05Salman Azhar: Database Systems1 XML Query Languages Salman Azhar XPATH XQUERY These slides use some figures, definitions, and explanations from

2/10/05 Salman Azhar: Database Systems

27

Example: LET

LET $sodas := /RESTS/SODA/@nameRETURN

<SODANAMES>$sodas</SODANAMES> Returns one object with all the names

of the sodas, like: <SODANAMES>Dew, Slice,…</SODANAMES>

Page 28: 2/10/05Salman Azhar: Database Systems1 XML Query Languages Salman Azhar XPATH XQUERY These slides use some figures, definitions, and explanations from

2/10/05 Salman Azhar: Database Systems

28

Following IDREFs

XQUERY (but not XPATH) allows us to use paths that follow attributes

that are IDREFs If x denotes a set of IDREFs,

then x =>y denotes all the objects with tag y whose IDs are one of these IDREFs

Page 29: 2/10/05Salman Azhar: Database Systems1 XML Query Languages Salman Azhar XPATH XQUERY These slides use some figures, definitions, and explanations from

2/10/05 Salman Azhar: Database Systems

29

Example Find all the soda objects where the

soda is sold by Joe’s Rest for less than 1.60

Strategy:1. $soda will for-loop over all soda objects2. For each $soda,

a. let $joe be either the Joe’s-Rest object, if Joe sells the soda,

b. or the empty set of rest objects.

3. Test whether $joe sells the soda for < 1.60

Page 30: 2/10/05Salman Azhar: Database Systems1 XML Query Languages Salman Azhar XPATH XQUERY These slides use some figures, definitions, and explanations from

2/10/05 Salman Azhar: Database Systems

30

Example: The Query

FOR $soda IN /RESTS/SODALET $joe :=

$soda/@soldBy=>REST[@name=“JoesRest”] LET $joePrice :=

$joe/PRICE[@theSoda=$soda/@name]WHERE $joePrice < 1.60RETURN <CHEAPSODA>$soda</CHEAPSODA>

Attribute soldBy is of typeIDREFS. Follow each refto a REST and check if its

name is Joe’s Rest.

Find that PRICE subobjectof the Joe’s Rest object thatrepresents whatever soda is

currently $soda.

Only pass the values of$soda, $joe, $joePrice tothe RETURN clause if thestring inside the PRICE

object $joePrice is < 1. 60

Page 31: 2/10/05Salman Azhar: Database Systems1 XML Query Languages Salman Azhar XPATH XQUERY These slides use some figures, definitions, and explanations from

2/10/05 Salman Azhar: Database Systems

31

Summary

Two query language to search in XML documents XPATH

Queries are really path descriptors XQUERY

SQL like language for querying XML documents