from account
Download
Report
Transcript from account
Chapter 10: XML
2009
Originally from Database System
Concepts (by A. Silberschatz, 5th edition)
and edited by Sang Ho Lee
XML
Introduction to XML
Structure of XML Data
XML Document Schema
Querying and Transformation
Application Program Interfaces to XML
Storage of XML Data
XML Applications
Database System Concepts - 5th Edition
10.2
©Sang Ho Lee
Introduction
XML: Extensible Markup Language
Defined by the WWW Consortium (W3C)
Derived from SGML (Standard Generalized
Markup Language), but simpler to use than
SGML
Documents have tags giving extra information
about sections of the document
E.g. <title> XML </title> <slide> Introduction
…</slide>
Extensible, unlike HTML
Users can add new tags, and separately specify how
the tag should be handled for display
Database System Concepts - 5th Edition
10.3
©Sang Ho Lee
XML Introduction (cont.)
The ability to specify new tags, and to create nested tag
structures make XML a great way to exchange data, not just
documents.
Much of the use of XML has been in data exchange applications, not
as a replacement for HTML
XML´s main purpose is a physical representation for electronic data
exchange (EDS)
Tags make data (relatively) self-documenting
<bank>
<account>
<account_number> A-101
</account_number>
<branch_name>
Downtown </branch_name>
<balance>
500
</balance>
</account>
<depositor>
<account_number> A-101 </account_number>
<customer_name> Johnson </customer_name>
</depositor>
</bank>
Database System Concepts - 5th Edition
10.4
©Sang Ho Lee
What is XML? (History)
Arpanet (1969) Communication
Protocols
1970
1980
First ideas for SGML
at IBM (1969)
First “internet”
Protocols (1975)
Internet (ca. 1980)
Syntax
SGML ISO Spec.
500 pages (1986)
HTML 0.0 (1989)
Syntax, DTD,..
2000
HTML 1.0 (1992)
HTTP
1990
XML (1996)
CSS
HTML 4.0
(1998)
XML 1.0 (1998)
XHTML
XML 1.1 (2004)
WWW
Database System Concepts - 5th Edition
10.5
Publishing
Communit
y
©Sang Ho Lee
Markup Languages
Other Markup Languages
SGML (XML is a subset of SGML)
TeX (LaTex) for type setting
RTF (rich text format)
HTML (Hypertext Markup Language)
The last three standards define syntax and
semantics of documents
XML only defines syntax only. Semantics are
not part of XML
Database System Concepts - 5th Edition
10.6
©Sang Ho Lee
What is Markup?
Markups
Keywords enclosing text or nested markup
Examples
HTML: <b>Soongsil University</b>
“Soongsil
University” is presented in bold face
XML: <name> Soongsil University </name>
“Soongsil
University” is marked as a name, with no
further meaning
LaTex: \begin{abstract} In our book, we…
\end{abstract}
The
enclosed text may be type set in a special way
for a printed version of a book
Database System Concepts - 5th Edition
10.7
©Sang Ho Lee
XML: Motivation (1)
Data interchange is critical in today’s networked
world
Examples:
Banking: funds transfer
Order processing (especially inter-company orders)
Scientific data
– Chemistry: ChemML, …
– Genetics: BSML (Bio-Sequence Markup Language), …
Paper flow of information between organizations is being
replaced by electronic flow of information
Each application area has its own set of standards
for representing information
XML has become the basis for all new generation
data interchange formats
Database System Concepts - 5th Edition
10.8
©Sang Ho Lee
XML Motivation (2)
Earlier generation formats were based on plain text
with line headers indicating the meaning of fields
Similar in concept to email headers
Does not allow for nested structures, no standard “type”
language
Tied too closely to low level document structure (lines, spaces,
etc)
Each XML based standard defines what are valid
elements, using XML type specification languages to
specify the syntax (DTD (Document Type Descriptors), XML
Schema) plus textual descriptions of the semantics
XML allows new tags to be defined as required
However, this may be constrained by DTDs
A wide variety of tools is available for parsing,
browsing and querying XML documents/data
Database System Concepts - 5th Edition
10.9
©Sang Ho Lee
Comparison with Relational Data
Inefficient: tags, which in effect represent
schema information, are repeated
Better than relational tuples as a data-
exchange format
Unlike relational tuples, XML data is selfdocumenting due to presence of tags
Non-rigid format: tags can be added
Allows nested structures
Wide acceptance, not only in database systems,
but also in browsers, tools, and applications
Database System Concepts - 5th Edition
10.10
©Sang Ho Lee
Why XML
Common standard for specifying electronic documents
Matured standard for defining a subset of context free languages
Standard of the World Wide Web Consortium (W3C)
XML solves some issues in documents exchange (character
encoding, escape sequences, syntactical definition of documents,
parsing…)
Easier to integrate existing systems
Tools for parsing, modifying XML documents exist for all
technologies
XML is part of future extension of the World Wide Web
Web documents with content instead of presentation only
“semantic web”
XML became to the major standard for electronic documents
An important tool for computer scientists
Database System Concepts - 5th Edition
10.11
©Sang Ho Lee
XML Applications
Exchange of information between organizations
Electronic commerce applications where different organizations
collaborate to serve a customer
Large Web site maintenance
Off-loading and reloading of databases
Syndicated content, where content is being made available to different
web sites.
Scientific applications with new markup languages for mathematical
and chemical formulas
Electronic books with new markup languages to express rights and
ownership
Handheld devices and smart phones with new markup languages
optimized for these “alternative” devices
Database System Concepts - 5th Edition
10.12
©Sang Ho Lee
HTML vs. XML (1)
A Sample HTML document
<HTML>
<HEAD>
<TITLE>Hello From HTML</TITLE>
</HEAD>
<BODY>
<CENTER> <H1> An HTML Document </H1> </CENTER>
This is an HTML document!
</BODY>
</HTML>
Database System Concepts - 5th Edition
10.13
©Sang Ho Lee
HTML vs. XML (2)
A Sample XML Document
<?xml version="1.0" encoding="UTF-8"?>
<document>
<heading>
Hello From XML
</heading>
<message>
This is an XML document!
</message>
</document>
Database System Concepts - 5th Edition
10.14
©Sang Ho Lee
XML Syntax
XML elements are case sensitive
<Root>
<Branch>
</Branch>
</Root>
Element attributes: (details later)
Like HTML tags, XML elements can have attributes
<Countries>
<country name=“Korea”> </country>
<country name=“China”> </country>
</Countries>
An element may have several attributes, but each
attribute name can only occur once
<account
Database System Concepts - 5th Edition
acct-type = “checking” monthly-fee=“5”>
10.15
©Sang Ho Lee
XML Syntax: Naming Rules
Element naming rules:
Can contain all alphanumeric characters
Can’t begin with a number or a punctuation character
(except ‘_ ‘and ‘ : ’ )
Must not start with “xml”, which is reserved
Can’t contain any spaces
XML delimits between element names and attributes using a
space character
Valid tags:
<p> <copyright-information> <FirstName> <_firstname>
Invalid tags:
<123> <tom&jerry> <xml.is.nice> <database books>
Database System Concepts - 5th Edition
10.16
©Sang Ho Lee
XML Syntax: Empty Element
Empty element
elements that have no content or subelement are
known as empty elements.
empty elements only have one tag, not a start and
end tag
close an empty element with “/> “
Example
<?xml version="1.0" encoding="UTF-8"?>
<document>
<heading position=“center”/>
<message> An XML document!
</message>
</document>
Database System Concepts - 5th Edition
10.17
©Sang Ho Lee
XML Syntax
To store string data that may contain tags,
without the tags being interpreted as
subelements, use CDATA as below
<![CDATA[<account> … </account>]]>
Here, <account> and </account> are treated as just
strings
CDATA stands for “character data”
Database System Concepts - 5th Edition
10.18
©Sang Ho Lee
XML Example
An XML document using a style sheet
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="style.css"?>
<document>
<heading>
Hello From XML
</heading>
<message>
This is an XML document!
</message>
</document>
style.css
heading {display: block; font-size: 24pt; color: #ff0000; text-align: center}
message {display: block; font-size: 18pt; color: #0000ff; text-align: center}
Database System Concepts - 5th Edition
10.19
©Sang Ho Lee
XML Declaration
<?xml version = "1.0" standalone="yes" encoding="UTF-
8"?>
Optional XML declaration as first line (processing
instruction)
Always has prefix “xml”, starts with “<?”
Version : The XML version; currently, only 1.0 or 1.1 is
possible here, and most XML processors do not support
1.1 yet.
Encoding : The language encoding for the document.
the default here is UTF-8
This attribute is optional
Standalone : Set to "yes" if the document does not refer
to any external
documents or entities, "no" otherwise.
This attribute is optional
Database System Concepts - 5th Edition
10.20
©Sang Ho Lee
Structure of an XML Document
Element
Tags
Well-formed documents
Each start tag is terminated by one end tag with the
same name
One root tag (root node) exists
Arbitrary text between tags allowed and/or well formed
sequence of other tags
Valid documents (details later)
Well-formed document
Document conforms to associated XML schema (a
data type definition or W3C schema)
Database System Concepts - 5th Edition
10.21
©Sang Ho Lee
XML Syntax
Mixture of text with sub-elements is legal in
XML
Example:
<account>
This account is seldom used any more.
<account_number> A-102</account_number>
<branch_name> Perryridge</branch_name>
<balance>400 </balance>
</account>
Useful for document markup, but
discouraged for data representation
Database System Concepts - 5th Edition
10.22
©Sang Ho Lee
Tag Attributes (1)
Elements can have attributes
<account acct-type = “checking” >
<account_num> A-102 </account_num>
<branch_name> Perryridge </branch_name>
<balance> 400 </balance>
</account>
Attributes are specified by “name=value” pairs
inside the starting tag of an element
An element may have several attributes, but
each attribute name can only occur once
<account acct-type = “checking” monthly-fee=“5”>
Database System Concepts - 5th Edition
10.23
©Sang Ho Lee
Tag Attributes (2)
Provide additional information to a tag
Attributes consist of a name followed by “=“ and a value
enclosed in single or double quotes
<title language=“EN”> Database System Concept </title>
<telephone mobile=“13331638119” home=‘0433-291-4175’/>
Attribute names follow the same rules as tag names
Attributes are separated by spaces from each others and
the tag name
Attribute are not part of the tag name
<title language=“KO”>..</title>
Attribute names of a tag must be unique
<title language=“KO” language=“EN”> not valid
Database System Concepts - 5th Edition
10.24
©Sang Ho Lee
Attributes vs. Subelements
Distinction between subelement and attribute
In the context of documents, attributes are part of
markup, while subelement contents are part of
the basic document contents
In the context of data representation, the
difference is unclear and may be confusing
Same
information can be represented in two ways
– <account account_number = “A-101”> …. </account>
– <account>
<account_number>A-101</account_number> …
</account>
Suggestion: use attributes for identifiers of
elements, and use subelements for contents
Database System Concepts - 5th Edition
10.25
©Sang Ho Lee
Comments in XML
Comments:
Both XML and HTML use the same character
strings for comments
Comments are ignored by XML parsers
<!-- This is a comment and will not be processed
by the HTML or XML parser -->
Database System Concepts - 5th Edition
10.26
©Sang Ho Lee
Example on XML Comments
<?xml version=“1.0”?>
<!-- this is a comment , here is the end of the comment -->
<address>
<!-- this is a root-->
<person>
<name>SHLee</name>
<email>[email protected]</email>
</person>
<street>1-1 Sangdo-dong</street>
<room>303</room>
</address> <!-- this is a the end of root-->
Database System Concepts - 5th Edition
10.27
©Sang Ho Lee
Well- Formed XML Documents (1)
Requirements for well-form documents
Beginning the document with an XML Declaration
Using only legal character references
Including at least one element
Structuring elements correctly
Using the root element to contain all other
elements
Making attribute names unique
Database System Concepts - 5th Edition
10.28
©Sang Ho Lee
Well- Formed XML Documents (2)
Well-Formed
Not Well-Formed
<?xml version=“1.0”?>
<address>
<person>
<name>SHLee</name>
<email>shlee199@… </email>
</person>
<street>Sangdo-dong</street>
<room>3-303</room>
</address>
<no-root-element>
<comment>missing end tag
</no-root-element>
<no-root-element/>
Database System Concepts - 5th Edition
10.29
©Sang Ho Lee
Well- Formed XML Documents (3)
XML documents are structured hierarchically
The structure is tree-like
Order of nodes relevant
person tag before street tag
name
--
person
email
--
address
street
--
room
-Database System Concepts - 5th Edition
10.30
©Sang Ho Lee
Example of Nested Elements
<bank-1>
<customer>
<customer_name> Hayes </customer_name>
<customer_street> Main </customer_street>
<customer_city> Harrison </customer_city>
<account>
<account_number> A-102 </account_number>
<branch_name>
Perryridge </branch_name>
<balance>
400 </balance>
</account>
<account>
…
</account>
</customer>
.
.
</bank-1>
Database System Concepts - 5th Edition
10.31
©Sang Ho Lee
Motivation for Nesting
Nesting of data is useful in data transfer
Example: elements representing customer_id,
customer_name, and address nested within an order
element
Nesting is not supported, or discouraged, in
relational databases
With multiple orders, customer name and address are
stored redundantly
normalization replaces nested structures in each order by
foreign key into table storing customer name and address
information
Nesting is supported in object-relational databases
But nesting is appropriate when transferring data
External application does not have direct access to data
referenced by a foreign key
Database System Concepts - 5th Edition
10.32
©Sang Ho Lee
XML Namespaces (1)
XML data has to be exchanged between organizations
Other could add tags to your format independent of each others
The same tag name may have different meaning in different
organizations, causing confusion on exchanged documents
Better solution: use unique-name:element-name
Avoid using long unique names all over documents by using XML
Namespaces
<bank xmlns:FB=‘http://www.FirstBank.com’>
…
<FB:branch>
<FB:branchname>Downtown</FB:branchname>
<FB:branchcity> Brooklyn </FB:branchcity>
</FB:branch>
…
</bank>
Database System Concepts - 5th Edition
10.33
©Sang Ho Lee
Namespace Example (1)
How can conflicts between tag names be avoided?
Consider an XML format for movies
Company choose <rating> for a tag denoting how other
consume rates the file (from 5 starts = very good to zero
stars = very bad)
New version of vendor systems may introduce <rating> for
family guide (G, PG, …)
Renaming of companies tag name are expensive (many
systems may change)
Use XML name spaces for distinguishing names
Similar to Java package names for unique class names
Database System Concepts - 5th Edition
10.34
©Sang Ho Lee
Namespace Example (2)
Original vendor XML, Version 1.0
<movies>
<movie>
<title>Man in Black</title>
<rating>3</rating>
</movie>
</movies>
Vendors extension in later version 1.1
Rating for family guide
<movies>
<movie>
<title>Man in Black</title>
<rating>PG</rating>
</movie>
</movies>
Database System Concepts - 5th Edition
10.35
©Sang Ho Lee
Namespace Example (3)
Conflicts when merging !!!
Use unique prefixes for tag names
Vendor uses own prefix for element names
Every extension to XML format by others: use
other prefix for new elements
Prefix separated by a colon ( : )
<vendor:movies>
<vendor:movie>
<vendor:title>
Man In Black
</vendor:title>
<my:rating>
3
</my:rating>
</vendor:movie>
</vendor:movies>
Database System Concepts - 5th Edition
<vendor:movies>
<vendor:movie>
<vendor:title>
Man In Black
</vendor:title>
<vendor:rating>
PG
</vendor:rating>
</vendor:movie>
</vendor:movies>
10.36
©Sang Ho Lee
Namespace Example (4)
my is defined as a reference to a Uniform
Resource Locator (URL)
URI are unique (among the WWW)
URI consists of the protocol, an internet domain
name, a path
http://www.fh-karlsruhe.de/movies
Internet domain of an URI is unique because
world wide registered
Namespace declared for an element (here:
movies) and for all its subelements (until
overriden by another namespace declaration)
Database System Concepts - 5th Edition
10.37
©Sang Ho Lee
Namespace Example (5)
<vendor:movies
xmlns:my=“http://www.hs-karlsruhe.de/movies”
xmlns:vendor=“http://www.vendor.com/movies1.2”>
<vendor:movie>
<vendor:title> Man In Black </vendor:title>
<my:rating> 3 </my:rating>
<vendor:rating> PG </vendor:rating>
</vendor:movie>
</vendor:movies>
Database System Concepts - 5th Edition
10.38
©Sang Ho Lee
URI, URN, URL
URI (Uniform Resource Identifier)
RFC 2396 (1998) a compact string of characters for
identifying an abstract or physical resource
Examples: all URL and URN
URN (Uniform Resource Name)
RFC 2141 (1997): subset of URI that are required to remain
globally unique and persistent even when the resource
ceases to exist or becomes unavailable
Example: “URN:ISBN:0-395-36341-1” (defines a specific
book)
URL (Uniform Resource Locator)
RFC 1738 (1994): a formalized information for location and
access of resources via the internet
Example URL: “http://www.ietf.org/rfc/rfc1738.txt”
Database System Concepts - 5th Edition
10.39
©Sang Ho Lee
XML Namespaces (1)
URI
URN
URN:ISBN:0-395-36341-1
URL
http://www.fh-karlsruhe.de
URI ⊊ URN (⊆∕)
URI ⊊ URL (⊆∕)
Database System Concepts - 5th Edition
10.40
©Sang Ho Lee
XML Namespaces (2)
URI must no be existent, URI’s existence is not
checked by a parser.
Use URLs as URI
Unique because of registered domain name
For one domain name, infinite number of paths
Easy to point them to a web server (use http)
Database System Concepts - 5th Edition
10.41
©Sang Ho Lee
XML Namespaces (3)
xmlns=URI declares the default namespace
(the namespace for all elements without a prefix)
No Prefix name given.
Every element without prefix uses this namespace
<movies xmlns=“http://www.company.de/2005/movies/1.3”
xmlns:cp=“http://www.christian-pape.de/2005/movies”>
<movie>
<title>Man in Black</title>
<rating>PG</rating>
<cp:rating>3</cp:rating>
</movie>
</movies>
Database System Concepts - 5th Edition
10.42
©Sang Ho Lee
Overview (1)
Formal languages: Notations for specifying the
syntax of documents unambiguously
Chomsky Hierarchy
all languages
context-sensitive
context-free
Turingmachines
Production
systems
(vPw ->aX)
Database System Concepts - 5th Edition
regular
EBNF
10.43
Finite-state-machines
(FSM)
regular expressions
©Sang Ho Lee
Overview (2)
XML defines a subset of context free languages
DTD, XML Schemas are tools to specify a XML language
Regular languages are not a subset of XML
all languages
context-sensitive
XML
context-free
DTD
XML Schemas
regular
Database System Concepts - 5th Edition
10.44
©Sang Ho Lee
DTD vs. Schema (1)
So far
learned how to read and write XML documents
Syntax of XML documents
But,
no information is given whether a tag is optional or
mandatory, whether it can be repeated or not, …
No definition of how data (content of an element) is
represented, e.g. numbers, dates, …
Why DTD / XML Schema?
Well-formed documents are not strict enough for
automatically processing
Parser can check syntax of documents before processing
or writing them
Electronics documents should be as unambiguous for
automated processing as possible
Database System Concepts - 5th Edition
10.45
©Sang Ho Lee
DTD vs. Schema (2)
Document Type Definitions (DTD)
20 year old, part of SGML
Developed before XML
Simple, proven solution
Not itself in XML syntax
No type system (content is text)
XML schemas
Developed after XML
More complicate than DTD
In XML syntax
Elaborated and extensible type system
Both are equally powerful with respect to the
syntactical definition of an XML language
Database System Concepts - 5th Edition
10.46
©Sang Ho Lee
DTD vs. Schema (3)
When to use which mechanism
DTD simple to use, sufficient to check documents
validity
XML schema more powerful and complicated, for
rigorous type checking and automatic mapping
(XML <-> Java)
Database System Concepts - 5th Edition
10.47
©Sang Ho Lee
DTD Example (1)
One or more movie elements
Mandatory title followed by mandatory rating
Title and rating contain plain text
<?xml version=“1.0”?>
<movies>
<movie>
<title>Man in Black</title>
<rating>3</rating>
</movie>
</movies>
Database System Concepts - 5th Edition
10.48
©Sang Ho Lee
DTD Example (2)
Defines a tag (element) named “movies” that must contain one
or more “movie” elements
Defines an element “movie” that must contain a “title” followed
by a “rating”
Defines an element “title” that contains Parsed Character Data
(PCDATA)
Defines an element “rating” that contains PCDATA
<!ELEMENT
<!ELEMENT
<!ELEMENT
<!ELEMENT
Database System Concepts - 5th Edition
movies
movie
title
rating
(movie+)>
(title, rating)>
(#PCDATA)>
(#PCDATA)>
10.49
©Sang Ho Lee
DTD Example (3)
File “movies.dtd”
<!ELEMENT
<!ELEMENT
<!ELEMENT
<!ELEMENT
movies
movie
title
rating
(movie+)>
(title, rating)>
(#PCDATA)>
(#PCDATA)>
DTD fragment
Link to XML’s DTD
<?xml version=“1.0”?>
<!DOCTYPE movies SYSTEM “movies.dtd”>
<movies>
<movie>
<title>Man in Black</title>
<rating>3</rating>
</movie>
</movies>
Database System Concepts - 5th Edition
10.50
©Sang Ho Lee
DTD Example (4)
A XML document is valid if it is well-formed and it is a word of
the language defined by its attached DTD (or XML Schema)
<!ELEMENT
<!ELEMENT
<!ELEMENT
<!ELEMENT
movies
movie
title
rating
(movie+)>
(title, rating)>
(#PCDATA)>
(#PCDATA)>
DTD
Well-formed
<?xml version=“1.0”?>
<movies>
<movie>
<rating>3</rating>
<title>Man in Black</title>
<media> DVD </media>
</movie>
</movies>
Not valid: rating must follow title, tag media does not exist
Database System Concepts - 5th Edition
10.51
©Sang Ho Lee
DTD Example (5)
XML document may contain its own DTD
Useful if system cannot store DTD (XML
processing in a distribute environment)
<?xml version=“1.0”?>
<!DOCTYPE movies [
<!ELEMENT movies
(movie+)>
<!ELEMENT movie (title, rating)>
<!ELEMENT title
(#PCDATA)>
<!ELEMENT rating
(#PCDATA)>
]>
<movies>
<movie>
<title>Man in Black</title>
<rating>3</rating>
</movie>
</movies>
Database System Concepts - 5th Edition
10.52
©Sang Ho Lee
DTD Example (6)
Mixing external and internal DTDs
Internal DTD overwrites external definitions
Avoid! Error prone and confusing
<?xml version=“1.0”?>
<!DOCTYPE movies SYSTEM “movie.dtd” [
<!ELEMENT movie (title, rating?)>
]>
<movies>
<movie>
<title>Man in Black</title>
</movie>
</movies>
Database System Concepts - 5th Edition
10.53
©Sang Ho Lee
Doctype Declaration
“root” is element name defined in the DTD
Root element is not defined in DTD itself, but in the doctype
declaration
The location of the DTD (file) is given as a URI after
the keyword SYSTEM
If no external DTD is used, SYSTEN is omitted and
the DTD is given in brackets [ ]
If internal and external DTD is used, the internal
DTD is given in brackets after the URI of the external
DTD
<!DOCTYPE root SYSTEM URI >
<!DOCTYPE root [DTD] >
<!DOCTYPE root SYSTEM URI [DTD] >
Database System Concepts - 5th Edition
10.54
©Sang Ho Lee
Element Declaration (1)
The element declarations define the vocabulary
and structure of the XML dialect
“name” must be a valid tag name
“content-model” defines which content for “name” is
valid
When an application or parser reads an element
“name”, then its content is checked against the
rules defined in its content model
<!ELEMENT name
Database System Concepts - 5th Edition
content-model>
10.55
©Sang Ho Lee
Element Declaration (2)
Three types of content models exist
ANY: any well-formed content is allowed for
“name”
EMPTY: no content at all is allowed
Regular expressions over tag name, regular
operators, and #PCDATA
<!ELEMENT name
Database System Concepts - 5th Edition
10.56
content-model>
©Sang Ho Lee
Element Declaration - ANY
<!ELEMENT
<!ELEMENT
example (description, example-xml)>
example-xml ANY>
DTD (fragment)
<example>
<description>
An XML dialect for rating movies
</description>
<example-xml>
<movies>
<movie>
<title>Man in Black</title>
<rating>3</rating>
</movie>
</movies>
</example-xml>
<example>
Database System Concepts - 5th Edition
10.57
Well-formed and valid
©Sang Ho Lee
Element Declaration - EMPTY
<!ELEMENT movies (movie+)>
<!ELEMENT movie (title, rating, available-on-dvd?)>
<!ELEMENT title (#PCDATA)>
<!ELEMENT rating (#PCDATA)>
<!ELEMENT available-on-dvd EMPTY>
DTD
<movies>
<movie>
<title>Man in Black</title>
Well-formed and valid
<rating>3</rating>
<available-on-dvd/>
</movie>
</movies>
<movies>
<movie>
<title>Man in Black</title>
Well-formed
<rating>3</rating>
<available-on-dvd>yes</available-on-dvd>
Invalid
</movie>
</movies>
Database System Concepts - 5th Edition
10.58
©Sang Ho Lee
Element Declaration – Regular Expression (1)
<!ELEMENT movies
<!ELEMENT movie
<!ELEMENT title
<!ELEMENT rating
(movie+)>
(title, rating*, media?)>
(#PCDATA)>
(#PCDATA)>
DTD
<movies>
<movie>
<title>Man in Black</title>
<rating>3</rating>
<rating>5</rating>
</movie>
</movies>
Well-formed and valid
<movies>
<movie>
<title>Man in Black</title>
<media> DVD </media>
</movie>
</movies>
Database System Concepts - 5th Edition
10.59
©Sang Ho Lee
Element Declaration – Regular Expression (2)
Regular expression in parenthesis ( )
Sub-expressions can be set in parenthesis if
necessary
Operator
Semantics
Tag name t
Child element with tag name t
#PCDATA
Any text (parsed character data)
r1,r2
r1 followed by r2
r1 | r2
r1 or alternatively r2
r+
One or more times r
r*
Zero or more times r
r?
Zero or one time r
Database System Concepts - 5th Edition
10.60
©Sang Ho Lee
Element Declaration – Regular Expression (3)
<!ELEMENT
<!ELEMENT
<!ELEMENT
<!ELEMENT
DTD(PCDATA omitted)
address-book (entry+)>
entry
(name, address*, comment?)>
address
(street, location)>
location
( town | ( zip-code, town) )>
<address-book>
<entry>
<name>Mr. X</name>
<comment>Address not know</comment>
</entry>
<entry>
Well-formed
and valid
<name>Christian Pape</name>
<address>
<street>Moltkestrasse 30</street>
<location>
<town>Karlsruhe</town>
</location>
</address>
</entry>
</address-book>
Database System Concepts - 5th Edition
10.61
©Sang Ho Lee
Element Declaration – Regular Expression (4)
Recursion in regular expressions is allowed
<tree>
<node>
<node>
<leaf>a</leaf>
<leaf>b</leaf>
</node>
<leaf>c</leaf>
<node>
<leaf>d</leaf>
</node>
</node>
<node>
<leaf>e</leaf>
</node>
</tree>
Database System Concepts - 5th Edition
<!ELEMENT tree (node*)>
<!ELEMENT node ( (node | leaf)* ) >
<!ELEMENT leaf
(#PCDATA)>
a
10.62
b
c
d
e
©Sang Ho Lee
Element Declaration – Regular Expression (5)
Mixed Content: Element contains #PCDATA and a
regular expression
#PCDATA must be separated with | from regular expression
The mixed content must always be repeated ( * )
<!ELEMENT address-book
<!ELEMENT entry
<!ELEMENT comments
<!ELEMENT b
(entry+)>
(name, address*, comments?)>
(#PCDATA | b)*>
(#PCDATA)>
DTD (fragment)
<address-book>
<entry>
<name>Mr. X</name>
<comments>Address <b>not</b> know</comments>
</entry>
</address-book>
Database System Concepts - 5th Edition
10.63
©Sang Ho Lee
Element Declaration – Regular Expression (6)
Regular Expressions must be deterministic
Validating a document by reading one element at time
Advantage: better performance for XML parsers
<!ELEMENT book-cover ( (title, author) | (title, subtitle)>
<book-cover>
<title>
XML by Example
</title>
<author>
Benoît Marchal
</author>
</book-cover>
Database System Concepts - 5th Edition
When reading title,
parser can not decide
whether to choose
(title, author) or
(title, subtitle)
10.64
©Sang Ho Lee
Element Declaration – Regular Expression (7)
Every indeterministic regular expression can be transformed to
a deterministic one
For an indeterministic regular expression there exists an equivalent
indeterministic finite-state-machine (FSM)
For an indeterminisitc FSM, there exists a deterministic FSM
For the deterministic FSM, there exists an equivalent regular
expression
<!ELEMENT book-cover ( (title, author) | (title, subtitle)>
title
author
author
title
book-cover
title
book- cover
subtitle
subtitle
<!ELEMENT book-cover ( title, author | subtitle)>
Database System Concepts - 5th Edition
10.65
©Sang Ho Lee
Attribute Declaration (1)
Attributes must be declared in DTD, too
Keyword ATTLIST
Can occur anywhere in the DTD
should be placed directly behind declaration of corresponding
element
Element can have several attributes
No order of attributes
<!ELEMENT
<!ATTLIST
email
EMPTY>
email
href
CDATA
preferred
(true | false)
#REQUIRED
“false”>
<email href=“mailto:[email protected]” preferred=“true”/>
<email href=“mailto:[email protected]” />
Database System Concepts - 5th Edition
10.66
Well-formed and valid
©Sang Ho Lee
Attribute Declaration (2)
Attribute name
Attribute name, unique among the given element name
Type
Defines which values are valid for this attribute
Default
Defines whether attribute is mandatory, optional, and/or a
default value
Attributes in list are considered unordered
<!ATTLIST element-name
Database System Concepts - 5th Edition
attr-name-1
…
attr-name-n
10.67
type-1
default-1
type-n
default-n>
©Sang Ho Lee
Attributes Declaration - Types
Attribute type
Semantics
CDATA
Character data, without <, but with entity
references like <
v1 | v2 |…| vn
Attribute can be one of the given literal values
ID
Value is a document wide unique identifier
IDREF
Value is a reference to a value of a ID attribute
<!ATTLIST element-name
Database System Concepts - 5th Edition
attr-name-1
…
attr-name-n
10.68
type-1
default-1
type-n
default-n>
©Sang Ho Lee
DTD / Attributes Declaration – Types
<!ELEMENT
<!ELEMENT
<!ATTLIST
<!ELEMENT
<!ATTLIST
movies (producer+, movie+) >
producer
(#PCDATA)>
producer
pid
ID
#REQUIRED>
movie
(title)>
movie
download CDATA “www.download.com”
rating
(P|PG| … )
#REQUIRED
producer IDREF
#REQUIRED>
<movies>
<producer pid=”123”>Columbia Pictures</producer>
<movie producer=”123”
rating=”PG”>
<title>Man in Black</title>
</movie>
<movie producer=”123”
download=”www.mib2.com”
rating=“PG”>
<title>Man in Black II</title>
</movie>
</movies>
Database System Concepts - 5th Edition
10.69
Producer
producer
1
0..*
Movie
©Sang Ho Lee
DTD / Attribute Declaration (1)
Attribute Default
Semantics
#REQUIRED
Attribute is mandatory
#IMPLIED
Attribute is optional with no default
v (a value)
Attribute is optional with default v
#FIXED v
Attribute is optional
when given, then value must be v
<!ATTLIST element-name
Database System Concepts - 5th Edition
attr-name-1
…
attr-name-n
10.70
type-1
default-1
type-n
default-n>
©Sang Ho Lee
DTD / Attribute Declaration (2)
<!ELEMENT an-element EMPTY>
<!ATTLIST an-element an-attribute CDATA #REQUIRED>
<!ATTLIST an-element an-attribute CDATA #IMPLIED>
<!ATTLIST an-element an-attribute (yes | no) “yes”>
<!ATTLIST an-element an-attribute #FIXED “yes”>
<an-element/>
<an-element an-attribute=“yes”/>
<an-element an-attribute=“no”/>
<an-element an-attribute=“maybe”/>
Database System Concepts - 5th Edition
10.71
©Sang Ho Lee
Bank DTD
<!DOCTYPE bank [
<!ELEMENT bank ( ( account | customer | depositor)+)>
<!ELEMENT account (account_number, branch_name, balance)>
<! ELEMENT customer(customer_name, customer_street,
customer_city)>
<! ELEMENT depositor (customer_name, account_number)>
<! ELEMENT account_number (#PCDATA)>
<! ELEMENT branch_name (#PCDATA)>
<! ELEMENT balance (#PCDATA)>
<! ELEMENT customer_name (#PCDATA)>
<! ELEMENT customer_street (#PCDATA)>
<! ELEMENT customer_city (#PCDATA)>
]>
Database System Concepts - 5th Edition
10.72
©Sang Ho Lee
IDs and IDREFs
An element can have at most one attribute of
type ID
The ID attribute value of each element in an
XML document must be distinct
Thus the ID attribute value is an object identifier
An attribute of type IDREF must contain the ID
value of an element in the same document
An attribute of type IDREFS contains a set of (0
or more) ID values. Each ID value must contain
the ID value of an element in the same
document
Database System Concepts - 5th Edition
10.73
©Sang Ho Lee
Bank DTD with Attributes
Bank DTD with ID and IDREF attribute types
<!DOCTYPE bank-2[
<!ELEMENT account (branch, balance)>
<!ATTLIST account
account_number
ID
# REQUIRED
owners
IDREFS # REQUIRED>
<!ELEMENT customer(customer_name, customer_street,
customer_city)>
<!ATTLIST customer
customer_id
ID
# REQUIRED
accounts
IDREFS # REQUIRED>
]>
… declarations for branch, balance, customer_name,
customer_street and customer_city
Database System Concepts - 5th Edition
10.74
©Sang Ho Lee
XML data with ID and IDREF attributes
<bank-2>
<account account_number=“A-401” owners=“C100 C102”>
<branch_name> Downtown
<balance>
500
</branch_name>
</balance>
</account>
<customer customer_id=“C100” accounts=“A-401”>
<customer_name> Joe
<customer_street> Monroe
<customer_city> Madison
</customer_name>
</customer_street>
</customer_city>
</customer>
<customer customer_id=“C102” accounts=“A-401 A-402”>
<customer_name> Mary
</customer_name>
<customer_street> Erin
</customer_street>
<customer_city> Newark </customer_city>
</customer>
</bank-2>
Database System Concepts - 5th Edition
10.75
©Sang Ho Lee
DTD Namespaces (1)
DTD do not understand namespaces
Namespaces younger than DTDs
Attribute xmlns:my, xmlns:yours and prefixes in elements
are unknown
DTD must provide attributes and prefixes
<movies xmlns:my=“http://www.fh-karlsruhe.de/movies”
xmlns:yours=“http://fh-karlsruhe.de/movies”>
<movie>
<title>Man in Black</title>
<my:rating>3</my:rating>
<yours:rating>Averaqe</yours:rating>
<rating>PG</rating>
</movie>
</movies>
Database System Concepts - 5th Edition
10.76
©Sang Ho Lee
DTD Namespaces (2)
<!ELEMENT movies
<!ELEMENT movie
<!ATTLIST
movies
<!ELEMENT
<!ELEMENT
my:rating
yours:rating
(movie)>
(title, my:rating, yours:rating)>
xmlns:my CDATA #IMPLIED
xmlns:yours CDATA #IMPLIED>
#PCDATA>
#PCDATA>
DTD (fragment)
<movies xmlns:my=“http://www.fh-karlsruhe.de/movies”
xmlns:yours=“http://fh-karlsruhe.de/movies”>
<movie>
<title>Man in Black</title>
<my:rating>3</my:rating>
<yours:rating>Averaqe</yours:rating>
<rating>PG</rating>
</movie>
</movies>
Database System Concepts - 5th Edition
10.77
©Sang Ho Lee
Limitations of DTDs
No typing of text elements and attributes
All values are strings, no integers, reals, etc.
Difficult to specify unordered sets of subelements
Order is usually irrelevant in databases (unlike in the
document-layout environment from which XML evolved)
(A | B)* allows specification of an unordered set, but cannot
ensure that each of A and B occurs only once
IDs and IDREFs are untyped
The owners attribute of an account may contain a reference
to another account, which is meaningless
owners attribute should ideally be constrained to refer to customer
elements
Database System Concepts - 5th Edition
10.78
©Sang Ho Lee
XML Schema
XML Schema is a more sophisticated schema language which
addresses the drawbacks of DTDs. Supports
Typing of values
E.g. integer, string, etc
Also, constraints on min/max values
User-defined, complex types
Many more features including
uniqueness and foreign key constraints, inheritance
XML Schema is itself specified in XML syntax, unlike DTDs
More-standard representation, but verbose
XML Scheme is integrated with namespaces
But, XML Schema is significantly more complicated than DTDs
Database System Concepts - 5th Edition
10.79
©Sang Ho Lee
XML schema vs. DTD
DTD’s limitations
Not itself in XML Syntax
Does not “understand” namespaces
No other type than “strings”: (P)CDATA
XML schema
Is a XML dialect itself
Support for namespaces
More types and ability to build own types
Extensible type systems allows far reaching
automated processing of XML documents
Rigor automated checking of document content
Automated conversion of XML documents to, for example,
Java objects and vice versa
Database System Concepts - 5th Edition
10.80
©Sang Ho Lee
XML Schema --- Hello World Example
XML schema starts with xsd:schema
Namespace xsd used
Elements declared with xsd:element
Attribute for name and for the type of the element’s content
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name=“hello-world" type="xsd:string"/>
</xsd:schema>
XML Schema
<!ELEMENT hello-world (#PCDATA)>
DTD
Database System Concepts - 5th Edition
10.81
©Sang Ho Lee
XML Schema Example (1/2)
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:complexType name="movie-type">
<xsd:sequence>
<xsd:element name="title" type="xsd:string"/>
<xsd:element name="rating" type="xsd:int"/>
</xsd:sequence>
</xsd:complexType>
<xsd:element name="movies">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="movie" type="movie-type“
maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
01_movies.xsd
Type declaration
Element definition
Anonymous
Type declaration
<!ELEMENT movies (movie+)>
<!ELEMENT movie (title, rating)>
<!ELEMENT title (#PCDATA)>
<!ELEMENT rating (#PCDATA)>
Database System Concepts - 5th Edition
10.82
DTD
©Sang Ho Lee
XML Schema Example (2/2)
<?xml version="1.0"?>
<movies xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="01_movies.xsd">
<movie>
<title>Die Hard</title>
<rating>1</rating>
</movie>
</movies>
valid
Invalid (“Good” is not of type int)
<?xml version="1.0"?>
<movies xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="01_movies.xsd">
<movie>
<title>Die Hard</title>
<rating>Good</rating>
</movie>
</movies>
Database System Concepts - 5th Edition
10.83
©Sang Ho Lee
XML Schema Type
Atomic types
Build in. integer, Boolean, date, and more
Can be used directly with declaration of elements
Simple types
Derived from atomic types
Can be restricted to a set of values, e.g. by regular
expressions
Complex types
Contain elements and attributes
Build from simple types
Each element has a type for its content
Complex type of content consists of elements
Simple type if content is pure data
Database System Concepts - 5th Edition
10.84
©Sang Ho Lee
Atomic types
Type name
Semantics
anyType, anyURI
Any simple or complex type, any URI
string
Character string
normalizedString
String without carriage return, line feed, or tabs
long, int, short, byte
unsignedLong, …
64, 32, 16, 8 bit integer (like in Java)
non-negative long, …
decimal, float, double
Arbitrary precision, IEEE-32, 64 Bit floating point
boolean
true, false
dateTime
date
time
ISO coded date and time: 1971-11-08T10:00:00
Format: YYYY-DD-MM
Format: HH:MM:SS
ID, IDREF, ENTITY
As defined in by DTD
Database System Concepts - 5th Edition
10.85
©Sang Ho Lee
Atomic types Example
<xsd:schema xmlns:xsd=“http://www.w3.org/2001/XMLSchema”>
<xsd:complexType name=“person-type”>
<xsd:sequence>
<xsd:element name=“name” type=“xsd:normalizedString”/>
<xsd:element name=“birthDate” type=“xsd:date”/>
<xsd:element name=“salary” type=“xsd:decimal”/>
<xsd:element name=“comment” type=“xsd:string”/>
<xsd:element name=“male” type=“xsd:boolean”/>
</xsd:sequence>
</xsd:complexType>
<xsd:element name=“person” type=“person-type”/>
</xsd: schema>
XML Schema
“person.xsd”
<person xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="person.xsd">
<name>Mr. X</name>
<birthDate>1958-06-13</birthDate>
<salary>2500.00</salary>
<comment>This person does not really exists</comment>
<male>true</male>
</person>
Database System Concepts - 5th Edition
10.86
©Sang Ho Lee
Simple types (1/2)
Simple types are declared standalone
(optional) restriction tag for restriction values
attribute base for defining the base type
multiple enumeration-elements for defining all
possible values of the new type
country-code can be use as a new type
<xsd:simpleType name=“country-code”>
<xsd:restriction base=“xsd:string”>
<xsd:enumeration value=“KO”/>
<xsd:enumeration value=“EN”/>
<xsd:enumeration value=“FR”/>
</xsd:restriction>
</xsd:simpleType>
<xsd:element name=“country” type=“country-code”/>
Database System Concepts - 5th Edition
10.87
©Sang Ho Lee
Simple types (2/2)
Restrictions in XML Schema are called facets
Most common facets for string types:
Tag name
enumeration
length
minLength
maxLength
pattern
Database System Concepts - 5th Edition
Semantics
Limits the set of values to those in the enumerations
Forces a string to the given length
Sets the minimal length of a string
Sets the maximum length of a string
Limits the values of a string to a regular expression
r* zero or more times a
r+ one or more times a
r? zero or one times a (a is optional)
[a-z] all character from “a” to “z”
r1 | r2 either r1 or r2
r{n,m} n- up to m-times r
^r all characters except r (r a concatenation of chars)
10.88
©Sang Ho Lee
Simple types Example
<xsd:element name=“customer-identification”>
<xsd:simpleType>
<xsd:restriction base=“xsd:string”>
<xsd:length value=“8”/>
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
valid
<customer-identification>no-12345</customer-identification>
<customer-identification>
too-many-characters
</customer-identification>
Invalid (more than 8 characters)
Leading spaces count, too!
Database System Concepts - 5th Edition
10.89
©Sang Ho Lee
Complex types (1)
Complex types can be sequences of elements,
attributes , simple or complex content
Control of repetitiveness with min/maxOccurs
<!ELEMENT address-book (entry+)>
<!ELEMENT entry (name, address*, comment?)>
<!ELEMENT address (street, location)>
<xsd:complexType name=“entry-type">
<xsd:sequence>
<xsd:element name=“name“ type="xsd:string“
minOccures=“1” maxOccures=“1” />
<xsd:element name=“address" type="xsd:address-type“
maxOccurs=“unbound”/>
<xsd:element name=“comment” type=“xsd:string”
minOccures=“0”/>
</xsd:sequence>
</xsd:complexType>
Database System Concepts - 5th Edition
10.90
default
Another
complex - type
©Sang Ho Lee
Complex types (2)
MinOccurs
MaxOccurs
Semantics
DTD equivalent
1
1
Element must
occur exactly
once (default)
(element)
0
1
Zero or once
(element?)
0
unbounded
Zero or more
(element*)
1
unbounded
Once or more
(element+)
2
4
Two to four
times
not applicable?
Database System Concepts - 5th Edition
10.91
©Sang Ho Lee
Complex types (3)
Complex types can be anonymous
Content of an element declaration, omit name of type
<xsd:element name=“entry”>
<xsd:complexType>
<xsd:sequence>
<xsd:element name=“name“ type="xsd:string“/>
<xsd:element name=“address” maxOccurs=“unbound”>
<xsd:complexType>
<xsd:sequence>
<xsd:element name=“street” type=“xsd:string”/>
<xsd:element name=“location” type=“xsd:string”/>
</xsd:sequence>
</xsd:complexType>
</xsd.element>
<xsd:element name=“comment” type=“xsd:string” minOccures=“0”/>
</xsd:sequence>
</xsd:complexType>
<!ELEMENT address-book (entry+)>
</xsd:element>
<!ELEMENT entry (name, address*, comment?)>
<!ELEMENT address (street, location)>
Database System Concepts - 5th Edition
10.92
©Sang Ho Lee
Complex types (4)
xsd:sequence
Declared elements are ordered in sequence
xsd:all
Declared elements are unordered
Must be top-most in a content-model and constituents must
be elements
xsd:choice
One of the declared elements had to be chosen
Database System Concepts - 5th Edition
10.93
©Sang Ho Lee
xsd:all Example
xsd:all
<xsd:element name=“address”>
<xsd:complexType>
<xsd:all>
<xsd:element name=“street”
type=“xsd:string”/>
<xsd:element name=“location”
type=“xsd:string”/>
</xsd:all>
</xsd:complexType>
</xsd.element>
Database System Concepts - 5th Edition
10.94
<address>
<street>Moltkestr. 30</street>
<location>Karlsruhe</location>
</address>
Both are valid
<address>
<location>Karlsruhe</location>
<street>Moltkestr. 30</street>
</address>
©Sang Ho Lee
xsd:choice Example
xsd:choice
<address>
<street>Moltkestr. 30</street>
</address>
Both are valid
<xsd:element name=“address”>
<xsd:complexType>
<xsd:choice>
<xsd:element name=“street”
type=“xsd:string”/>
<xsd:element name=“location”
type=“xsd:string”/>
</xsd:choice>
</xsd:complexType>
</xsd.element>
Database System Concepts - 5th Edition
10.95
<address>
<location>Karlsruhe</location>
</address>
invalid
<address>
<street>Moltkestr. 30</street>
<location>Karlsruhe</location>
</address>
©Sang Ho Lee
Extending types
Types (atomic, simple, complex) can be extended to new types
Additional complexContent / simpleContent element necessary
<xsd:complexType name=“address-type”>
<xsd:all>
<xsd:element name=“street”
type=“xsd:string”/>
<xsd:element name=“location”
type=“xsd:string”/>
</xsd:all>
</xsd:complexType>
<other-address>
<zip-code>…</zip-code>
<location>…</location>
<street>…</street>
<house-number/>
</other-address>
Database System Concepts - 5th Edition
<address>
<location>…</location>
<street>…</street>
</address>
<xsd:complexType name=“address-full”>
<xsd:complexContent>
<xsd:extension base=“address-type”>
<xsd:all>
<xsd:element name=“house-number”
type=“xsd:string”/>
<xsd:element name=“zip-code”
type=“xsd:string”/>
</xsd:all>
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
10.96
©Sang Ho Lee
XML Schema Attributes (1)
Attributes declared with xsd:attribute within the type
definition and the name of attribute, its type, use and
value
<xsd:complexType name=“address-type”>
<xsd:all>
<xsd:element name=“street”
type=“xsd:string”/>
<xsd:element name=“location”
type=“xsd:string”/>
</xsd:all>
<xsd:attribute name=“preferred”
type=“xsd:boolean”
use=“default”
value=“false”/>
</xsd:complexType>
Database System Concepts - 5th Edition
10.97
<address preferred=“true”>
<location>…</location>
<street>…</street>
</address>
©Sang Ho Lee
XML Schema Attributes (2)
Attribute
Optional
name=“aName”
type=“aType”
use=“optional”
use=“required”
use=“default” value=“v”
Semantics
no
Name of the attribute
unique among the type/element
no
a simple type
zero or once
exactly once
zero or once, with given default
value
exactly once, with fixed given
value
yes
use=“fixed” value=“v”
<attribute name=“id” type=“xsd:ID” use=“required”/>
<attribute name=“age” type=“xsd:string” use=“optional”/>
<attribute name=“language” type=“xsd:string” use=“default” value=“ko”/>
Database System Concepts - 5th Edition
10.98
©Sang Ho Lee
XML Schema Namespaces
DTD
No support of XML namespaces
Prefix is direct part of element name in DTD
Changing prefix results in change of whole DTD
One defined, prefixes have to be used in document
instances
XML Schemas
Prefix declaration separated from element and
attribute names: easier to change namespace
Only root element need to be qualified in document
instance
Database System Concepts - 5th Edition
10.99
©Sang Ho Lee
Namespace Example (1/2)
<xsd:schema
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:mv="http://www.fh-karlsruhe.de/movies“
targetNamespace="http://www.fh-karlsruhe.de/movies“
elementFormDefault="unqualified"
attributeFormDefault="unqualified">
<xsd:complexType name="movie-type">
<xsd:sequence>
<xsd:element name="title" type="xsd:string"/>
<xsd:element name="rating" type="xsd:int"/>
</xsd:sequence>
</xsd:complexType>
<xsd:element name="movies">
<xsd:complexType>
<xsd:sequence>
<xsd:element
name="movie" type="movie-type“
maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
Database System Concepts - 5th Edition
10.100
©Sang Ho Lee
Namespace Example (2/2)
Global elements (in particular the root
element) must always be qualified with a
namespace prefix
Local elements (attributes)
may or may not be qualified with a
namespace prefix, if elementFormDefault
(attributeFormDefault) is set to
“unqualified”
Must always be qualified, otherwise
Database System Concepts - 5th Edition
10.101
©Sang Ho Lee
XML Schema Version of Bank DTD (1)
<xs:schema xmlns:xs=http://www.w3.org/2001/XMLSchema>
<xs:element name=“bank” type=“BankType”/>
<xs:element name=“account”
<xs:complexType>
<xs:sequence>
<xs:element name=“account_number” type=“xs:string”/>
<xs:element name=“branch_name”
type=“xs:string”/>
<xs:element name=“balance”
type=“xs:decimal”/>
</xs:sequence>
</xs:complexType>
</xs:element>
… definitions of customer and depositor …
<xs:complexType name=“BankType”>
<xs:squence>
<xs:element ref=“account” minOccurs=“0” maxOccurs=“unbounded”/>
<xs:element ref=“customer” minOccurs=“0” maxOccurs=“unbounded”/>
<xs:element ref=“depositor” minOccurs=“0” maxOccurs=“unbounded”/>
</xs:sequence>
</xs:complexType>
</xs:schema>
Database System Concepts - 5th Edition
10.102
©Sang Ho Lee
XML Schema Version of Bank DTD (2)
Choice of “xs:” was ours -- any other
namespace prefix could be chosen
Element “bank” has type “BankType”, which
is defined separately
xs:complexType is used later to create the named
complex type “BankType”
Element “account” has its type defined in-line
Database System Concepts - 5th Edition
10.103
©Sang Ho Lee
More features of XML Schema
Attributes specified by xs:attribute tag:
<xs:attribute name = “account_number”/>
adding the attribute use = “required” means value must be specified
Key constraint: “account numbers form a key for account
elements under the root bank element:
<xs:key name = “accountKey”>
<xs:selector xpath = “]bank/account”/>
<xs:field xpath = “account_number”/>
</xs:key>
Foreign key constraint from depositor to account:
<xs:keyref name = “depositorAccountKey” refer=“accountKey”>
<xs:selector xpath = “]bank/account”/>
<xs:field xpath = “account_number”/>
</xs:keyref>
Database System Concepts - 5th Edition
10.104
©Sang Ho Lee
Querying and Transforming XML Data
Translation of information from one XML
schema to another
Querying on XML data
Above two are closely related, and handled by
the same tools
Standard XML querying/translation languages
XPath
Simple
language consisting of path expressions
XSLT
Simple
language designed for translation from XML to XML
and XML to HTML
XQuery
An
XML query language with a rich set of features
Database System Concepts - 5th Edition
10.105
©Sang Ho Lee
Tree Model of XML Data
Query and transformation languages are based on a
tree model of XML data
An XML document is modeled as a tree, with nodes
corresponding to elements and attributes
Element nodes have child nodes, which can be attributes or
subelements
Text in an element is modeled as a text node child of the
element
Children of a node are ordered according to their order in
the XML document
Element and attribute nodes (except for the root node) have
a single parent, which is an element node
The root node has a single child, which is the root element
of the document
Database System Concepts - 5th Edition
10.106
©Sang Ho Lee
Tree Model Example (1/2)
The XPath data model
XML documents consists of seven types
Root node
Elements
Attribute
Text
Namespace
Comment
Processing instruction
Database System Concepts - 5th Edition
<?xml-stylesheet type=“text/xsl”?>
<!-- comments go here -->
<amount vendor=“314”
xmlns=“urn:wyeast-net: invoice>
8989.00
10.107
</amount>
©Sang Ho Lee
Tree Model Example (2/2)
XML structured can viewed as tree with different
types of nodes
root
Processing instruction <?xml-stylesheet type=“text/xsl”?>
comment
<!-- comments go here -->
element
amount
vendor=“314”
attribute
namespace
text
Database System Concepts - 5th Edition
xmlns=“urn:wyeast-net: invoice>
8989.00
10.108
©Sang Ho Lee
XPath at a glance
XPath is used to address (select) parts of
documents using path expressions
A path expression is a sequence of steps
separated by “/”
Result of path expression: set of values that
along with their containing
elements/attributes match the specified path
Database System Concepts - 5th Edition
10.109
©Sang Ho Lee
XSchema on Bank2
<bank-2>
<account account_number=“A-401” owners=“C100 C102”>
<branch_name> Downtown
<balance>
500
</branch_name>
</balance>
</account>
<customer customer_id=“C100” accounts=“A-401”>
<customer_name> Joe
<customer_street> Monroe
<customer_city> Madison
</customer_name>
</customer_street>
</customer_city>
</customer>
<customer customer_id=“C102” accounts=“A-401 A-402”>
<customer_name> Mary
</customer_name>
<customer_street> Erin
</customer_street>
<customer_city> Newark </customer_city>
</customer>
</bank-2>
Database System Concepts - 5th Edition
10.110
©Sang Ho Lee
XPath Example
/bank-2/customer/customer_name
<customer_name>Joe</customer_name>
<customer_name>Mary</customer_name>
/bank-2/customer/customer_name/text( )
returns the same names, but without the
enclosing tags
Database System Concepts - 5th Edition
10.111
©Sang Ho Lee
XPath
The initial “/” denotes root of the document (above the
top-level tag)
Path expressions are evaluated left to right
Each step operates on the set of instances produced by the
previous step
Selection predicates may follow any step in a path, in [ ]
E.g. /bank-2/account[balance > 400]
returns account elements with a balance value greater than 400
/bank-2/account[balance] returns account elements containing a
balance subelement
Attributes are accessed using “@”
E.g. /bank-2/account[balance > 400]/@account_number
returns the account numbers of accounts with balance > 400
IDREF attributes are not dereferenced automatically (more on
this later)
Database System Concepts - 5th Edition
10.112
©Sang Ho Lee
Functions in XPath
XPath provides several functions
The function count() at the end of a path counts the number of
elements in the set generated by the path
E.g. /bank-2/account[count(./customer) > 2]
– Returns accounts with > 2 customers
Also function for testing position (1, 2, ..) of node w.r.t. siblings
Boolean connectives “and” and “or” and function “not()”
can be used in predicates
IDREFs can be referenced using function id()
id() can also be applied to sets of references such as IDREFS
and even to strings containing multiple references separated
by blanks
E.g. /bank-2/account/id(@owner)
returns all customers referred to from the owners attribute of account
elements.
Database System Concepts - 5th Edition
10.113
©Sang Ho Lee
More XPath Features
Operator “|” used to implement union
E.g. /bank-2/account/id(@owner) | /bank2/loan/id(@borrower)
Gives customers with either accounts or loans
However, “|” cannot be nested inside other operators
“//” can be used to skip multiple levels of nodes
E.g. /bank-2//customer_name
finds any customer_name element anywhere under the /bank-2
element, regardless of the element in which it is contained.
A step in the path can go to parents, siblings, ancestors
and descendants of the nodes generated by the
previous step, not just to the children
“//”, described above, is a short from for specifying “all
descendants”
“..” specifies the parent
doc(name) returns the root of a named document
Database System Concepts - 5th Edition
10.114
©Sang Ho Lee
XPath Overview
Location step
/
Description
The root node
element-name
text()
comment()
The name of an element
element’s text
a comment
@attribute-name
node()
*
@*
The name of an attribute
any node
any element name
any attribute name
Database System Concepts - 5th Edition
10.115
©Sang Ho Lee
XPath Example
Result of an XPath location: duplicate free set of nodes
/movies/movie: the set of all movie elements
/movies/movie/rating: the set of all rating elements
“<rating>3</rating>”, “<rating>2</rating>”
/movies/movie/title/@lang: the set of all language attribute names
<movies>
<movie>
<title>Man In Black</title>
<rating>3</rating>
</movie>
<movie>
<title lang=“en”>Batman</title>
<title lang=“ko”>Bateman</title>
<rating>2</rating>
</movie>
</movies>
Database System Concepts - 5th Edition
10.116
©Sang Ho Lee
XQuery
XQuery is a general purpose query language for XML data
Currently being standardized by the World Wide Web
Consortium (W3C)
The textbook description is based on a January 2005 draft of the
standard. The final version may differ, but major features likely to stay
unchanged.
XQuery is derived from the Quilt query language, which itself
borrows from SQL, XQL and XML-QL
XQuery uses a
for … let … where … order by …result …
syntax
for
SQL from
where SQL where
order by SQL order by
result SQL select
let allows temporary variables, and has no equivalent in
SQL
Database System Concepts - 5th Edition
10.117
©Sang Ho Lee
FLWOR Syntax in XQuery
For clause uses XPath expressions, and variable in for clause
ranges over values in the set returned by XPath
Simple FLWOR expression in XQuery
find all accounts with balance > 400, with each result enclosed in an
<account_number> .. </account_number> tag
for
$x in /bank-2/account
let
$acctno := $x/@account_number
where $x/balance > 400
return <account_number> { $acctno } </account_number>
Items in the return clause are XML text unless enclosed in {}, in which
case they are evaluated
Let clause not really needed in this query, and selection can be
done In XPath. Query can be written as:
for $x in /bank-2/account[balance>400]
return <account_number> { $x/@account_number }
</account_number>
Database System Concepts - 5th Edition
10.118
©Sang Ho Lee
Joins
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>
Database System Concepts - 5th Edition
10.119
©Sang Ho Lee
Nested 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
Database System Concepts - 5th Edition
10.120
©Sang Ho Lee
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_name
return
<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>
Database System Concepts - 5th Edition
10.121
©Sang Ho Lee
Functions and Other XQuery Features
User defined functions with the type system of XMLSchema
function balances(xs:string $c) returns list(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
Database System Concepts - 5th Edition
10.122
©Sang Ho Lee
XSL and XSLT
XSL is a standard consisting of two parts
XSLT (XSL Transformation): a scripting language
to manipulate XML documents
FO (Formatting Objects): a simple and effective
styling tool, similar to CSS
XSLT gives XML documents meaning
through transformation
FO gives XML documents meaning through
presentation
Database System Concepts - 5th Edition
10.123
©Sang Ho Lee
XSL and XPath
XSL
Declarative XML syntax
Template mechanism (output generated with templates)
Script Language (conditions, variables, etc.) with several
build-in functions
XPath
Used for locating information of XML source documents
Similar to file paths
(“/home/usr/marvin/.public_html/index.html”)
Database System Concepts - 5th Edition
10.124
©Sang Ho Lee
XSLT
XSL concept
Input transformed by XSLT Style Sheet (a program) into any type
of output document
XSL Processor
A runtime environment that can execute XSLT Style Sheets
XML
Document(s)
input
XSL
Processor
output
Document
(XML, HTML,..)
input
XSLT
Style Sheet
Database System Concepts - 5th Edition
10.125
©Sang Ho Lee
XSLT
Possible applications of XSLT
Transforming XML into (X)HTML for a web server
(often found in Content Management Systems)
Add content to an XML document, e.g., for presentation
(logo-image, disclaimer, …)
Create new content, e.g., a table of contents
Convert between different XML vocabularies to match a
target XML format (often found in application integration
software)
Database System Concepts - 5th Edition
10.126
©Sang Ho Lee
XSLT / Example
<?xml version="1.0"?>
<message>World
</message>
input
XSL
Processor
output
Hello World
input
<?xml version="1.0"?>
<stylesheet version="1.0"
xmlns="http://www.w3.org/1999/XSL/Transform">
<output method="text"/>
<template match="message">Hello <apply-templates/>
</template>
</stylesheet>
Database System Concepts - 5th Edition
10.127
©Sang Ho Lee
XSLT / Hello World Example
input
XSL
Processor
output
Messages:
Hello World
Hello Class
input
<?xml version="1.0"?>
<messages>
<message>World
</message>
<message>Class
</message>
</messages>
<?xml version="1.0"?>
<stylesheet version="1.0“ xmlns="http://www.w3.org/1999/XSL/Transform">
<output method="text"/>
<template match="messages">Messages:
<apply-templates/>
</template>
<template match="message">Hello <apply-templates/>
</template>
</stylesheet>
Database System Concepts - 5th Edition
10.128
©Sang Ho Lee
XSLT / Hello World
Structure of XML documents is a tree
Each element / text / attribute is a node
Matching is done with respect to a current node
Result of a match is a list of nodes
apply-templates applied recursive algorithm to resulting list of nodes (in
sequence of nodes)
Current node moves to next node in list
Matching starts with root node as current node
messages
Database System Concepts - 5th Edition
Current node
message
result of first match
(list of nodes)
message
Recursion on first node
as new current node
10.129
©Sang Ho Lee
XSLT / Hello World
Structure of XML documents is a tree
Each element / text / attribute is a node
Matching is done with respect to a current node
Result of a match is a list of nodes
apply-templates applied recursive algorithm to resulting list of nodes (in
sequence of nodes)
Current node moves to next node in list
Matching starts with root node as current node
messages
Current node
result of next match
messages
World
Default rule for text nodes:
Text is printed out
message
Class
Database System Concepts - 5th Edition
10.130
©Sang Ho Lee
XSLT / Hello World
Structure of XML documents is a tree
Each element / text / attribute is a node
Matching is done with respect to a current node
Result of a match is a list of nodes
apply-templates applied recursive algorithm to resulting list of nodes (in
sequence of nodes)
Current node moves to next node in list
Matching starts with root node as current node
messages
Current node
result of 3rd match
message
World
After recursion completes:
Current node moves to next
Node in previous result, etc.
message
Class
Database System Concepts - 5th Edition
10.131
©Sang Ho Lee
XSLT
A stylesheet stores formatting options for a document, usually
separately from document
E.g. an HTML style sheet may specify font colors and sizes for
headings, etc.
The XML Stylesheet Language (XSL) was originally designed for
generating HTML from XML
XSLT is a general-purpose transformation language
Can translate XML to XML, and XML to HTML
XSLT transformations are expressed using rules called templates
Templates combine selection using XPath with construction of
results
Database System Concepts - 5th Edition
10.132
©Sang Ho Lee
XSLT Templates
Example of XSLT template with match and select part
<xsl:template match=“/bank-2/customer”>
<xsl:value-of select=“customer_name”/>
</xsl:template>
<xsl:template match=“*”/>
The match attribute of xsl:template specifies a pattern in XPath
Elements in the XML document matching the pattern are processed by the
actions within the xsl:template element
xsl:value-of selects (outputs) specified values (here, customer_name)
For elements that do not match any template
Attributes and text contents are output as is
Templates are recursively applied on subelements
The <xsl:template match=“*”/> template matches all
elements that do not match any other template
Used to ensure that their contents do not get output.
If an element matches several templates, only one is used based on a
complex priority scheme/user-defined priorities
Database System Concepts - 5th Edition
10.133
©Sang Ho Lee
Creating XML Output
Any text or tag in the XSL stylesheet that is not in the xsl namespace
is output as is
E.g. to wrap results in new XML elements.
<xsl:template match=“/bank-2/customer”>
<customer>
<xsl:value-of select=“customer_name”/>
</customer>
</xsl;template>
<xsl:template match=“*”/>
Example output:
<customer> Joe </customer>
<customer> Mary </customer>
Database System Concepts - 5th Edition
10.134
©Sang Ho Lee
Creating XML Output (Cont.)
Note: Cannot directly insert a xsl:value-of tag inside another tag
E.g. cannot create an attribute for <customer> in the previous example
by directly using xsl:value-of
XSLT provides a construct xsl:attribute to handle this situation
xsl:attribute adds attribute to the preceding element
E.g. <customer>
<xsl:attribute name=“customer_id”>
<xsl:value-of select = “customer_id”/>
</xsl:attribute>
</customer>
results in output of the form
<customer customer_id=“….”> ….
xsl:element is used to create output elements with computed names
Database System Concepts - 5th Edition
10.135
©Sang Ho Lee
Structural Recursion
Template action can apply templates recursively to the contents of a
matched element
<xsl:template match=“/bank”>
<customers>
<xsl:template apply-templates/>
</customers >
</xsl:template>
<xsl:template match=“/customer”>
<customer>
<xsl:value-of select=“customer_name”/>
</customer>
</xsl:template>
<xsl:template match=“*”/>
Example output:
<customers>
<customer> John </customer>
<customer> Mary </customer>
</customers>
Database System Concepts - 5th Edition
10.136
©Sang Ho Lee
Joins in XSLT
XSLT keys allow elements to be looked up (indexed) by values of
subelements or attributes
Keys must be declared (with a name) and, the key() function can then
be used for lookup. E.g.
<xsl:key name=“acctno” match=“account”
use=“account_number”/>
<xsl:value-of select=key(“acctno”, “A-101”)
Keys permit (some) joins to be expressed in XSLT
<xsl:key name=“acctno” match=“account” use=“account_number”/>
<xsl:key name=“custno” match=“customer” use=“customer_name”/>
<xsl:template match=“depositor”>
<cust_acct>
<xsl:value-of select=key(“custno”, “customer_name”)/>
<xsl:value-of select=key(“acctno”, “account_number”)/>
</cust_acct>
</xsl:template>
<xsl:template match=“*”/>
Database System Concepts - 5th Edition
10.137
©Sang Ho Lee
Sorting in XSLT
Using an xsl:sort directive inside a template causes all elements
matching the template to be sorted
Sorting is done before applying other templates
<xsl:template match=“/bank”>
<xsl:apply-templates select=“customer”>
<xsl:sort select=“customer_name”/>
</xsl:apply-templates>
</xsl:template>
<xsl:template match=“customer”>
<customer>
<xsl:value-of select=“customer_name”/>
<xsl:value-of select=“customer_street”/>
<xsl:value-of select=“customer_city”/>
</customer>
<xsl:template>
<xsl:template match=“*”/>
Database System Concepts - 5th Edition
10.138
©Sang Ho Lee
Application Program Interfaces
There are two standard application program
interfaces to XML data:
DOM
SAX
Database System Concepts - 5th Edition
10.139
©Sang Ho Lee
DOM (Document Object Model)
XML data is parsed into a tree representation
Variety of functions provided for traversing the DOM
tree
For example, Java DOM API provides node class
with methods:
getParentNode( ), getFirstChild( ), getNextSibling( ),
getAttribute( ), getData( ) (for text node)
getElementsByTagName( ), …
Also provides functions for updating DOM tree
DOM can be used to access XML data, but does not
support any form of declararive querying
Database System Concepts - 5th Edition
10.140
©Sang Ho Lee
SAX (Simple API for XML)
To provide a common interface bet. parsers
and applications
SAX is built on the notion of event handlers,
which consists of user-specified functions
associated with parsing events
Users provide event handlers for parsing events
(for example, start of element, end of element)
Not suitable for database applications
Database System Concepts - 5th Edition
10.141
©Sang Ho Lee
Storage of XML Data
XML data can be stored in
Non-relational databases
Flat files
– Natural for storing XML
– But has all problems discussed in Chapter 1 (no concurrency, no
recovery, …)
XML databases
– Database built specifically for storing XML data, supporting DOM model
and declarative querying
– Early XML databases implemented the DOM on a C++-based objectoriented database
– Currently no commercial-grade systems
Relational databases
Data must be translated into relational form
Advantage: mature database systems
Disadvantages: overhead of translating data and queries
Database System Concepts - 5th Edition
10.142
©Sang Ho Lee
Storage of XML in Relational Databases
Alternatives:
String Representation
Tree Representation
Map to relations
Database System Concepts - 5th Edition
10.143
©Sang Ho Lee
String Representation (1)
Store each top level element as a string field (clob) of a
tuple in a relational database
Use a single relation to store all elements, or
Use a separate relation for each top-level element type
For example, account, customer, depositor relations (see Fig. 10.1)
– Each with a string-valued attribute to store the element, plus extra attributes
to store some subelements
Indexing:
Store values of subelements/attributes to be indexed as extra
fields of the relation, and build indices on these fields
E.g. customer_name or account_number
Some database systems support function indices, which use
the result of a function as the key value.
The function should return the value of the required
subelement/attribute
Database System Concepts - 5th Edition
10.144
©Sang Ho Lee
String Representation (2)
Benefits:
Can store any XML data even without DTD
As long as there are many top-level elements in a
document, strings are small compared to full
document
Allows
fast access to individual elements.
Drawback: Need to parse strings to access
values inside the elements
Parsing is slow
Database System Concepts - 5th Edition
10.145
©Sang Ho Lee
Tree Representation (1)
Tree representation: model XML data as tree and store using
relations
nodes(id, type, label, value)
child (child_id, parent_id)
bank (id:1)
customer (id:2)
account (id: 5)
customer_name
(id: 3)
account_number
(id: 7)
Each element/attribute is given a unique “identifier”
“type” indicates element/attribute
“label” specifies the tag name of the element/name of attribute
“value” is the text value of the element/attribute
The relation child notes the parent-child relationships in the tree
Can add an extra attribute to child to record ordering of children
Database System Concepts - 5th Edition
10.146
©Sang Ho Lee
Tree Representation (2)
Benefit: Can store any XML data, even
without DTD
Drawbacks:
Data is broken up into too many pieces,
increasing space overheads
Even simple queries require a large number of
joins, which can be slow
Database System Concepts - 5th Edition
10.147
©Sang Ho Lee
Mapping XML Data to Relations
Relation created for each element type whose schema is
known:
An id attribute to store a unique id for each element
A relation attribute corresponding to each element attribute
A parent_id attribute to keep track of parent element
As in the tree representation
Position information (ith child) can be store too
All subelements that occur only once can become relation
attributes
For text-valued subelements, store the text as attribute value
For complex subelements, can store the id of the subelement
Subelements that can occur multiple times represented in a
separate table
Similar to handling of multivalued attributes when converting ER
diagrams to tables
Variants of this approach is possible
Database System Concepts - 5th Edition
10.148
©Sang Ho Lee
Publishing and shredding XML data
Publishing: process of converting relational data
to an XML format
Shredding: process of converting an XML
document into a set of tuples to be inserted into
one or more relations
XML-enabled database systems support
automated publishing and shredding
Some systems offer native storage of XML data
using the “xml” data type. Special internal data
structures and indices are used for efficiency
Allow XQuery queries to be embedded within SQL
queries
Database System Concepts - 5th Edition
10.149
©Sang Ho Lee
SQL/XML
New standard SQL extension that allows creation of
nested XML output
Each tuple is mapped to an XML element “row”
Each relation attribute is mapped to an XML element
<bank>
<account>
<row>
<account_number> A-101 </account_number>
<branch_name> Downtown </branch_name>
<balance> 500 </balance>
</row>
more rows if there are more output tuples …
</account>
</bank>
Database System Concepts - 5th Edition
10.150
©Sang Ho Lee
SQL/XML
SQL/XML adds several operators and
aggregate operations to SQL to allow the
construction of XML output directly from the
extended SQL
xmlelement creates XML elements
xmlattributes creates attributes
xmlforest is similar to xmlattributes, except that it
creates a forest of subelements, instead of a list
of attributes
xmlagg for aggregate functions
Database System Concepts - 5th Edition
10.151
©Sang Ho Lee
SQL/XML Example (1)
select xmlelement(name “account”,
xmlattributes(account_number as account_number),
xmlelement(name “branch_name”, branch_name),
xmlelement(name “balance”, balance) )
from account
<account account_number=“A-401”>
<branch_name> Downtown </branch_name>
<balance> 500 </balance
</account>
<account account_number=“A-402”>
<branch_name> Perryridge </branch_name>
<balance> 900 </balance>
</account>
…
Database System Concepts - 5th Edition
10.152
©Sang Ho Lee
SQL/XML Example (2)
select xmlelement(name “branch”,
branch_name,
xmlagg(xmlforest(account_number)
order by account_number) )
from account
group by branch_name
This query creates an element for each branch,
containing as subelements all the account
numbers at that branch
Database System Concepts - 5th Edition
10.153
©Sang Ho Lee
XML Applications
Storing data with complex structure
Standardized data exchange formats
Web services
Data midiation
Database System Concepts - 5th Edition
10.154
©Sang Ho Lee
Web Services
The Simple Object Access Protocol (SOAP)
standard:
Invocation of procedures across applications with
distinct databases
XML used to represent procedure input and output
A Web service is a site providing a collection of
SOAP procedures
Described using the Web Services Description
Language (WSDL)
Directories of Web services are described using the
Universal Description, Discovery, and Integration
(UDDI) standard
Database System Concepts - 5th Edition
10.155
©Sang Ho Lee