Вы находитесь на странице: 1из 30

Lecture 12: XML and Databases

Motivating questions:
Why use XML to store data?
What is XML?
What are XML databases?

Connelly & Begg Ch 31


Why use XML?
XML is platform independent and programming language
independent.
XML is a simple text-based language.
XML support has become standard in browsers,
application servers, and databases.
XML schema can be used to create data models.
Relational, Object-oriented and native XML databases are
being designed with built-in, high performance storage and
retrieval technologies for XML data.
What is XML - 1?
XML stands for EXtensible Markup Language
XML looks a lot like HTML with tags, attributes and values.
HTML is a language for creating web pages.
XML is a meta-language a language to describe other
languages.
XML was designed to transport and store data, not to display
data
HTML was designed to display data.
XML tags are not predefined. You must define your own tags
XML is designed to be self-descriptive


What is XML - 2?
The tags in XML documents describe metadata -- data
that describes data (a bit like columns in a relational table).
The tags in XML documents surround the actual data and
can contain attribute values.
Anyone can design their own XML document with their
own tags to define the attribute and data types.
XML Does Not DO Anything
XML does not DO anything. XML was created to
structure, store, and transport information.
The following example is a note to Tove, from Jani, stored
as XML:

XML Does Not DO Anything
The note has sender and receiver information, it also has a
heading and a message body.
But still, this XML document does not DO anything. It is
just information wrapped in tags. Someone must write a
piece of software to send, receive or display it.
XML is used in many aspects of web development, often
to simplify data storage and sharing.

XML Separates Data from HTML
The first line is the XML declaration. It defines the XML
version (1.0) and the encoding used (ISO-8859-1 = Latin-
1/West European character set).
The next line describes the root element of the document
(like saying: "this document is a note"):
1
st
line
2
nd
line
4 child elements
of the root

End of root element
XML Documents Form a Tree
Structure
XML documents must contain a root element. This
element is "the parent" of all other elements.
The elements in an XML document form a document tree.
The tree starts at the root and branches to the lowest level
of the tree.
All elements can have sub elements (child elements):
<root>
<child>
<subchild>.....</subchild>
</child>
</root>
XML example
The root element in the example is <bookstore>. All
<book> elements in the document are contained
within <bookstore>.
The <book> element has 4 children: <title>,<
author>, <year>, <price>.
What is XML Schema?
XML Schema is a XML language to define what a given
set of one or more XML documents can look like:
What tags (or elements) they contain
What the ordering of tags is
What attributes the tags contain
An XML document written in the XML Schema language
is known as a schema.
A schema defines the meta-data for a set of XML
documents.
There are many schema specifying particular data
structures: MathML, CML.
XML Schema example - 1
<?xml version=1.0?>
<xsd:schema xmlns:xsd=http://www.w3.org/2000/10/XMLSchema>

<xsd:element name=Person type=studentType/>
<xsd:complexType name=studentType>
<xsd:sequence>
<xsd:attribute name="kind" type="xsd:string" use="required"/>
<xsd:element name=FirstName type=xsd:string/>
<xsd:element name=SecondName type=xsd:string/>
<xsd:element name=Module type=moduleType
maxOccurs=6/>
</xsd:sequence>
</xsd:complexType>
XML Schema example - 2
Schema example continued:

<xsd:complexType name=moduleType>
<xsd:sequence>
<xsd:element name=Coursework type=xsd:string maxOccurs=4
minOccurs=0/>
</xsd:sequence>
</xsd:complexType>

<!-- comments -->
</xsd:schema>

XML Data Manipulation
XPath is a syntax use to specify the location of tags or
elements in a XML document. Used in XQuery and
XSLT.
XQuery is a query language for XML.
XQuery example:

Document(People.xml)/Person[1]/FirstName

XML Databases - 1
An XML document is a database as it is a collection of
data.
As a "database" format, XML has some advantages:
Self-describing (the markup describes the structure and type names
of the data, although not the semantics)
Portable (text as Unicode)
It can describe data in tree or graph structures.
It can represent graphical and speech data and formatting files.
Disadvantages:
Verbose
Access to the data is slow due to parsing and text conversion.
XML DBMS - 1
Could XML and its surrounding technologies
constitute a database?
XML provides:
storage (XML documents)
Data models as schemas (XML Schemas)
query languages (XQuery, XPath, etc.)
programming interfaces (SAX, DOM, JDOM).
XML DBMS - 2
Does XML and its surrounding technologies
constitute a DBMS?
XML lacks:
efficient storage
Indexes
Security
transactions and data integrity
multi-user access
Triggers
queries across multiple documents.
XML Databases Storage Types
XML Document storage is characterized as
either data-centric or document-centric
Data-centric:
The data from XML documents is mapped into one or
more tables in XML-enabled RDBMS.
Document-centric:
XML documents are stored as XML documents in
native XML databases.
Native XML Databases -
Advantages
A XML-enabled database uses XML queries (XPath) as an
add on to SQL. A query is:
converted into SQL
may use slow joins to access similar data spread across a number
of tables.
Native XML databases store entire XML documents
physically together. This allows the documents to be
retrieved faster than relational databases which have data
spread across a number of tables. There is no query
conversion required.
Native XML Databases -
Disadvantages
The increased speed applies only when retrieving data in
the order it is stored on disk. Retrieving a different view of
the data might be slower.
Most Native XML databases can only return the data as
XML. If an application needs the data in another format it
must parse the XML before it can use the data.
Oracle and XML
Native Oracle XML datatype, called
XMLType, can be used as a column type.
Can perform:
Queries on XMLType as well as standard
XML operations.
XPath searches used within SQL queries.
Oracle and XML in Tables
Creating a Table with an XMLType Column

CREATE TABLE newPerson (
Name varchar (10) primary key,
PersonalDetails XMLType
);

Creating a Table of XMLType:
CREATE TABLE XMLDocuments OF XMLType;
Oracle and XML Insert
INSERT INTO newPerson VALUES ( Chang, XMLType( '
<Person kind=student>
<FirstName>Chris</FirstName>
<SecondName>Chang</SecondName>
<Module>IDBE</Module>
<Coursework>Completed</Coursework>
<Module>InternetProgramming</Module>
</Person> ')
);
Oracle and XML Queries 1 11g
SQL queries on part of or the whole XMLType: using
functions existsNode(), XMLExists(), XMLQuery(),
XMLSerialize(), XMLCast()
XMLQuery can return the text value of one tag

SELECT XMLQuery('/Person/Module[1]/text()' PASSING
PersonalDetails RETURNING CONTENT) FROM
newPerson
WHERE XMLExists('/Person/FirstName[node()="Chris"]'
PASSING PersonalDetails) AND
XMLExists('/Person/SecondName[node()="Chang"]'
PASSING PersonalDetails);
Oracle and XML Queries 2 11g
XMLQuery can also return tags

SELECT XMLQuery('/Person/Module' PASSING
PersonalDetails RETURNING CONTENT) FROM
newPerson
WHERE XMLExists('/Person/FirstName[node()="Chris"]'
PASSING PersonalDetails) AND
XMLExists('/Person/SecondName[node()="Chang"]'
PASSING PersonalDetails);
Oracle and XML Update - 1
Replacing XMLType with a new XMLType

UPDATE newPerson SET PersonalDetails = XMLType( '
<Person kind="student">
<FirstName>Chris</FirstName>
<SecondName>Chang</SecondName>
<Module>Software Development and Implementation
2</Module>
<Module>System Software</Module>
</Person> ')
WHERE XMLExists('/Person/FirstName[node()="Chris"]' PASSING
PersonalDetails) AND
XMLExists('/Person/SecondName[node()="Chang"]' PASSING
PersonalDetails)';
Oracle and XML Update - 2
Updating XML tag (or node) values

UPDATE newPerson
SET PersonalDetails = updateXML(PersonalDetails,
'/Person/Module[2]',
XMLType('<Module>ICT</Module>'))
WHERE XMLExists('/Person/FirstName[node()="Chris"]'
PASSING PersonalDetails) AND
XMLExists('/Person/SecondName[node()="Chang"]'
PASSING PersonalDetails);
Oracle and XML Update - 3
Deleting nodes from XMLType

UPDATE newPerson
SET PersonalDetails = deleteXML(PersonalDetails,
'/Person/Module[2]')
WHERE XMLExists('/Person/FirstName[node()="Chris"]'
PASSING PersonalDetails) AND
XMLExists('/Person/SecondName[node()="Chang"]'
PASSING PersonalDetails);
Oracle and XML Update - 4
inserting nodes into existing XMLType

UPDATE newPerson
SET PersonalDetails =
insertXMLbefore(PersonalDetails,
'/Person/Module[1]',
XMLType('<Module>IDBE</Module>'))
WHERE XMLExists('/Person/FirstName[node()="Chris"]'
PASSING PersonalDetails) AND
XMLExists('/Person/SecondName[node()="Chang"]'
PASSING PersonalDetails);
Oracle and XML Validation
Creating an XMLType Table column that conforms to an
XML Schema:

CREATE TABLE Person (
name varchar (10) primary key,
personalDetails XMLType)
XMLTYPE COLUMN personalDetails
XMLSCHEMA
"http://www.doc.ntu.ac.uk/xsd/Person.xsd" ELEMENT
Person;
What You Should Now Know
What XML is.
What the difference between XML-enabled
and native XML databases is.
How to define XMLTypes in a table.
How to insert XML into an XMLType.
How to select, update and delete
XMLTypes.

Вам также может понравиться