57
XML Document Type Definitions Database Systems and Concepts, CSCI 3030U, UOIT, Course Instructor: Jarek Szlichta 1

Document Type Definitionsdata.science.uoit.ca/wp-content/uploads/2014/07/DS... · Well-Formed and Valid XML •Well-Formed XMLallows you to invent your own tags. •Valid XML conforms

  • Upload
    others

  • View
    4

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Document Type Definitionsdata.science.uoit.ca/wp-content/uploads/2014/07/DS... · Well-Formed and Valid XML •Well-Formed XMLallows you to invent your own tags. •Valid XML conforms

XML

Document Type Definitions

Database Systems and Concepts, CSCI 3030U, UOIT, Course Instructor: Jarek Szlichta 1

Page 2: Document Type Definitionsdata.science.uoit.ca/wp-content/uploads/2014/07/DS... · Well-Formed and Valid XML •Well-Formed XMLallows you to invent your own tags. •Valid XML conforms

XML

• XML stands for eXtensible Markup Language.• XML was designed to describe data.• XML has come into common use for the interchange of data over the

Internet.

Database Systems and Concepts, CSCI 3030U, UOIT, Course Instructor: Jarek Szlichta 2

Page 3: Document Type Definitionsdata.science.uoit.ca/wp-content/uploads/2014/07/DS... · Well-Formed and Valid XML •Well-Formed XMLallows you to invent your own tags. •Valid XML conforms

Well-Formed and Valid XML

• Well-Formed XML allows you to invent your own tags.• Valid XML conforms to a certain DTD (Document Type Definition).

Database Systems and Concepts, CSCI 3030U, UOIT, Course Instructor: Jarek Szlichta 3

Page 4: Document Type Definitionsdata.science.uoit.ca/wp-content/uploads/2014/07/DS... · Well-Formed and Valid XML •Well-Formed XMLallows you to invent your own tags. •Valid XML conforms

Well-Formed XML

• Start the document with a declaration, surrounded by <?xml … ?> .• Normal declaration is:<?xml version = ”1.0” standalone = ”yes” ?>• “standalone” = “no DTD provided.”

• Balance of document is a root tag surrounding nested tags.• White spaces: yes

Database Systems and Concepts, CSCI 3030U, UOIT, Course Instructor: Jarek Szlichta 4

Page 5: Document Type Definitionsdata.science.uoit.ca/wp-content/uploads/2014/07/DS... · Well-Formed and Valid XML •Well-Formed XMLallows you to invent your own tags. •Valid XML conforms

Tags

• Tags are normally matched pairs, as <FOO> … </FOO>.• Unmatched tags also allowed, as <FOO/>• Tags may be nested arbitrarily.• XML tags are case-sensitive.

Database Systems and Concepts, CSCI 3030U, UOIT, Course Instructor: Jarek Szlichta 5

Page 6: Document Type Definitionsdata.science.uoit.ca/wp-content/uploads/2014/07/DS... · Well-Formed and Valid XML •Well-Formed XMLallows you to invent your own tags. •Valid XML conforms

Example: Well-Formed XML

<?xml version = “1.0” standalone = “yes” ?><BARS><BAR><NAME>Joe’s Bar</NAME>

<BEER><NAME>Bud</NAME><PRICE>2.50</PRICE></BEER>

<BEER><NAME>Miller</NAME><PRICE>3.00</PRICE></BEER>

</BAR><BAR> …

</BARS>

Database Systems and Concepts, CSCI 3030U, UOIT, Course Instructor: Jarek Szlichta 6

A NAMEsubelement

A BEERsubelement

Root tag

Tags surroundinga BEER element

Page 7: Document Type Definitionsdata.science.uoit.ca/wp-content/uploads/2014/07/DS... · Well-Formed and Valid XML •Well-Formed XMLallows you to invent your own tags. •Valid XML conforms

DTD Structure

<!DOCTYPE <root tag> [<!ELEMENT <name>(<components>)>. . . more elements . . .

]>

Database Systems and Concepts, CSCI 3030U, UOIT, Course Instructor: Jarek Szlichta 7

Page 8: Document Type Definitionsdata.science.uoit.ca/wp-content/uploads/2014/07/DS... · Well-Formed and Valid XML •Well-Formed XMLallows you to invent your own tags. •Valid XML conforms

DTD Elements

• The description of an element consists of its name (tag), and a parenthesized description of any nested tags.• Includes order of subtags and their multiplicity.

• Leaves (text elements) have #PCDATA (Parsed Character DATA ) in place of nested tags.

Database Systems and Concepts, CSCI 3030U, UOIT, Course Instructor: Jarek Szlichta 8

Page 9: Document Type Definitionsdata.science.uoit.ca/wp-content/uploads/2014/07/DS... · Well-Formed and Valid XML •Well-Formed XMLallows you to invent your own tags. •Valid XML conforms

Example: DTD

<!DOCTYPE BARS [<!ELEMENT BARS (BAR*)><!ELEMENT BAR (NAME, BEER+)><!ELEMENT NAME (#PCDATA)><!ELEMENT BEER (NAME, PRICE)><!ELEMENT PRICE (#PCDATA)>

]>

Database Systems and Concepts, CSCI 3030U, UOIT, Course Instructor: Jarek Szlichta 9

A BARS object haszero or more BAR’snested within.

A BAR has oneNAME and oneor more BEERsubobjects.

A BEER has aNAME and aPRICE.

NAME and PRICEare text.

Page 10: Document Type Definitionsdata.science.uoit.ca/wp-content/uploads/2014/07/DS... · Well-Formed and Valid XML •Well-Formed XMLallows you to invent your own tags. •Valid XML conforms

Element Descriptions

• Subtags must appear in order shown.• A tag may be followed by a symbol to indicate its multiplicity.• * = zero or more.• + = one or more.• ? = zero or one.

• Symbol | can connect alternative sequences of tags.

Database Systems and Concepts, CSCI 3030U, UOIT, Course Instructor: Jarek Szlichta 10

Page 11: Document Type Definitionsdata.science.uoit.ca/wp-content/uploads/2014/07/DS... · Well-Formed and Valid XML •Well-Formed XMLallows you to invent your own tags. •Valid XML conforms

Example: Element Description

• A name is an optional title (e.g., “Prof.”), a first name, and a last name, in that order, or it is an IP address:

<!ELEMENT NAME ((TITLE?, FIRST, LAST) | IPADDR

)>

Database Systems and Concepts, CSCI 3030U, UOIT, Course Instructor: Jarek Szlichta 11

Page 12: Document Type Definitionsdata.science.uoit.ca/wp-content/uploads/2014/07/DS... · Well-Formed and Valid XML •Well-Formed XMLallows you to invent your own tags. •Valid XML conforms

Use of DTD’s

1. Set standalone = “no”.2. Either:

a) Include the DTD as a preamble of the XML document, orb) Follow DOCTYPE and the <root tag> by SYSTEM and a path to the file

where the DTD can be found.

Database Systems and Concepts, CSCI 3030U, UOIT, Course Instructor: Jarek Szlichta 12

Page 13: Document Type Definitionsdata.science.uoit.ca/wp-content/uploads/2014/07/DS... · Well-Formed and Valid XML •Well-Formed XMLallows you to invent your own tags. •Valid XML conforms

Example: (a)<?xml version = “1.0” standalone = “no” ?><!DOCTYPE BARS [

<!ELEMENT BARS (BAR*)><!ELEMENT BAR (NAME, BEER+)><!ELEMENT NAME (#PCDATA)><!ELEMENT BEER (NAME, PRICE)><!ELEMENT PRICE (#PCDATA)>

]><BARS>

<BAR><NAME>Joe’s Bar</NAME><BEER><NAME>Bud</NAME> <PRICE>2.50</PRICE></BEER><BEER><NAME>Miller</NAME> <PRICE>3.00</PRICE></BEER>

</BAR> <BAR> …

</BARS>

Database Systems and Concepts, CSCI 3030U, UOIT, Course Instructor: Jarek Szlichta 13

The DTD

The document

Page 14: Document Type Definitionsdata.science.uoit.ca/wp-content/uploads/2014/07/DS... · Well-Formed and Valid XML •Well-Formed XMLallows you to invent your own tags. •Valid XML conforms

Example: (b)

•Assume the BARS DTD is in file bar.dtd.<?xml version = “1.0” standalone = “no” ?><!DOCTYPE BARS SYSTEM ”bar.dtd”><BARS><BAR><NAME>Joe’s Bar</NAME>

<BEER><NAME>Bud</NAME><PRICE>2.50</PRICE></BEER>

<BEER><NAME>Miller</NAME><PRICE>3.00</PRICE></BEER>

</BAR><BAR> …

</BARS>

Database Systems and Concepts, CSCI 3030U, UOIT, Course Instructor: Jarek Szlichta 14

Get the DTDfrom the filebar.dtd

Page 15: Document Type Definitionsdata.science.uoit.ca/wp-content/uploads/2014/07/DS... · Well-Formed and Valid XML •Well-Formed XMLallows you to invent your own tags. •Valid XML conforms

Attributes

• Opening tags in XML can have attributes.• In a DTD,<!ATTLIST E . . . >

declares attributes for element E, along with its datatype.

Database Systems and Concepts, CSCI 3030U, UOIT, Course Instructor: Jarek Szlichta 15

Page 16: Document Type Definitionsdata.science.uoit.ca/wp-content/uploads/2014/07/DS... · Well-Formed and Valid XML •Well-Formed XMLallows you to invent your own tags. •Valid XML conforms

Example: Attributes

• Bars can have an attribute kind, a character string describing the bar.<!ELEMENT BAR (NAME BEER*)><!ATTLIST BAR kind CDATA

#IMPLIED>

Database Systems and Concepts, CSCI 3030U, UOIT, Course Instructor: Jarek Szlichta 16

Character stringtype; no tagsAttribute is optional

opposite: #REQUIRED

Page 17: Document Type Definitionsdata.science.uoit.ca/wp-content/uploads/2014/07/DS... · Well-Formed and Valid XML •Well-Formed XMLallows you to invent your own tags. •Valid XML conforms

Example: Attribute Use

• In a document that allows BAR tags, we might see:<BAR kind = ”sushi”><NAME>Homma’s</NAME><BEER><NAME>Sapporo</NAME>

<PRICE>5.00</PRICE></BEER>...</BAR>

Database Systems and Concepts, CSCI 3030U, UOIT, Course Instructor: Jarek Szlichta 17

Page 18: Document Type Definitionsdata.science.uoit.ca/wp-content/uploads/2014/07/DS... · Well-Formed and Valid XML •Well-Formed XMLallows you to invent your own tags. •Valid XML conforms

ID’s and IDREF’s

• Attributes can be pointers from one object to another.• Allows the structure of an XML document to be a general graph,

rather than just a tree.

Database Systems and Concepts, CSCI 3030U, UOIT, Course Instructor: Jarek Szlichta 18

Page 19: Document Type Definitionsdata.science.uoit.ca/wp-content/uploads/2014/07/DS... · Well-Formed and Valid XML •Well-Formed XMLallows you to invent your own tags. •Valid XML conforms

Creating ID’s

• Give an element E an attribute A of type ID.• When using tag <E > in an XML document, give its attribute A a

unique value.• Example:

<E A = ”xyz”>

Database Systems and Concepts, CSCI 3030U, UOIT, Course Instructor: Jarek Szlichta 19

Page 20: Document Type Definitionsdata.science.uoit.ca/wp-content/uploads/2014/07/DS... · Well-Formed and Valid XML •Well-Formed XMLallows you to invent your own tags. •Valid XML conforms

Creating IDREF’s

• To allow elements of type F to refer to another element with an ID attribute, give F an attribute of type IDREF.• Or, let the attribute have type IDREFS, so the F -element can refer to

any number of other elements.

Database Systems and Concepts, CSCI 3030U, UOIT, Course Instructor: Jarek Szlichta 20

Page 21: Document Type Definitionsdata.science.uoit.ca/wp-content/uploads/2014/07/DS... · Well-Formed and Valid XML •Well-Formed XMLallows you to invent your own tags. •Valid XML conforms

Example: ID’s and IDREF’s

•A new BARS DTD includes both BAR and BEER subelements.•BARS and BEERS have ID attributes name.•BARS have SELLS subelements, consisting of a

number (the price of one beer) and an IDREF theBeer leading to that beer.•BEERS have attribute soldBy, which is an IDREFS

leading to all the bars that sell it.

Database Systems and Concepts, CSCI 3030U, UOIT, Course Instructor: Jarek Szlichta 21

Page 22: Document Type Definitionsdata.science.uoit.ca/wp-content/uploads/2014/07/DS... · Well-Formed and Valid XML •Well-Formed XMLallows you to invent your own tags. •Valid XML conforms

The DTD

<!DOCTYPE BARS [<!ELEMENT BARS (BAR*, BEER*)><!ELEMENT BAR (SELLS+)>

<!ATTLIST BAR name ID #REQUIRED><!ELEMENT SELLS (#PCDATA)>

<!ATTLIST SELLS theBeer IDREF #REQUIRED><!ELEMENT BEER EMPTY>

<!ATTLIST BEER name ID #REQUIRED><!ATTLIST BEER soldBy IDREFS #IMPLIED>

]>

Database Systems and Concepts, CSCI 3030U, UOIT, Course Instructor: Jarek Szlichta 22

Beer elements have an ID attribute called name,and a soldBy attribute that is a set of Bar names.

SELLS elementshave a number(the price) andone referenceto a beer.

Bar elements have nameas an ID attribute andhave one or moreSELLS subelements.

Explainednext

Page 23: Document Type Definitionsdata.science.uoit.ca/wp-content/uploads/2014/07/DS... · Well-Formed and Valid XML •Well-Formed XMLallows you to invent your own tags. •Valid XML conforms

Example: A Document

<BARS><BAR name = ”JoesBar”>

<SELLS theBeer = ”Bud”>2.50</SELLS><SELLS theBeer = ”Miller”>3.00</SELLS>

</BAR> …<BEER name = ”Bud” soldBy = ”JoesBar

SuesBar …” /> …</BARS>

Database Systems and Concepts, CSCI 3030U, UOIT, Course Instructor: Jarek Szlichta 23

Page 24: Document Type Definitionsdata.science.uoit.ca/wp-content/uploads/2014/07/DS... · Well-Formed and Valid XML •Well-Formed XMLallows you to invent your own tags. •Valid XML conforms

24Database Systems and Concepts, CSCI 3030U, UOIT, Course Instructor: Jarek Szlichta

XPathXQuery

Query Languages for XML

Page 25: Document Type Definitionsdata.science.uoit.ca/wp-content/uploads/2014/07/DS... · Well-Formed and Valid XML •Well-Formed XMLallows you to invent your own tags. •Valid XML conforms

25Database Systems and Concepts, CSCI 3030U, UOIT, Course Instructor: Jarek Szlichta

• Corresponding to the fundamental “relation” of the relational model is: sequence of items.

• An item is either:1. A primitive value, e.g., integer or string.2. A node (defined next).

The XPath/XQuery Data Model

Page 26: Document Type Definitionsdata.science.uoit.ca/wp-content/uploads/2014/07/DS... · Well-Formed and Valid XML •Well-Formed XMLallows you to invent your own tags. •Valid XML conforms

26Database Systems and Concepts, CSCI 3030U, UOIT, Course Instructor: Jarek Szlichta

Principal Kinds of Nodes

1. Document nodes represent entire documents.2. Elements are pieces of a document consisting of some opening

tag, its matching closing tag (if any), and everything in between.3. Attributes names that are given values inside opening tags.

Page 27: Document Type Definitionsdata.science.uoit.ca/wp-content/uploads/2014/07/DS... · Well-Formed and Valid XML •Well-Formed XMLallows you to invent your own tags. •Valid XML conforms

DTD for Running Example

<!DOCTYPE BARS [<!ELEMENT BARS (BAR*, BEER*)><!ELEMENT BAR (PRICE+)>

<!ATTLIST BAR name ID #REQUIRED><!ELEMENT PRICE (#PCDATA)>

<!ATTLIST PRICE theBeer IDREF #REQUIRED><!ELEMENT BEER EMPTY>

<!ATTLIST BEER name ID #REQUIRED><!ATTLIST BEER soldBy IDREFS #IMPLIED>

]>27Database Systems and Concepts, CSCI 3030U, UOIT,

Course Instructor: Jarek Szlichta

Page 28: Document Type Definitionsdata.science.uoit.ca/wp-content/uploads/2014/07/DS... · Well-Formed and Valid XML •Well-Formed XMLallows you to invent your own tags. •Valid XML conforms

Example Document

<BARS><BAR name = ”JoesBar”>

<PRICE theBeer = ”Bud”>2.50</PRICE><PRICE theBeer = ”Miller”>3.00</PRICE>

</BAR> …<BEER name = ”Bud” soldBy = ”JoesBar

SuesBar … ”/> …</BARS>

28

An element node

An attribute node

Document node is all of this, plusthe header ( <? xml version… ).

Database Systems and Concepts, CSCI 3030U, UOIT, Course Instructor: Jarek Szlichta

Page 29: Document Type Definitionsdata.science.uoit.ca/wp-content/uploads/2014/07/DS... · Well-Formed and Valid XML •Well-Formed XMLallows you to invent your own tags. •Valid XML conforms

Nodes as Semistructured Data

29

BARS

PRICEPRICE

BEERBAR name =”JoesBar”

theBeer =”Miller”

theBeer= ”Bud”

SoldBy= ”…”

name =”Bud”

3.002.50

Rose =documentBlue = elementGold = attributePurple = primitive

value

bars.xml

Database Systems and Concepts, CSCI 3030U, UOIT, Course Instructor: Jarek Szlichta

Page 30: Document Type Definitionsdata.science.uoit.ca/wp-content/uploads/2014/07/DS... · Well-Formed and Valid XML •Well-Formed XMLallows you to invent your own tags. •Valid XML conforms

Paths in XML Documents

• XPath is a language for describing paths in XML documents.• The result of the described path is a sequence of items.

30Database Systems and Concepts, CSCI 3030U, UOIT, Course Instructor: Jarek Szlichta

Page 31: Document Type Definitionsdata.science.uoit.ca/wp-content/uploads/2014/07/DS... · Well-Formed and Valid XML •Well-Formed XMLallows you to invent your own tags. •Valid XML conforms

Path Expressions

• Simple path expressions are sequences of slashes (/) and tags, starting with /.• Example: /BARS/BAR/PRICE

• Construct the result by starting with just the doc node and processing each tag from the left.

31Database Systems and Concepts, CSCI 3030U, UOIT, Course Instructor: Jarek Szlichta

Page 32: Document Type Definitionsdata.science.uoit.ca/wp-content/uploads/2014/07/DS... · Well-Formed and Valid XML •Well-Formed XMLallows you to invent your own tags. •Valid XML conforms

Example: /BARS

<BARS><BAR name = ”JoesBar”>

<PRICE theBeer = ”Bud”>2.50</PRICE><PRICE theBeer = ”Miller”>3.00</PRICE>

</BAR> …<BEER name = ”Bud” soldBy = ”JoesBar

SuesBar … ”/> …</BARS>

32

One item, theBARS elementDatabase Systems and Concepts, CSCI 3030U, UOIT,

Course Instructor: Jarek Szlichta

Page 33: Document Type Definitionsdata.science.uoit.ca/wp-content/uploads/2014/07/DS... · Well-Formed and Valid XML •Well-Formed XMLallows you to invent your own tags. •Valid XML conforms

Example: /BARS/BAR

<BARS><BAR name = ”JoesBar”>

<PRICE theBeer =”Bud”>2.50</PRICE><PRICE theBeer = ”Miller”>3.00</PRICE>

</BAR> …<BEER name = ”Bud” soldBy = ”JoesBar

SuesBar …”/> …</BARS>

33

This BAR element followed byall the other BAR elements

Database Systems and Concepts, CSCI 3030U, UOIT, Course Instructor: Jarek Szlichta

Page 34: Document Type Definitionsdata.science.uoit.ca/wp-content/uploads/2014/07/DS... · Well-Formed and Valid XML •Well-Formed XMLallows you to invent your own tags. •Valid XML conforms

Example: /BARS/BAR/PRICE

<BARS><BAR name = ”JoesBar”>

<PRICE theBeer =”Bud”>2.50</PRICE><PRICE theBeer = ”Miller”>3.00</PRICE>

</BAR> …<BEER name = ”Bud” soldBy = ”JoesBar

SuesBar …”/> …</BARS>

34

These PRICE elements followedby the PRICE elementsof all the other bars.

Database Systems and Concepts, CSCI 3030U, UOIT, Course Instructor: Jarek Szlichta

Page 35: Document Type Definitionsdata.science.uoit.ca/wp-content/uploads/2014/07/DS... · Well-Formed and Valid XML •Well-Formed XMLallows you to invent your own tags. •Valid XML conforms

Attributes in Paths

• Instead of going to subelements with a given tag, you can go to an attribute of the elements you already have.• An attribute is indicated by putting @ in front of its name.

35Database Systems and Concepts, CSCI 3030U, UOIT, Course Instructor: Jarek Szlichta

Page 36: Document Type Definitionsdata.science.uoit.ca/wp-content/uploads/2014/07/DS... · Well-Formed and Valid XML •Well-Formed XMLallows you to invent your own tags. •Valid XML conforms

Example: /BARS/BAR/PRICE/@theBeer

<BARS><BAR name = ”JoesBar”>

<PRICE theBeer = ”Bud”>2.50</PRICE><PRICE theBeer = ”Miller”>3.00</PRICE>

</BAR> …<BEER name = ”Bud” soldBy = ”JoesBar

SuesBar …”/> …</BARS>

36

These attributes contribute”Bud” ”Miller” to the result,followed by other theBeervalues.

Database Systems and Concepts, CSCI 3030U, UOIT, Course Instructor: Jarek Szlichta

Page 37: Document Type Definitionsdata.science.uoit.ca/wp-content/uploads/2014/07/DS... · Well-Formed and Valid XML •Well-Formed XMLallows you to invent your own tags. •Valid XML conforms

Paths that Begin Anywhere

• If the path starts from the document node and begins with //X, then the first step can begin at the root or any subelement of the root, as long as the tag is X.

37Database Systems and Concepts, CSCI 3030U, UOIT, Course Instructor: Jarek Szlichta

Page 38: Document Type Definitionsdata.science.uoit.ca/wp-content/uploads/2014/07/DS... · Well-Formed and Valid XML •Well-Formed XMLallows you to invent your own tags. •Valid XML conforms

Example: //PRICE

<BARS><BAR name = ”JoesBar”>

<PRICE theBeer =”Bud”>2.50</PRICE><PRICE theBeer = ”Miller”>3.00</PRICE>

</BAR> …<BEER name = ”Bud” soldBy = ”JoesBar

SuesBar …”/> …</BARS>

38

These PRICE elements andany other PRICE elementsin the entire document

Database Systems and Concepts, CSCI 3030U, UOIT, Course Instructor: Jarek Szlichta

Page 39: Document Type Definitionsdata.science.uoit.ca/wp-content/uploads/2014/07/DS... · Well-Formed and Valid XML •Well-Formed XMLallows you to invent your own tags. •Valid XML conforms

Wild-Card *

• A star (*) in place of a tag represents any one tag.• Example: /*/*/PRICE represents all price objects at the third level of

nesting.

39Database Systems and Concepts, CSCI 3030U, UOIT, Course Instructor: Jarek Szlichta

Page 40: Document Type Definitionsdata.science.uoit.ca/wp-content/uploads/2014/07/DS... · Well-Formed and Valid XML •Well-Formed XMLallows you to invent your own tags. •Valid XML conforms

Example: /BARS/*

<BARS><BAR name = ”JoesBar”>

<PRICE theBeer = ”Bud”>2.50</PRICE><PRICE theBeer = ”Miller”>3.00</PRICE>

</BAR> …<BEER name = ”Bud” soldBy = ”JoesBar

SuesBar … ”/> …</BARS>

40

This BAR element, all other BARelements, the BEER element, allother BEER elements

Database Systems and Concepts, CSCI 3030U, UOIT, Course Instructor: Jarek Szlichta

Page 41: Document Type Definitionsdata.science.uoit.ca/wp-content/uploads/2014/07/DS... · Well-Formed and Valid XML •Well-Formed XMLallows you to invent your own tags. •Valid XML conforms

Selection Conditions

• A condition inside […] may follow a tag.• If so, then only paths that have that tag and also satisfy the condition

are included in the result of a path expression.

41Database Systems and Concepts, CSCI 3030U, UOIT, Course Instructor: Jarek Szlichta

Page 42: Document Type Definitionsdata.science.uoit.ca/wp-content/uploads/2014/07/DS... · Well-Formed and Valid XML •Well-Formed XMLallows you to invent your own tags. •Valid XML conforms

Example: Selection Condition

• /BARS/BAR/PRICE[. < 2.75]

<BARS><BAR name = ”JoesBar”>

<PRICE theBeer = ”Bud”>2.50</PRICE><PRICE theBeer = ”Miller”>3.00</PRICE>

</BAR> …

42

The condition that the PRICE be< $2.75 makes this price but notthe Miller price part of the result.

The currentelement.

Database Systems and Concepts, CSCI 3030U, UOIT, Course Instructor: Jarek Szlichta

Page 43: Document Type Definitionsdata.science.uoit.ca/wp-content/uploads/2014/07/DS... · Well-Formed and Valid XML •Well-Formed XMLallows you to invent your own tags. •Valid XML conforms

Example: Attribute in Selection

• /BARS/BAR/PRICE[@theBeer = ”Miller”]

<BARS><BAR name = ”JoesBar”>

<PRICE theBeer = ”Bud”>2.50</PRICE><PRICE theBeer = ”Miller”>3.00</PRICE>

</BAR> …

43

Now, this PRICE elementis selected, along withany other prices for Miller.

Database Systems and Concepts, CSCI 3030U, UOIT, Course Instructor: Jarek Szlichta

Page 44: Document Type Definitionsdata.science.uoit.ca/wp-content/uploads/2014/07/DS... · Well-Formed and Valid XML •Well-Formed XMLallows you to invent your own tags. •Valid XML conforms

XQuery

• XQuery extends XPath to a query language that has power similar to SQL.• Uses the same sequence-of-items data model.• XQuery is an expression language.• Like relational algebra --- any XQuery expression can be an argument of any

other XQuery expression.

44Database Systems and Concepts, CSCI 3030U, UOIT, Course Instructor: Jarek Szlichta

Page 45: Document Type Definitionsdata.science.uoit.ca/wp-content/uploads/2014/07/DS... · Well-Formed and Valid XML •Well-Formed XMLallows you to invent your own tags. •Valid XML conforms

More About Item Sequences

• XQuery will sometimes form sequences of sequences.• All sequences are flattened.• Example: (1 2 () (3 4)) = (1 2 3 4).

45

Emptysequence

Database Systems and Concepts, CSCI 3030U, UOIT, Course Instructor: Jarek Szlichta

Page 46: Document Type Definitionsdata.science.uoit.ca/wp-content/uploads/2014/07/DS... · Well-Formed and Valid XML •Well-Formed XMLallows you to invent your own tags. •Valid XML conforms

FLWR Expressions

1. One or more for and/or let clauses.2. Then an optional where clause.3. A return clause.

46Database Systems and Concepts, CSCI 3030U, UOIT, Course Instructor: Jarek Szlichta

Page 47: Document Type Definitionsdata.science.uoit.ca/wp-content/uploads/2014/07/DS... · Well-Formed and Valid XML •Well-Formed XMLallows you to invent your own tags. •Valid XML conforms

Semantics of FLWR Expressions

• Each for creates a loop.• let produces only a local definition.

• At each iteration of the nested loops, if any, evaluate the whereclause.• If the where clause returns TRUE, invoke the return clause, and

append its value to the output.

47Database Systems and Concepts, CSCI 3030U, UOIT, Course Instructor: Jarek Szlichta

Page 48: Document Type Definitionsdata.science.uoit.ca/wp-content/uploads/2014/07/DS... · Well-Formed and Valid XML •Well-Formed XMLallows you to invent your own tags. •Valid XML conforms

Example: FOR

for $beer in document(”bars.xml”)/BARS/BEER/@namereturn

<BEERNAME> {$beer} </BEERNAME>• $beer ranges over the name attributes of all beers in

our example document.• Result is a sequence of BEERNAME elements:

<BEERNAME>Bud</BEERNAME> <BEERNAME>Miller</BEERNAME> . . .

48

“Expand the en-closed string byreplacing variablesand path exps. bytheir values.”

Our exampleBARS document

Database Systems and Concepts, CSCI 3030U, UOIT, Course Instructor: Jarek Szlichta

Page 49: Document Type Definitionsdata.science.uoit.ca/wp-content/uploads/2014/07/DS... · Well-Formed and Valid XML •Well-Formed XMLallows you to invent your own tags. •Valid XML conforms

LET Clauses

let <variable> := <expression>, . . .• Value of the variable becomes the sequence of items defined by the

expression.• Note let does not cause iteration; for does.

49Database Systems and Concepts, CSCI 3030U, UOIT, Course Instructor: Jarek Szlichta

Page 50: Document Type Definitionsdata.science.uoit.ca/wp-content/uploads/2014/07/DS... · Well-Formed and Valid XML •Well-Formed XMLallows you to invent your own tags. •Valid XML conforms

Example: LET

let $d := document(”bars.xml”)

let $beers := $d/BARS/BEER/@name

return

<BEERNAMES> {$beers} </BEERNAMES>

• Returns one element with all the names of the beers, like:

<BEERNAMES>Bud Miller …</BEERNAMES>

50Database Systems and Concepts, CSCI 3030U, UOIT, Course Instructor: Jarek Szlichta

Page 51: Document Type Definitionsdata.science.uoit.ca/wp-content/uploads/2014/07/DS... · Well-Formed and Valid XML •Well-Formed XMLallows you to invent your own tags. •Valid XML conforms

Order-By Clauses

• FLWR is really FLWOR: an order-by clause can precede the return.• Form: order by <expression>• With optional ascending or descending.

• The expression is evaluated for each assignment to variables.• Determines placement in output sequence.

51Database Systems and Concepts, CSCI 3030U, UOIT, Course Instructor: Jarek Szlichta

Page 52: Document Type Definitionsdata.science.uoit.ca/wp-content/uploads/2014/07/DS... · Well-Formed and Valid XML •Well-Formed XMLallows you to invent your own tags. •Valid XML conforms

Example: Order-By

• List all prices for Bud, lowest first.let $d := document(”bars.xml”)for $p in $d/BARS/BAR/PRICE[@theBeer=”Bud”]order by $preturn $p

52

Generates bindingsfor $p to PRICEelements.

Order those bindingsby the values insidethe elements .

Each binding is evaluatedfor the output. Theresult is a sequence ofPRICE elements.

Database Systems and Concepts, CSCI 3030U, UOIT, Course Instructor: Jarek Szlichta

Page 53: Document Type Definitionsdata.science.uoit.ca/wp-content/uploads/2014/07/DS... · Well-Formed and Valid XML •Well-Formed XMLallows you to invent your own tags. •Valid XML conforms

Aside: SQL ORDER BY

• SQL works the same way; it’s the result of the FROM and WHERE that get ordered, not the output.• Example: Using R(a,b),SELECT b FROM RWHERE b > 10

ORDER BY a;

53

R tuples with b>10are ordered by theira-values.

Then, the b-valuesare extracted from thesetuples and printed in thesame order.

Database Systems and Concepts, CSCI 3030U, UOIT, Course Instructor: Jarek Szlichta

Page 54: Document Type Definitionsdata.science.uoit.ca/wp-content/uploads/2014/07/DS... · Well-Formed and Valid XML •Well-Formed XMLallows you to invent your own tags. •Valid XML conforms

Predicates

• Normally, conditions imply existential quantification.

• Example: /BARS/BAR[@name] means “all the bars that have a name.”

• Example: /BARS/BEER[@soldAt = ”JoesBar”] gives the set of beers that are sold at Joe’s Bar.

54Database Systems and Concepts, CSCI 3030U, UOIT, Course Instructor: Jarek Szlichta

Page 55: Document Type Definitionsdata.science.uoit.ca/wp-content/uploads/2014/07/DS... · Well-Formed and Valid XML •Well-Formed XMLallows you to invent your own tags. •Valid XML conforms

The Query

let $bars = doc(”bars.xml”)/BARSfor $beer in $bars/BEERfor $bar in $bars/BARfor $price in $bar/PRICE

where $beer/@soldAt = ”JoesBar” and $price/@theBeer = $beer/@namereturn <BBP bar = {$bar/@name} beer = {$beer/@name}>{$price}</BBP>

55

True if ”JoesBar”appears anywherein the sequence

Database Systems and Concepts, CSCI 3030U, UOIT, Course Instructor: Jarek Szlichta

Page 56: Document Type Definitionsdata.science.uoit.ca/wp-content/uploads/2014/07/DS... · Well-Formed and Valid XML •Well-Formed XMLallows you to invent your own tags. •Valid XML conforms

Comparisons

• Let us produce the PRICE elements (from all bars) for all the beers that are sold by Joe’s Bar.• The output will be BBP elements with the names of the bar and beer

as attributes and the price element as a subelement.

56Database Systems and Concepts, CSCI 3030U, UOIT, Course Instructor: Jarek Szlichta

Page 57: Document Type Definitionsdata.science.uoit.ca/wp-content/uploads/2014/07/DS... · Well-Formed and Valid XML •Well-Formed XMLallows you to invent your own tags. •Valid XML conforms

Actions

• Read Chapters 11 about XML DTD and 12 about Xpath and XQuery• Review slides!

57Database Systems and Concepts, CSCI 3030U, UOIT, Course Instructor: Jarek Szlichta