30
XML QUERY LANGUAGE Prepared by Prof. Zaniolo, Hung-chih Yang, Ling-Jyh Chen Modified by Fernando Farfán

XML Query Language

  • Upload
    jewell

  • View
    74

  • Download
    2

Embed Size (px)

DESCRIPTION

XML Query Language. Prepared by Prof. Zaniolo, Hung- chih Yang, Ling- Jyh Chen Modified by Fernando Farfán. Motivation. Increasing amounts of information stored, exchanged, and presented as XML. Ability to intelligently query XML data sources. - PowerPoint PPT Presentation

Citation preview

Page 1: XML Query Language

XML QUERY LANGUAGEPrepared by Prof. Zaniolo, Hung-chih Yang, Ling-Jyh ChenModified by Fernando Farfán

Page 2: XML Query Language

Motivation Increasing amounts of information stored,

exchanged, and presented as XML. Ability to intelligently query XML data

sources. XML strength: Flexibility representing many

kinds of information from diverse sources. XML query language must retrieve and

interpret information from these diverse sources.

2

XML Query Language Tutorial

Page 3: XML Query Language

Desiderata for an XML Query Language

Expressive power Semantics Compositionality Schema Program manipulation

3

XML Query Language Tutorial

Page 4: XML Query Language

Different Query Languages for XML

XPath & XQL: path expression syntax suitable for hierarchical documents

XML-QL: binding variables and using bound variables to create new structures

SQL: SELECT-FROM-WHERE pattern for restructuring data

OQL: ODMG Quilt: accept a lot of advantages from

above XML query languages, and it’s the immediate ancestor of XQuery

4

XML Query Language Tutorial

Page 5: XML Query Language

What is XQuery

XML document /databases

XQuery

SQLRelational databases

5

XML Query Language Tutorial

Page 6: XML Query Language

What is XQuery (cont.) Designed to meet the requirements identified

by the W3C XML Query Working Group “XML Query 1.0 Requirements” “XML Query Use Cases”.

Designed to be a small, easily implementable language.

Flexible enough to query a broad spectrum of XML sources (both databases and documents).

Defines a human-readable syntax for that language.

6

XML Query Language Tutorial

Page 7: XML Query Language

What is XQuery (cont.) Expression: Basic building block. Functional language (at least claimed by

the spec.) Strongly-typed language.

7

XML Query Language Tutorial

Page 8: XML Query Language

XQuery vs. XSLTReinventing the Wheel?

XSLT is document-driven; XQuery is program driven.

XSLT is written in XML; XQuery is not. An assertion (unproven): XSLT 2.0 can do

everything XQuery can do.

8

XML Query Language Tutorial

Page 9: XML Query Language

XQuery Concepts A query in XQuery is an expression that:

Reads a number of XML documents or fragments

Returns a sequence of well-formed XML fragments

9

XML Query Language Tutorial

Page 10: XML Query Language

The Principal Forms of XQuery Expressions Primary

Literals, variables, function calls and parentheses (for control precedence).

Path Locates nodes within a tree, and returns a

sequence of distinct nodes in document order. Sequence

An ordered collection of zero or more items, where an item may be an atomic value or a node.

An item is identical to a sequence of length one containing that item. Sequences are never nested.

10

XML Query Language Tutorial

Page 11: XML Query Language

The Principal Forms of XQuery Expressions (Cont.) Arithmetic

Arithmetic operators for addition, subtraction, multiplication, division, and modulus.

Comparison Four kinds of comparisons: value, general,

node, and order comparisons. Logical

A logical expression is either an AND-expression or an OR-expression.

The value of a logical expression is always a Boolean value.

11

XML Query Language Tutorial

Page 12: XML Query Language

The Principal Forms of XQuery Expressions (Cont.) Constructor

Constructors can create XML structures within a query. There are constructors for elements, attributes, CDATA

sections, processing instructions, and comments. FLWR

Expression for iteration and for binding variables to intermediate results.

Useful for computing joins between two or more documents and for restructuring data.

Pronounced "flower", stands for the keywords FOR, LET, WHERE, and RETURN, the four clauses found in a FLWR expression.

12

XML Query Language Tutorial

Page 13: XML Query Language

The Principal Forms of XQuery Expressions (Cont.)

Sorting expressions Provides a way to control the order of items in

a sequence. Conditional expressions

Based on the keywords IF, THEN, and ELSE. Quantified expressions

support existential and universal quantification.

The value of a quantified expression is always true or false.

13

XML Query Language Tutorial

Page 14: XML Query Language

The Principal Forms of XQuery Expressions (Cont.)

Data types Runtime type checking and manipulation

Validate A validate expression validates its

argument with respect to the in-scope schema definitions, using the schema validation process described in XML Schema.

14

XML Query Language Tutorial

Page 15: XML Query Language

XQuery Example 1 Find all books with a price of $39.95XQuery:document("bib.xml")/bib/book[price = 39.95]

Result: <book year="2000">

<title>Data on the Web</title><author><last>Abiteboul</last><first>Serge</first></author><author><last>Buneman</last><first>Peter</first></author><author><last>Suciu</last><first>Dan</first></author><publisher>Morgan Kaufmann Publishers</publisher><price> 39.95</price>

</book>

15

XML Query Language Tutorial

Page 16: XML Query Language

XQuery Example 2 Find the title of all books published before 1995XQuery: document("bib.xml")/bib/book[@year < 1995]/title

Result: <title>TCP/IP Illustrated</title><title>Advanced Programming in the Unix environment</title>

16

XML Query Language Tutorial

Page 17: XML Query Language

XQuery Example 3 (For Loop) List books published by Addison-Wesley after

1991, including their year and title.XQuery:<bib> { for $b in document("bib.xml")/bib/book where $b/publisher = "Addison-Wesley" and $b/@year > 1991 return <book year="{ $b/@year }"> { $b/title } </book> }</bib>

17

XML Query Language Tutorial

Page 18: XML Query Language

XQuery Example 3 (For Loop) List books published by Addison-Wesley after

1991, including their year and title…Result: <bib>

<book year="1994">

<title>TCP/IP Illustrated</title>

</book>

<book year="1992">

<title>Advanced Programming in the Unix environment</title>

</book>

</bib>

18

XML Query Language Tutorial

Page 19: XML Query Language

XQuery Example 4 (Join) For each book found at both bn.com and amazon.com, list the title of the book and its

price from each source.XQuery:<books-with-prices>

{

for $b in document("bib.xml")//book,

$a in document("reviews.xml")//entry

where $b/title = $a/title

return

<book-with-prices>

{ $b/title }

<price-amazon>{ $a/price }</price-amazon>

<price-bn>{ $b/price }</price-bn>

</book-with-prices>

}

</books-with-prices>

19

XML Query Language Tutorial

Page 20: XML Query Language

XQuery Example 4 (Join) For each book found at both bn.com and amazon.com, list the title of the book

and its price from each source.Result: <books-with-prices> <book-with-prices> <title>TCP/IP Illustrated</title> <price-amazon><price>65.95</price></price-amazon> <price-bn><price> 65.95</price></price-bn> </book-with-prices><book-with-prices> <title>Advanced Programming in the Unix environment</title> <price-amazon><price>65.95</price></price-amazon> <price-bn><price>65.95</price></price-bn> </book-with-prices><book-with-prices> <title>Data on the Web</title> <price-amazon><price>34.95</price></price-amazon> <price-bn><price> 39.95</price></price-bn> </book-with-prices></books-with-prices>

20

XML Query Language Tutorial

Page 21: XML Query Language

XQuery Example 5 (Grouping + quantifier) For each author in the bibliography, list the author's name

and the titles of all books by that author, grouped inside a "result" element.

XQuery:<results>

{

for $a in distinct-values(document("bib.com")//author)

return <result>

{ $a }

{

for $b in document("http://bib.com")/bib/book

where some $ba in $b/author satisfies deep-equal($ba,$a)

return $b/title

}

</result>

}

</results>

21

XML Query Language Tutorial

Page 22: XML Query Language

XQuery Example 5 (Grouping + quantifier) For each author in the bibliography, list the author's name and

the titles of all books by that author, grouped inside a "result" element.

Result: <results> <result> <author> <last>Stevens</last> <first>W.</first> </author> <title>TCP/IP Illustrated</title> <title>Advanced Programming in the Unix environment</title> </result> <result> <author> <last>Abiteboul</last> <first>Serge</first> </author> <title>Data on the Web</title> </result> ……</results>

22

XML Query Language Tutorial

Page 23: XML Query Language

XQuery Example 6 (Sorting) List the titles and years of all books published by Addison-

Wesley after 1991, in alphabetic order.XQuery: <bib> { for $b in document("www.bn.com/bib.xml")//book where $b/publisher = "Addison-Wesley" and $b/@year > 1991 return <book> { $b/@year } { $b/title } </book> sortby (title) }</bib>

23

XML Query Language Tutorial

Page 24: XML Query Language

XQuery Example 6 (Sorting) List the titles and years of all books published by Addison-

Wesley after 1991, in alphabetic order.Result: <bib>

<book year="1992">

<title>Advanced Programming in the Unix environment</title>

</book>

<book year="1994">

<title>TCP/IP Illustrated</title>

</book>

</bib>

24

XML Query Language Tutorial

Page 25: XML Query Language

XQuery Example 7 (Recursion) Convert the sample document from "partlist" format to "parttree" format.XQuery: define function one_level (element $p) returns element

{

<part partid="{ $p/@partid }" name="{ $p/@name }" >

{

for $s in document("partlist.xml")//part

where $s/@partof = $p/@partid

return one_level($s)

}

</part>

}

<parttree>

{

for $p in document("partlist.xml")//part[empty(@partof)]

return one_level($p)

}

</parttree>

25

XML Query Language Tutorial

Page 26: XML Query Language

XQuery Example 7 (Recursion) Convert the sample document from "partlist" format to "parttree" format.Result: <parttree>

<part partid="0" name="car">

<part partid="1" name="engine">

<part partid="3" name="piston"/>

</part>

<part partid="2" name="door">

<part partid="4" name="window"/>

<part partid="5" name="lock"/>

</part>

</part>

<part partid="10" name="skateboard">

<part partid="11" name="board"/>

<part partid="12" name="wheel"/>

</part>

<part partid="20" name="canoe"/>

</parttree>

26

XML Query Language Tutorial

Page 27: XML Query Language

XQuery Example 8 (Sequence) In the Procedure section of Report1, what Instruments were used

in the second Incision?XQuery: for $s in document("report1.xml")//section[section.title = "Procedure"]

return ($s//incision)[2]/instrument

Result: <instrument>electrocautery</instrument>

27

XML Query Language Tutorial

Page 28: XML Query Language

XQuery Support on RDBMSs

XML Query Language Tutorial

28

Oracle XQuery Engine http://www.oracle.com/technology/tech/xml/xquery/

index.html Introduction to XQuery in SQL Server 2005

http://msdn.microsoft.com/en-us/library/ms345122(SQL.90).aspx

Query DB2 XML data with XQuery http://www.ibm.com/developerworks/data/library/

techarticle/dm-0604saracco/ DataDirect: Data Integration Suite – MySQL Database

Support http://www.datadirect.com/products/data-integration/

datasources/databases/mysql/index.ssp

Page 29: XML Query Language

Conclusion XQuery is a simple substitution of XSLT,

JSP, ASP, Servlet, CGI, PHP, etc. XQuery programs can accomplish most

tasks of other tools aforementioned, and yet is much simpler to learn and easier to write.

Possible direction is to extend XQuery for UPDATE and INSERT to an XML database

Still lack of support from industry till now

29

XML Query Language Tutorial

Page 30: XML Query Language

References Jonathan Pinnock, et al. “Professional XML, 2nd edition”,

ISBN: 1861005059, WROX Publishers, 2001 Serge Abiteboul, Peter Buneman and Dan Suciu, “Data on the

Web: from Relations to Semistructured Data and XML”, ISBN 1-55860-622-X, Morgan Kaufmann Publishers, 2000

World Wide Web Consortium, “XQuery 1.0. An XML Query Language”, W3C Working Draft, Apr. 30, 2002

World Wide Web Consortium, “XML Path Language (XPath) Version 1.0”, W3C Recommendation, Nov. 16, 1999

Qexo: The GNU Kawa implementation of XQuery, http://www.gnu.org/software/qexo/

Don Chamberlin, Jonathan Robie, and Daniela Florescu, “Quilt: An XML Query Language for Heterogeneous Data Sources”, WebDB 2000, Dallas, May 2000

30

XML Query Language Tutorial