XML and Data Management
For: Dr. Haddouti
Storing XML using Relational Model
a. Why Store XML
b. Where Store XML?
2. XML Storage Requirements.
3. Strategies of Storing XML.
4. Mapping XML to Relational Model.
5. XML enabled databases:
a. Microsoft SQL Server 2000.
b. Oracle 8i/9i.
c. IBM DB2.
XML is becoming the standard for the exchange of information from Business to Business.
And the amount of information exchanged using XML grows very fast. But why do we need
to store XML?
The answer to this question is:
- XML documents are conceived as transitory form of data.
- XML is not designed to facilitate efficient data retrieval and storage.
- Processing and accessing data in large XML files is a time consuming process.
- XML data should be stored in a consistent and efficient manner to enforce data
XML documents can be stored either into an RDBMS, an ODBMS or a Native XML
The focus of this paper is on the relational database model.
2. XML Storage Requirements:
XML documents are either: Data Centric, Document Centric or Semi structured.
a. Data Centric Documents:
An XML document is data centric if it has a regular structure and contains
updateable data (e.g. invoice documents, purchase orders, flight schedules).
“Traditional relational databases are typically better at dealing with data centric
The technique that is appropriate for storing data centric XML documents is
mapping the structure of the XML document to the database: store the XML
document contents into relational tables. 
An Example of data centric document:
b. Document Centric Documents:
Document centric data tends to be more unpredictable in size and content (e.g.
newspaper content, articles, and advertisements).
Native XML databases and content management systems are typically better at
storing document centric data.
A content management system is an application designed to manage documents
and it can be built on top of a native XML database. 
c. Semi Structured Documents:
The difference between data centric and document centric documents is not always
easy to identify. Semi structured documents are combination of data centric and
document centric. 
An example of semi structured document:
<description>Sent to a small Alaska town to investigate the murder of a
teenage girl, a veteran police officer (Al Pacino) is forced…
</ description >
3. Strategies for Storing XML:
The storage of XML can be achieved in three different ways: structure based storing,
model based storing and text based storing.
a. Structure Based Storing (e.g. STORED, POET):
In structure based storing, the database schemas represent the logical structure of the
XML document (or DTDs if they are available). Therefore, a relation or a class is
created for each element type in the XML documents. 
Take the example of STORED (Semi-structured TO Relational Data). STORED is a
query language that does the mapping between XML (semi-structured data) and
relational data. This mapping is generated automatically using data mining techniques.
Mapping with STORED is lossless because an overflow graph is used to store the data
that does not fit into the schema. After the STORED process is finished, the system
becomes ready for insert and update in the database. 
b. Model Based Storing (e.g. Excelon’s Xis, Infonyte):
In model based storing, a fixed database schema is used to store the structure of all
XML documents. 
Take the example of Xis. Xis is a native XML database that stores XML data directly
as Document Object Model (DOM) trees. It makes changes in the structure and data of
XML documents dynamically and in real time by processing only those XML
elements or sub-elements needed for a particular business process or transaction. 
c. Text Based Storing:
Text based storing / method 1:
Store the entire document as text, such as a Binary Large OBject (BLOB) into the
relational database. This strategy is appropriate for document centric data.
All leading RDBMS vendors support storing the entire document (Microsoft SQL
Server 2000, Oracle8i, IBM DB2). This method is simple but it does not allow flexible
indexing and searching of data. 
Text Based storing / method 2:
Store the entire document in the file system with a pointer to that file stored in the
database. This method is useful if the number of XML documents is small and
infrequently updated and it is supported by leading database vendors. But it has
problems: (1) It is not very flexible for storing and retrieving data (2) has the problem
of security since the files are stored outside the database. 
4. Mapping XML to Relational Model:
The strategy that is the most used for storing XML into the Relational BD is mapping
the XML document structure to the database which means that the XML data is stored
into relational tables (called side tables).
This method allows easy search, update and retrieval of the data stored in the database.
The problem here is how to do the mapping between and the database knowing that
the relational database model is based on primary keys, foreign keys, tables, rows and
columns. Obviously, XML is built on a different principle.
The differences between XML and relational databases can be summarised as follows:
Therefore the mapping between XML and relational model can be achieved as
Mapping Example: 
This XML document can be mapped into the following tables:
Number Customer Date
----------- --------------------------- --------------
1234 Gallagher Industries 29.10.00
... ... ... ... ... ...
SONumber Item Part Quantity Price
--------------- ------ ------ ----------- -------
1234 1 A-10 12 10.95
1234 2 B-43 600 3.99
... ... ... ... ...
Problems with mapping:
There are many problems associated with the mapping between semi-structured
documents and relational tables. The first one is that the mapping between XML and
the DB is not done in a natural way. Second, the
XML documents have to be cut into pieces and inserted into the relational tables.
Third to query the database, tables should be joined, and with large tables, this process
becomes time consuming. And finally, it takes time to restore the entire XML
The solution for these problems associated with relational model is to use Native XML
5. XML Enabled Databases:
Various Database vendors have developed efficient solutions for automatic conversions of
XML into and out of relational databases: Microsoft's SQL Server 2000,
Oracle 8i, Oracle 9i and IBM DB2.
The tools and techniques offered to achieve retrieval and storage of the XML data vary.
a. Microsoft SQL Server 2000:
SQL server 2000 introduced many features to deal with the storage and retrieval of
- SELECT statement result sets can be mapped into XML documents by using
FOR XML keyword.
- XML documents can be stored into the database using OPENXML. 
Retrieving XML documents from the database using FOR XML:
In General, the SQL code that generates an XML document from the database is:
FOR XML AUTO | RAW | EXPLICIT [, XMLDATA] [, ELEMENTS] [, BIN
FOR XML – example: 
Assume we have these two tables in the SQL database:
OrderID ProductID UnitPrice Quantity
10248 11 14 12
10248 42 9,8 10
10248 72 34,8 5
10249 15 5 2
Using this SELECT statement we will construct an XML document that will contain
information about an order (create an invoice):
SELECT Invoice.OrderID InvoiceNo,
FROM Orders Invoice JOIN [Order Details] Item
ON Invoice.OrderID = Item.OrderID
WHERE Invoice.OrderID = 10248
FOR XML AUTO, ELEMENTS
The SELECT statement gives the following result:
- AUTO allows you to rename the attributes in the XML document. The default is the
name of the table.
-Queries executed using RAW mode return an XML element for each row in the
- EXPLICIT is used to produce both elements and attributes.
- XMLDATA is optional and used only with EXPLICIT.
- ELEMENTS instructs the creation of sub-elements instead of attributes.
- BINARY is used to extract binary data (such as an image) in the XML document.
Storing XML documents into the database using OPENXML:
The storing is done in three steps:
1. Compiling the XML document into internal DOM representation to obtain an
“XML document handler” using the stored procedure sp_xml_preparedocument.
2. Creating a database schema with OPENXML.
3. Removing the compiled XML document from memory using the stored procedure
OPENXML – Example: 
DECLARE @idoc int
DECLARE @doc varchar(1000)
SET @doc =‘
<Customer CustomerID="VINET" ContactName="Paul Henriot">
<Order CustomerID="VINET" EmployeeID="5"
<OrderDetail OrderID="10248" ProductID="11" Quantity="12"/>
<OrderDetail OrderID="10248" ProductID="42" Quantity="10"/>
<Customer CustomerID="LILAS" ContactName="Carlos Gonzlez">
--Create an internal representation of the XML document.
EXEC sp_xml_preparedocument @idoc OUTPUT, @doc
-- Execute a SELECT statement that uses the OPENXML rowset provider.
SELECT *FROM OPENXML (@idoc, '/ROOT/Customer',1)
WITH (CustomerID varchar(10), ContactName varchar(20))
EXEC sp_xml_removedocument @idoc
The result set is:
VINET Paul Henriot
LILAS Carlos Gonzlez
Storing XML documents into the SQL database uses OPENXML (for insert and
OPENXML is an extension to transact-SQL that provides a rowset.
OPENXML(idoc int [in], rowpattern nvarchar[in], [flags byte[in]])
[WITH (SchemaDeclaration | TableName)]
- idoc is the document handle of the internal representation of an XML document
created by calling sp_xml_preparedocument.
- rowpattern is the XPath pattern used to identify the nodes.
- flags Indicates the mapping that should be used between the XML data and the
relational rowset. 
SQL: mapping XML schema into DB schema:
One way to map an XML schema to a relational schema (and the opposite) is to
transform the XML schema into a mapping schema.
It is the mapping schema that allows the mapping of elements and attributes to tables
and columns and the retrieval of relational data as XML documents.
XML View Mapper utility is a utility for SQL Server 2000 that allows the creation of
an XDR (XML Data Reduced) schema from relational tables. it also allows the
creation of a mapping schema corresponding to the XML schema. XML View Mapper
also generates XDR schema from an XML document or create schema from DTD.
NB: XDR schemas were created by Microsoft to be used with their products.
Example of mapping schema:
<?xml version="1.0" ?>
<ElementType name="Employee“ sql:relation="Employees">
<AttributeType name="EmpID" />
<AttributeType name="FName" />
<AttributeType name="LName" />
<attribute type="EmpID" sql:field="EmployeeID" />
<attribute type="FName" sql:field="FirstName" />
<attribute type="LName" sql:field="LastName" />
The bolded elements are what makes this schema a mapping schema.
b. Oracle 8i - 9i:
Oracle offers XML SQL utility (a set of Java classes) that:
– models XML document elements as a collection of nested tables and allows
insert, update and delete.
– generates XML documents from SQL query results or a JDBC result set
Extracting XML documents from the database:
We want to get the result set of the following SQL statement into an XML document:
SELECT Title, Author, Publisher, Year, ISBN
FROM BookList WHERE BookID = 1234
XML utility can be used to generate a DTD based on the schema of the underlying
table being queried.
The following simple code allows the creation of an XML document that contains the
result set of the query:
public class read_samp1e
public static void main(String args) throws SQLException
String user = "scott/tiger";
//init a JDBC connection by passing in the user
Connection conn =
// init the OracleXMLQuery by using the initialized JDB connection
and passing in "Booklist" as tabName
OracleXMLQuery qry = new OracleXMLQuery(conn,"select * from
Booklist WHERE BookID = 1234");
// get the XML document in the string format which allows us to print it
String xmlString = qry.getXMLString();
// print out the result to the screen
System.out.println(" OUPUT IS:n"+xmlString);
// Close the JDBC connection
The query results in the creation of the following XML document:
<PUBLISHER>William Morrow and
Storing XML documents into the database:
Storing the XML document into the database uses the following instructions:
//Init the OracleXMLSave class:
OracleXMLSave sav = new OracleXMLSave(conn, Booklist);
Remark: XSU (XML SQL) utility does not allow the storage of attributes. These have
to be transformed into elements. 
Oracle: mapping XML schema into DB schema:
The Oracle 9i Release 2 has a new feature called XML DB Repository. This repository
allows storage of XML documents directly in Oracle9i Database provided an XML
schema. Once the XML schema is registered, the XML-to-relational database mapping
is done automatically. After storage of XML data into the RDB, you can restore an
XML document with the same DOM representation of the original XML document.
Oracle 9i – XML Type: 
Oracle9i Database implements a number of standards-based functions to query
relational data and return XML documents: XMLType, XMLELEMENT…
XML or XMLType is a datatype to hold XML data.
XMLType view contains the result of an SQL query in the form of an XML document.
CREATE OR REPLACE VIEW STUDENT
OF XMLType WITH …
c. IBM DB2: 
IBM DB2 offers DB2 XML Extender. It allows the storage of XML documents in two
– XML Column: allows the storage and retrieval of the entire XML document
as a column data.
– XML Collection: decomposes/composes the XML document into/from a
collection of relational tables.
Document Access Definition:
DB2 XML Extender provides a mapping scheme called a Document Access Definition
(DAD). DAD is a file that allows an XML document to be mapped into relational data
using either XML columns or XML Collections using the DTDs.
A unique feature of DB2 is the ability to manage and index XML documents located
within the file system, a single column, or spread across multiple tables and columns.
Storing XML into relational model is a huge and very important topic. We saw that
there are many techniques to store XML documents among these are text based
storage and the mapping between XML and relational model.
Text based model is simple but it does not allow very flexible search and update.
Mapping XML into relational model is the most popular way to store XML, it allows
flexible manipulation and search of data but it has few problems due to the differences
between the structure of XML and relational data. Despite these problems, I think that
this method is the most appropriate for XML storage especially for data centric
Oracle 9i, Oracle 8i, Microsoft SQL server 2000 and IBM DB2 all support storage of
Oracle uses XML SQL Utility (XSU), SQL server uses OPENXML and FOR XML
keywords in the SQL statements and DB2 uses DAD.
I think the leading database is Oracle 9i that has added very important features to
make easier to do the mapping.