module 1: introductionhomepages.cwi.nl/~manegold/teaching/adt/lectures/adt2010_xml_… · 2...

14
ADT 2010 ADT 2010 Introduction to Introduction to (XML, XPath &) (XML, XPath &) XQuery XQuery Chapter 10 in Chapter 10 in Silberschatz, Korth, Sudarshan Silberschatz, Korth, Sudarshan “Database System Concepts” “Database System Concepts” Stefan Manegold [email protected] http://www.cwi.nl/~manegold/

Upload: others

Post on 26-Jul-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Module 1: Introductionhomepages.cwi.nl/~manegold/teaching/adt/lectures/ADT2010_XML_… · 2 Stefan.Manegold@CWI.nl Introduction to XML, XPath & XQuery ADT 2010 why • Motivation

ADT 2010ADT 2010

Introduction toIntroduction to

(XML, XPath &)(XML, XPath &) XQuery XQuery

Chapter 10 inChapter 10 in

Silberschatz, Korth, SudarshanSilberschatz, Korth, Sudarshan

“Database System Concepts”“Database System Concepts”

Stefan [email protected]

http://www.cwi.nl/~manegold/

Page 2: Module 1: Introductionhomepages.cwi.nl/~manegold/teaching/adt/lectures/ADT2010_XML_… · 2 Stefan.Manegold@CWI.nl Introduction to XML, XPath & XQuery ADT 2010 why • Motivation

2

[email protected] Introduction to XML, XPath & XQuery ADT 2010

why• Motivation & The Big Picture: XML, DTD, XML Schema, XPath

WHAT • Crash Course XQuery

who• XML files Saxon, Galax, GNU Qexo• XML DBMS eXist, BerkeleyDB, MonetDB, X-Hive, Tamino, Xyleme• XML RDBMS Oracle10g, SQLserver 2005, DB2

how• Under The Hood of MonetDB/XQuery• Some Benchmarks

XML DatabasesXML Databases

Page 3: Module 1: Introductionhomepages.cwi.nl/~manegold/teaching/adt/lectures/ADT2010_XML_… · 2 Stefan.Manegold@CWI.nl Introduction to XML, XPath & XQuery ADT 2010 why • Motivation

3

[email protected] Introduction to XML, XPath & XQuery ADT 2010

XQueryXQuery

A full-fledged XQuery implementation is available:

⊳ MonetDB/XQuery: http://monetdb-xquery.org/

Page 4: Module 1: Introductionhomepages.cwi.nl/~manegold/teaching/adt/lectures/ADT2010_XML_… · 2 Stefan.Manegold@CWI.nl Introduction to XML, XPath & XQuery ADT 2010 why • Motivation

4

[email protected] Introduction to XML, XPath & XQuery ADT 2010

XQueryXQuery

Page 5: Module 1: Introductionhomepages.cwi.nl/~manegold/teaching/adt/lectures/ADT2010_XML_… · 2 Stefan.Manegold@CWI.nl Introduction to XML, XPath & XQuery ADT 2010 why • Motivation

5

[email protected] Introduction to XML, XPath & XQuery ADT 2010

XQuery: FLWOR ExpressionsXQuery: FLWOR Expressions

Page 6: Module 1: Introductionhomepages.cwi.nl/~manegold/teaching/adt/lectures/ADT2010_XML_… · 2 Stefan.Manegold@CWI.nl Introduction to XML, XPath & XQuery ADT 2010 why • Motivation

6

[email protected] Introduction to XML, XPath & XQuery ADT 2010

XQuery ExampleXQuery Example

Page 7: Module 1: Introductionhomepages.cwi.nl/~manegold/teaching/adt/lectures/ADT2010_XML_… · 2 Stefan.Manegold@CWI.nl Introduction to XML, XPath & XQuery ADT 2010 why • Motivation

7

[email protected] Introduction to XML, XPath & XQuery ADT 2010

XQuery ExampleXQuery Example

(1, 10) (1, 20) (2, 10) (2, 20) (3, 10) (3, 20)

Page 8: Module 1: Introductionhomepages.cwi.nl/~manegold/teaching/adt/lectures/ADT2010_XML_… · 2 Stefan.Manegold@CWI.nl Introduction to XML, XPath & XQuery ADT 2010 why • Motivation

8

[email protected] Introduction to XML, XPath & XQuery ADT 2010

XQuery: FLWOR ExpressionsXQuery: FLWOR Expressions

event

Page 9: Module 1: Introductionhomepages.cwi.nl/~manegold/teaching/adt/lectures/ADT2010_XML_… · 2 Stefan.Manegold@CWI.nl Introduction to XML, XPath & XQuery ADT 2010 why • Motivation

9

[email protected] Introduction to XML, XPath & XQuery ADT 2010

XQuery: Element ConstructionXQuery: Element Construction

Page 10: Module 1: Introductionhomepages.cwi.nl/~manegold/teaching/adt/lectures/ADT2010_XML_… · 2 Stefan.Manegold@CWI.nl Introduction to XML, XPath & XQuery ADT 2010 why • Motivation

10

[email protected] Introduction to XML, XPath & XQuery ADT 2010

JoinsJoins

■ Joins are specified in a manner very similar to SQL

for $a in /bank/account, $c in /bank/customer, $d in /bank/depositor

where $a/account_number = $d/account_number and $c/customer_name = $d/customer_name

return <cust_acct> { $c $a } </cust_acct>

■ The same query can be expressed with the selections specified as XPath selections:

for $a in /bank/account $c in /bank/customer $d in /bank/depositor[

account_number = $a/account_number and customer_name = $c/customer_name] return <cust_acct> { $c $a } </cust_acct>

Page 11: Module 1: Introductionhomepages.cwi.nl/~manegold/teaching/adt/lectures/ADT2010_XML_… · 2 Stefan.Manegold@CWI.nl Introduction to XML, XPath & XQuery ADT 2010 why • Motivation

11

[email protected] Introduction to XML, XPath & XQuery ADT 2010

Nested QueriesNested Queries■ The following query converts data from the flat structure for

bank information into the nested structure used in bank-1 <bank-1> {

for $c in /bank/customer return

<customer> { $c/* } { for $d in /bank/depositor[customer_name =

$c/customer_name], $a in

/bank/account[account_number=$d/account_number] return $a }

</customer> } </bank-1>

■ $c/* denotes all the children of the node to which $c is bound, without the enclosing top-level tag

■ $c/text() gives text content of an element without any subelements / tags

Page 12: Module 1: Introductionhomepages.cwi.nl/~manegold/teaching/adt/lectures/ADT2010_XML_… · 2 Stefan.Manegold@CWI.nl Introduction to XML, XPath & XQuery ADT 2010 why • Motivation

12

[email protected] Introduction to XML, XPath & XQuery ADT 2010

Sorting in XQuery Sorting in XQuery ■ The order by clause can be used at the end of any expression.

E.g. to return customers sorted by name for $c in /bank/customer

order by $c/customer_name

return <customer> { $c/* } </customer>

■ Use order by $c/customer_name to sort in descending order■ Can sort at multiple levels of nesting (sort by customer_name, and by

account_number within each customer) <bank-1> {

for $c in /bank/customer order by $c/customer_namereturn <customer> { $c/* } { for $d in /bank/depositor[customer_name=$c/customer_name], $a in /bank/account[account_number=$d/account_number]

order by $a/account_number return <account> { $a/* } </account> }

</customer> } </bank-1>

Page 13: Module 1: Introductionhomepages.cwi.nl/~manegold/teaching/adt/lectures/ADT2010_XML_… · 2 Stefan.Manegold@CWI.nl Introduction to XML, XPath & XQuery ADT 2010 why • Motivation

13

[email protected] Introduction to XML, XPath & XQuery ADT 2010

Functions and Other XQuery FeaturesFunctions and Other XQuery Features

■ User defined functions declare function balances($c as xs:string) as xs:decimal* { for $d in /bank/depositor[customer_name = $c], $a in /bank/account[account_number = $d/account_number] return $a/balance

}

■ Types are optional for function parameters and return values

■ The * (as in decimal*) indicates a sequence of values of that type

■ Universal and existential quantification in where clause predicates

● some $e in path satisfies P

● every $e in path satisfies P

■ XQuery also supports If-then-else clauses

Page 14: Module 1: Introductionhomepages.cwi.nl/~manegold/teaching/adt/lectures/ADT2010_XML_… · 2 Stefan.Manegold@CWI.nl Introduction to XML, XPath & XQuery ADT 2010 why • Motivation

14

[email protected] Introduction to XML, XPath & XQuery ADT 2010

Further Reading MaterialFurther Reading Material