93
CS561 XML & XML Query 1 XML & XML Query Ling Wang Luping Ding

CS561XML & XML Query 1 Ling Wang Luping Ding. CS561XML & XML Query 2 The Web opens a new challenges in: - information technology - database framework

  • View
    238

  • Download
    0

Embed Size (px)

Citation preview

CS561 XML & XML Query 1

XML & XML Query

Ling WangLuping Ding

CS561 XML & XML Query 2

• The Web opens a new challenges in: - information technology - database framework.

• Why? - Data sources on the Web do NOT typically conform to any well-known structure. - Traditional databases technology is not adequate in dealing with rich data:

eg: audio, video, nested data structures …

Introduction

CS561 XML & XML Query 3

Web data characteristics, called semistructured :

•Object-likea collection of complex objects from CODM.

• Schema-lessNot typically conform to any type traditional structure.

• Self-describing meaning of the data is carried along with the data itself.

So, we need new database technologies to support those Web-based applications.

Features of Web Data

CS561 XML & XML Query 4

• XML---- Extensible Markup Language

- A mark up language for documents containing structured information. - Universal format for structured documents and data on the Web. - An HTML-like language.

• XML specification defines a standard way to add markup to documents.

• Note: Structured information , Markup language

What is XML?

CS561 XML & XML Query 5

A XML example for customer information:

<customer-details id="AcPharm39156"> <name>Acme Pharmaceuticals Co.</name> <address country="US"> <street>7301 Smokey Boulevard</street>

<city>Smallville</city> <state>Indiana</state> <postal>94571</postal>

</address> </customer-details>

What is XML ---- example

CS561 XML & XML Query 6

XML vs. HTML?

XML HTML

XML is extensible

- NOT specifies semantics or tag set

- Just facility

Not extensible

- Fix tag semantics and tag set

- Defined by W3C(the World Wide Web Consortium).

XML document is well formed:

- A root element.

- Opening tag is followed by a matching closing tag.

- Element properly nested.

Not strict required.

- Tags are not required to be closed.

- Browsers will forgive

etc.

CS561 XML & XML Query 7

Overview of XML

• Mechanisms for specifying document structure: ---- a set of rules for structuring an XML document.

DTD ---- Document type definition language (A part of XML standard )

XML Schema ---- A more recent specification  • Query languages for XML: XPath , XSLT, XQuery

CS561 XML & XML Query 8

• XML element Any properly nested piece of text of the form <sometag>…</sometag>.

eg: <street>7301 Smokey Boulevard</street>

• XML Attributes also a tools for datapresentation.

eg: <customer-details id="AcPharm39156"> </customer-details>

Basic concept in XML ---- element & attributes

name content

name Attribute Value

CS561 XML & XML Query 9

• Namespaces • - Why?

Element names in XML are not fixed, name conflict. - How? Different authors use different namespace identifiers for different domains. The general structure “namespace:local-name”

Namespace ---- URI (uniform resource identifier): URL (uniform resource locator) or URN (universal resource name). Local name ---- same form as regular XML tags.

No a “:” in it.

Basic concept in XML ---- namespace

CS561 XML & XML Query 10

• An example of Namespaces :

<item xmlns="http://www.acmeinc.com/jp#supplies">xmlns:toy=“http://www.acmeinc.com/jp#toys”> <name>African Coffee Table</name> <feature>

<toy:item><toy:name>cyberpet</toy:name>

</toy:item> </feature>

</item>

Basic concept in XML ---- namespace

default namespace

CS561 XML & XML Query 11

• Why DTD? - XML files carry a description of its own format with it. - Independent groups of people can agree with interchanging data. - Application verify received data from the outside world - Also verify own data.

• How? - DTD is included in your XML source file <!DOCTYPE root-element [element-declarations]> - DTD is external to your XML source file <!DOCTYPE root-element SYSTEM "filename">

DTD ---- Document Type Definitions

CS561 XML & XML Query 12

Example XML document with a DTD:

<?xml version="1.0"?><!DOCTYPE note [ <!ELEMENT note (to,from,heading,body)> <!ELEMENT to (#PCDATA)> <!ELEMENT from (#PCDATA)> <!ELEMENT heading (#PCDATA)> <!ELEMENT body (#PCDATA)>]><note> <to>Tove</to> <from>Jani</from> <heading>Reminder</heading> <body>Don't forget me this weekend</body></note>

DTD ---- example

CS561 XML & XML Query 13

XML document with an external DTD:

<?xml version="1.0"?><!DOCTYPE note SYSTEM "note.dtd"><note><to>Tove</to><from>Jani</from><heading>Reminder</heading><body>Don't forget me this weekend!</body></note>

"note.dtd" containing the DTD:

<!ELEMENT note (to,from,heading,body)><!ELEMENT to (#PCDATA)><!ELEMENT from (#PCDATA)><!ELEMENT heading (#PCDATA)><!ELEMENT body (#PCDATA)>

DTD ---- example

CS561 XML & XML Query 14

• Inadequacy of DTD:

- Not designed with namespaces.. - Use syntax ---- quite different from XML document. - A very limited set of basic types - Provide only limited means for expressing data consistency constraints. No keys

Referential integrity is weak:Attributes can be type ID, IDREF,

IDREFS. No for element.

DTD ---- Inadequacy

CS561 XML & XML Query 15

• Inadequacy of DTD:

- No ways of enforcing referential integrity for elements.

- Use alternatives to state that the order of elements is immaterial. Terrible as the number of attributes grows.

- Element definitions are global to the entire document.

DTD ---- Inadequacy

CS561 XML & XML Query 16

• XML Schemas An attempt to solve all those problems in DTD

- Powerful data typing - Range checking - Namespace-aware validation based on namespace URIs rather than on prefixes - Extensibility and scalability

XML Schema

CS561 XML & XML Query 17

• Here is a simple example about XML Schema:

<?xml version="1.0"?><xsd:schema

xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <xsd:element name="SONG" type="SongType"/> <xsd:complexType name="SongType"> <xsd:sequence> <xsd:element name="TITLE" type="xsd:string"/> <xsd:element name="COMPOSER" type="xsd:string"/> <xsd:element name="PRODUCER" type="xsd:string"/> <xsd:element name="PUBLISHER" type="xsd:string"/> <xsd:element name="LENGTH" type="xsd:string"/> <xsd:element name="YEAR" type="xsd:string"/> <xsd:element name="ARTIST" type="xsd:string"/> <xsd:element name="PRICE" type="xsd:string"/> </xsd:sequence> </xsd:complexType></xsd:schema>

XML Schema ---- example

CS561 XML & XML Query 18

• The root element ---- “schema”.• Default namespace ---- http://www.w3.org/2001/XMLSchema with prefix xsd or xs.

• Elements ---- xsd:element. divided into simple type and complex type.

simple type element is one that can only contain text and does not have any attributes. It cannot contain any child elements.

Syntax: <xs:element name="name" type="type"/>Examples: <xs:element name="to"

type="xs:string"/>

XML Schema ---- example

CS561 XML & XML Query 19

Complex type define a new type which can have attributes and can have child elements. This is very flexible.

Syntax: <xs:element name="name"> <xs:complexType>

. element content </xs:complexType></xs:element>

Example: <xs:element name="note"> <xs:complexType> <xs:sequence>

<element name="to" type="xs:string"/><element name="from" type="xs:string"/><element name="heading" type="xs:string"/><element name="body" type="xs:string"/>

</xs:sequence> </xs:complexType></xs:element>

XML Schema ---- example

CS561 XML & XML Query 20

• Simple Types

- 44 built-in simple types in the W3C XML Schema language. - Divided into seven groups:

Numeric types Time types XML types String types The boolean type The URI reference type The binary types

XML Schema ---- features

CS561 XML & XML Query 21

• Deriving Simple Types Not limited to the 44 simple types Create new data types by deriving from the

existing types -restrict a type to a subset of its normal values.

eg: A schema that derives a Str255 data type from xsd:string

<xsd:simpleType name="Str255"> <xsd:restriction base="xsd:string"> <xsd:minLength value="1"/> <xsd:maxLength value="255"/> </xsd:restriction> </xsd:simpleType>

XML Schema ---- features

CS561 XML & XML Query 22

• create enumerated types

Example:

<xsd:simpleType name="PublisherType"> <xsd:restriction base="xsd:string"> <xsd:enumeration value="Warner-Elektra-Atlantic"/> <xsd:enumeration value="Universal Music Group"/> <xsd:enumeration value="Sony Music

Entertainment,Inc."/> <xsd:enumeration value="Capitol Records, Inc."/> <xsd:enumeration value="BMG Music"/> </xsd:restriction> </xsd:simpleType>

XML Schema ---- features

CS561 XML & XML Query 23

• create new types by join existing types through a union.

Example:

<xsd:simpleType name="MoneyOrDecimal"> <xsd:union> <xsd:simpleType> <xsd:restriction base="xsd:decimal"> </xsd:restriction> </xsd:simpleType> <xsd:simpleType> <xsd:restriction base="xsd:string"> <xsd:pattern value="\p{Sc}\p{Nd}+(\.\p{Nd}\

p{Nd})?"/> </xsd:restriction> </xsd:simpleType> </xsd:union></xsd:simpleType>

XML Schema ---- features

CS561 XML & XML Query 24

• Namespaces

- http://www.w3.org/2001/XMLSchema the namespace that identifies the names of tags and attributes used in a schema. The name is understood by all schema aware XML processors.- http://www.w3.org/2001/XMLSchema-instance a small number of special names used in instance documents, not schema.- target namespace the set of names defined by a particular schema document the user-defined names that are to be used in the instance documents.

XML Schema ---- features

CS561 XML & XML Query 25

• Grouping- Does order really mattered? ?

- How? xsd:all group ---- each element in the group

must occur at most once, but that order is not important. xsd:choice group ---- any one element from the

group should appear. xsd:sequence group ---- each element in the

group appear exactly once, in the specified order.

XML Schema ---- features

CS561 XML & XML Query 26

Example for xsd:all group

<xsd:complexType name="PersonType"> <xsd:sequence> <xsd:element name="NAME"> <xsd:complexType> <xsd:all> <xsd:element name="GIVEN" type="xsd:string" minOccurs="1" maxOccurs="1"/> <xsd:element name="FAMILY" type="xsd:string" minOccurs="1" maxOccurs="1"/> </xsd:all> </xsd:complexType> </xsd:element> </xsd:sequence></xsd:complexType>

XML Schema ---- features

CS561 XML & XML Query 27

Example for XML Choice group:

<xsd:complexType name="SongType"> <xsd:sequence> <xsd:element name="TITLE" type="xsd:string"/> <xsd:choice> <xsd:element name="COMPOSER" type="PersonType"/> <xsd:element name="PRODUCER" type="PersonType"/> </xsd:choice> <xsd:element name="PUBLISHER" type="xsd:string" minOccurs="0"/> <xsd:element name="LENGTH" type="xsd:string"/> <xsd:element name="YEAR" type="xsd:string"/> <xsd:element name="ARTIST" type="xsd:string" maxOccurs="unbounded"/> <xsd:element name="PRICE" type="xsd:string"

minOccurs="0"/> </xsd:sequence></xsd:complexType>

XML Schema ---- features

CS561 XML & XML Query 28

• Schemas address limitations of DTDs: a strange, non-XML syntaxnamespace incompatibilitylack of data typinglimited extensibility and scalability.

• XML Schemas - Powerful data typing - Range checking - Namespace-aware validation based on namespace URIs rather than on prefixes - Extensibility and scalability

XML Schema ---- features

CS561 XML & XML Query 29

• DTD No keys, its Referential integrity is weak

Attributes :ID, IDREF, IDREFS. ID ---- Unique valueIDREF ---- Valid ID declared in same document IDREFS ---- Valid ID, space-separated

But these are also based on type string.

Element: no corresponding parts.

XML Constrains ---- DTD

CS561 XML & XML Query 30

XML Constrains ---- Schema

• XML keys: Similar with SQL, but complicated.

- complex structures- a key might be composed of a sequence of values- located at different depths inside an element.

Two ways:  - tag unique ---- UNIQUE constraint- tag key ---- PRIMARY KEY , not null

eg: <key name=“PrimaryKeyForClass”><selector xpath=“Classes/Class”/><field xpath=“CrsCode”/><field xpath=“Semester”/>

</key>

CS561 XML & XML Query 31

XML Constrains ---- Schema

• Foreign keys:

eg: <complexType> …… <keyref name=“NoBogusTranscripts” refer=“adm:PrimaryKeyForClass”>

<selector xpath=“Students/Student/CrsTaken”/><field xpath=“@CrsCode”/><field xpath=“@Semester”/>

</keyref> … … </complexType>

• Powerful?

CS561 XML & XML Query 32

•Is XML data model relational or object-relational?• Is XML a database?

Question

CS561 XML & XML Query 33

References

[1] Chapter 17, XML and Web Data

[2] Chapter 24, XML Bible (2nd edition): Schemas

http://www.ibiblio.org/xml/books/bible2/index.html#toc

[3] http://www.w3schools.com

http://www.w3.org/

http://www.xml.com/

CS561 XML & XML Query 34

Part II XML Query Language

Counterpart of SQL in XML World

CS561 XML & XML Query 35

XML Query Language Desired Characteristics for XML Query

Language - also Requirements Good candidate: XQuery Language Use Cases for XQuery Language

CS561 XML & XML Query 36

Desired Characteristics XML Output Declarative - what has to be done? Query Operation No Schema Required Preserve Order and Association Mutually Embedding with XML Support for New Datatypes Suitable for Metadata Ability to add update capabilities in future versions

CS561 XML & XML Query 37

Details XML Output

define derived database (virtual views) provide transparency to application (why?)

The XML Query Language MUST be declarative - like SQL specifies what has to be done it MUST not enforce a particular evaluation

strategy

CS561 XML & XML Query 38

Details (cont.) Query Operation

Projection, selection, join, and restructuring should all be possible in a single XML Query (why?)

for optimization reason

CS561 XML & XML Query 39

Query Operations

XML QUERY

Details Relational Algebra

Projection Extract particular sub-elements or attributes of an element

Projection

Selection Select values that satisfy some predicate Selection

Join Join values from one or more documents Join

Restructuring Constructing a new set of element instances to hold queried data

Create view

CS561 XML & XML Query 40

Example - Sample Data <bib> <book year="1999" isbn="1-55860-622-X"> <title>Data on the Web</title> <author>Abiteboul</author> <author>Buneman</author> <author>Suciu</author> </book> <book year="2001" isbn="1-XXXXX-YYY-Z"> <title>XML Query</title> <author>Fernandez</author> <author>Suciu</author> </book> </bib>

CS561 XML & XML Query 41

Example - XML Schema <xs:group name="Bib"> <xs:element name="bib"> <xs:complexType> <xs:group ref="Book" minOccurs="0" maxOccurs="unbounded"/> </xs:complexType> </xs:element> </xs:group>

CS561 XML & XML Query 42

Example - XML Schema (Cont.) <xs:group name="Book"> <xs:element name="book"> <xs:complexType> <xs:attribute name="year" type="xs:integer"/> <xs:attribute name="isbn" type="xs:string"/> <xs:element name="title" type="xs:string"/> <xs:element name="author"type="xs:string"

maxOccurs="unbounded"/> </xs:complexType> </xs:element> </xs:group>

CS561 XML & XML Query 43

Variable Binding LET $bib0 := <bib> <book year="1999" isbn="1-55860-622-X"> <title>Data on the Web</title> <author>Abiteboul</author> <author>Buneman</author> <author>Suciu</author> </book> <book year="2001" isbn="1-XXXXX-YYY-Z"> <title>XML Query</title> <author>Fernandez</author> <author>Suciu</author> </book>), </bib>

CS561 XML & XML Query 44

Projection $bib0/book/author

==> <author>Abiteboul</author>, <author>Buneman</author>, <author>Suciu</author>, <author>Fernandez</author>, <author>Suciu</author>

Notes: the document order of author elements is preserved

CS561 XML & XML Query 45

Selection FOR $b IN $bib0/book WHERE $b/@year/data() <= 2000 RETURN $b

==> <book year="1999" isbn="1-55860-622-X"> <title>Data on the Web</title> <author>Abiteboul</author> <author>Buneman</author> <author>Suciu</author> </book>

CS561 XML & XML Query 46

Join - Sample Data LET $review0 := <reviews> <book> <title>XML Query</title> <review>A darn fine book.</review> </book>, <book> <title>Data on the Web</title> <review>This is great!</review> </book> </review> : Reviews

CS561 XML & XML Query 47

Join FOR $b IN $bib0/book, $r IN $review0/book WHERE $b/title/data() = $r/title/data() RETURN <book>{ $b/title, $b/author, $r/review }</book> ==> <book> <title>Data on the Web</title> <author>Abiteboul</author> <author>Buneman</author> <author>Suciu</author> <review>A darn fine book.</review> </book>, <book> <title>XML Query</title> <author>Fernandez</author> <author>Suciu</author> <review>This is great!</review> </book>

CS561 XML & XML Query 48

Restructuring FOR $a IN distinct-value($bib0/book/author/data())

RETURN <biblio> <author>{ $a }</author> { FOR $b IN $bib0/book, $a2 IN $b/author/data() WHERE $a = $a2 RETURN $b/title } </biblio>

CS561 XML & XML Query 49

Restructuring (Cont.) ==> <biblio> <author>Abiteboul</author> <title>Data on the Web</title> </biblio>, <biblio> <author>Buneman</author> <title>Data on the Web</title> </biblio>, <biblio> <author>Suciu</author> <title>Data on the Web</title> <title>XML Query</title> </biblio>, <biblio> <author>Fernandez</author> <title>XML Query</title> </biblio>

CS561 XML & XML Query 50

Details (cont.) No Schema Required

XML Query should be usable on XML data when there is no schema (DTD or XML Schema) known in advance. But it should be able to exploit the schema if the schema is available.

Preserve Order and Association XML Query should preserve order and

association of elements in XML data (why?)

CS561 XML & XML Query 51

Details (cont.) Mutually Embedding with XML

An XML Query should be able to contain arbitrary XML data, and an XML document should be able to hold arbitrary XML Queries

Support for New Datatypes XML Query should have an extension

mechanism for conditions and operations specific to a particular datatypes (e.g. multimedia data).

CS561 XML & XML Query 52

Details (cont.) Suitable for Metadata

XML Query should be useful as a part of metadata descriptions (how?)

Question: how about metadata in relational database?

The current version MUST not preclude the ability to add update capabilities in future versions

CS561 XML & XML Query 53

Your Idea? Any other characteristics you desire?

CS561 XML & XML Query 54

XQuery Language Overview XPath XQuery 1.0 Semantics Future work for XQuery

CS561 XML & XML Query 55

Overview Combine the best features of XPath, SQL

and ideas borrowed from object query language.

CS561 XML & XML Query 56

XPath Language for navigation with tree-

structured documents XPath data model

XML document Tree Element Attribute Node text comment

CS561 XML & XML Query 57

Navigation in XPath Operators

Root: / Parent: .. Child (descendant): / or // Attribute value: @ Comment: comment() function Text: text() function Element: <element name>

Wildcards *: all e-children of a node irrespective of type, not

including text nodes @*: all attributes //: all descendants of current node

CS561 XML & XML Query 58

XPath expression Combination of XPath operators Input: a document tree Output: a set of nodes Absolute path expression

start from the root node Relative path expression

start from the current node

CS561 XML & XML Query 59

XPath query Selection conditions Built-in functions Aggregate functions

CS561 XML & XML Query 60

Example – XML file

<students> <student studid=“996341111”> <name><first>John</first><last>Doe</last></name> <status>U2</status> <crstaken crscode=“CS503” semester=“S2002”/> <crstaken crscode=“CS561” semester=“S2002”/> </student> <student studid=“996342222”> <name><first>Bart</first><last>Simpson</last></name> <status>U4</status> <crstaken crscode=“CS504” semester=“S2002”/> </student> </students>

CS561 XML & XML Query 61

XPath Document Tree

root

comment commentstudents

name

student

studid

crscode semestercrscodesemester

student

status

crstaken

crstaken

first

last

John Doe U2

CS561 XML & XML Query 62

Example – XPath Query

//student[status=“U2” and start-with(.//last, “D”) and not (.//last=.//first)]

//student[count(crstaken) &gt;=5]

//student[crstaken/@crscode=“CS561”] [crstaken/@semester=“S2002”]

CS561 XML & XML Query 63

Why XPath is not satisfying? Just for navigating, can only support limited

queries Cannot express join Cannot work on multiple XML documents Cannot filter unwanted elements Not support user-defined functions Not support importation and use of the types defined

in various XML schemas

Any other limitations you can think of?

CS561 XML & XML Query 64

A better candidate for XML Query? XQuery Language incorporates all the

above characteristics Any other characteristics you can think

of?

XQuery engine: Kweelt http://kweelt.sourceforge.net/

CS561 XML & XML Query 65

XQuery expressions – Path expressions – FLWR expressions – Element constructors – Expressions involving operators and

functions – Conditional expressions – Quantified expressions – List constructors – Expressions that test or modify datatypes

CS561 XML & XML Query 66

XQuery FLWR Expressions A FLWR expression binds some

expressions, applies a predicate, and constructs a new result.

FOR var IN expr …. LET var := expr …. WHERE expr …. RETURN expr ….

FOR and LET clauses grnerate a list of tuples of bound exprs,

preserving document order

WHERE clause applies a predicate, eliminating some of the tuples

RETURN clause is executing for each surviving tuple, generating

an ordered list of outputs

CS561 XML & XML Query 67

Example - DTD <!ELEMENT reviews (entry*)> <!ELEMENT entry (title, price, review)> <!ELEMENT title (#PCDATA)> <!ELEMENT price (#PCDATA)> <!ELEMENT review (#PCDATA)>

CS561 XML & XML Query 68

Example – Sample Data http://www.amazon.com/reviews.xml <reviews> <entry> <title>Data on the Web</title> <price>34.95</price> <review> a good discussion of database systems and XML. </review> </entry> <entry> <title>Advanced Unix Programming </title> <price>65.95</price> <review> a good discussion of UNIX programming. </review> </entry> </reviews>

CS561 XML & XML Query 69

Example - Request For each book found at both www.bn.com

and www.amazon.com, list the title of the book and its price from each source

CS561 XML & XML Query 70

Example - Query <books-with-prices> { for $b in document("www.bn.com/bib.xml")//book, $a in document("www.amazon.com/reviews.xml")//entry where $b/title = $a/title return <book-with-prices> { $b/title } <price-amazon>{ $a/price/data() }</price-amazon> <price-bn>{ $b/price/data() }</price-bn> </book-with-prices> } </books-with-prices>

CS561 XML & XML Query 71

Example - Result <books-with-prices> <book-with-prices> <title>Advanced Unix Programming</title> <price-amazon>65.95</price-amazon> <price-bn>65.95</price-bn> </book-with-prices> <book-with-prices> <title>Data on the Web</title> <price-amazon>34.95</price-amazon> <price-bn> 39.95</price-bn> </book-with-prices> </books-with-prices>

CS561 XML & XML Query 72

Use Cases Use Case 1: Queries that reserve hierarchy Use Case 2: Access to relational data

CS561 XML & XML Query 73

Use Case 1: Queries that reserve hierarchy

XML document has flexible structure Text is mixed with elements Many elements are optional Wide variation in structure from one

document to another The ways in which elements are ordered

and nested are quite important (Can you give me an example?)

CS561 XML & XML Query 74

Use Case 1 - DTD <!DOCTYPE book [

<!ELEMENT book (title, author+, section+)> <!ELEMENT title (#PCDATA)> <!ELEMENT author (#PCDATA)> <!ELEMENT section (title, (p | figure |

section)* )> <!ATTLIST section

id ID #IMPLIED difficulty CDATA #IMPLIED>

<!ELEMENT p (#PCDATA)> <!ELEMENT figure (title, image)> <!ATTLIST figure

width CDATA #REQUIRED height CDATA #REQUIRED >

<!ELEMENT image EMPTY> <!ATTLIST image

source CDATA #REQUIRED > ]>

CS561 XML & XML Query 75

<book> <title>Data on the Web</title> <author>Serge Abiteboul</author> <author>Peter Buneman</author> <section id="intro" difficulty="easy" >

<title>Introduction</title> <p>Text ... </p> <section>

<title>Audience</title> <p>Text ... </p>

</section> <section>

<title>Web Data and the Two Cultures</title> <p>Text ... </p> <figure height="400" width="400">

<title>Traditional client/server architecture</title> <image source="csarch.gif"/>

</figure> <p>Text ... </p>

</section> </section> <section id="syntax" difficulty="medium" >

... </section>

</book>

CS561 XML & XML Query 76

Use Case 1 - Request List all the sections and their titles.

Preserve the original attributes of each <section> element, if any.

Questions Do we need all the elements? How could we eliminate unwanted

elements? How could we preserve the original

attributes?

CS561 XML & XML Query 77

Use Case 1 - Solution <toc>

{Let $b := document(“book1.xml”)Return

Filter($b//section | $b//section/title | $b//section/title/data())

} </toc>

CS561 XML & XML Query 78

Use Case 1 - Result <toc>

<section id="intro" difficulty="easy"> <title>Introduction</title> <section>

<title>Audience</title> </section> <section>

<title>Web Data and the Two Cultures</title> </section>

</section> <section id="syntax" difficulty="medium">

... </section>

</toc>

CS561 XML & XML Query 79

Use Case 2 - Access to Relational Data Questions

How to represent relational tables as XML document?

Do we need multiple XML documents? How does XQuery work on multiple XML

documents?

CS561 XML & XML Query 80

Use Case 2 - Access to Relational Data

Represent database table as XML document Document element <-> table Tuple <-> nested element Column <-> nested element inside tuple-

element Column that allow null values are represented by

optional elements, and a missing element denotes a null value

CS561 XML & XML Query 81

Use Cases 2 - Online Auction Tables

USERS (userid, name, rating) Contains info on registered users

ITEMS (itemno, description, offered_by, start_date, end_date, reserve_price) Lists items currently or recently for sale

BIDS (userid, itemno, bid, bid_date) Contains all bids on record

CS561 XML & XML Query 82

Simplified E-R Diagram

USERS ITEMS

BIDS

userid

userid itemno

itemno

CS561 XML & XML Query 83

Use Case 2 - DTD <!DOCTYPE users [ <!ELEMENT users (user_tuple*)> <!ELEMENT user_tuple (userid, name,

rating?)> <!ELEMENT userid (#PCDATA)> <!ELEMENT name (#PCDATA)> <!ELEMENT rating (#PCDATA)> ]>

CS561 XML & XML Query 84

Use Case 2 - DTD

<!DOCTYPE items [ <!ELEMENT items (item_tuple*)> <!ELEMENT item_tuple (itemno, description,

offered_by, start_date?, end_date?, reserve_price? )> <!ELEMENT itemno (#PCDATA)> <!ELEMENT description (#PCDATA)> <!ELEMENT offered_by (#PCDATA)> <!ELEMENT start_date (#PCDATA)> <!ELEMENT end_date (#PCDATA)> <!ELEMENT reserve_price (#PCDATA)> ]>

CS561 XML & XML Query 85

Use Case 2 - DTD <!DOCTYPE bids [ <!ELEMENT bids (bid_tuple*)> <!ELEMENT bid_tuple (userid, itemno, bid, bid_date)> <!ELEMENT userid (#PCDATA)> <!ELEMENT itemno (#PCDATA)> <!ELEMENT bid (#PCDATA)> <!ELEMENT bid_date (#PCDATA)>

]>

CS561 XML & XML Query 86

Use Case 2 - Sample Data USER

USERID NAME RATING

U01 Tom Jones B

U02 Mary Doe A

U04 Roger Smith C

U05 Rip Sprat B

CS561 XML & XML Query 87

Use Case 2 - Sample Data ITEMS

ITEMNO DESCRIPTION OFFERED_BY START_DATE

END_DATE RESERVE_

PRICE

1001 Red Bicycle

U01 01-01-05 01-01-20 40

1002 Motorcycle U02 01-02-11 01-03-15 500

1003 Old Bicycle

U02 01-01-10 01-02-20 25

CS561 XML & XML Query 88

Use Case 2 – Sample Data BIDS

USERID ITEMNO BID BID_DATE

U02 1001 35 01-01-07

U04 1001 40 01-01-08

U02 1001 45 01-01-11

U04 1001 55 01-01-15

U01 1002 400 01-02-14

U02 1002 600 01-02-16

U04 1002 1000 01-02-25

U02 1002 1200 01-03-02

U04 1003 15 01-01-22

U05 1003 20 01-02-03

CS561 XML & XML Query 89

Use Case 2 - Request Request

For all bicycles, list the item number, description, and highest bid (if any), ordered by item number.

CS561 XML & XML Query 90

Use Case 2 – Solution <result> { for $i in document("items.xml")//item_tuple let $b := document("bids.xml")//bid_tuple[itemno =

$i/itemno] where contains($i/description, "Bicycle") return <item_tuple> { $i/itemno } { $i/description } <high_bid>{ max($b/bid) }</high_bid> </item_tuple> sortby(itemno) } </result>

CS561 XML & XML Query 91

Use Case 2 – Result (Bingo!) <result> <item_tuple> <itemno>1001</itemno> <description>Red Bicycle</description> <high_bid> <bid>55</bid> </high_bid> </item_tuple> <item_tuple> <itemno>1003</itemno> <description>Old Bicycle</description> <high_bid> <bid>20</bid> </high_bid> </item_tuple> </result>

CS561 XML & XML Query 92

Future Work about XQuery Add support for new desired characteristics

What are they? Any other future work?

CS561 XML & XML Query 93

Bibliography Chapter 17, XML and Web Data XML Query Requirements

http://www.w3.org/TR/2001/WD-xmlquery-req-20010215 XML Query Use Cases, W3C Working Draft 20

December 2001 http://www.w3.org/TR/2001/WD-xmlquery-use-cases-

20011220

Database Desiderata for an XML Query Language, David Maier, Oregon Graduate Institute