XML Ora
XML Ora
What's the best way of exchanging data between different sources without worrying about how the receiver will use
it? What's the best way of creating documents with the right content without worrying how it should be displayed on
the web and then able to display them with all the flexibility one could get? Welcome to the world of XML and its
family of technologies.
This whitepaper is aimed at understanding XML and related topics viz. XSL, DTD, DOM, SAX and Schemas. It also
looks at some of the products and tools from Oracle that supports XML through PL/SQL. Please note that a
downloadable version of this paper and the associated presentation are available at
www.quovera.com/forum/index.html
<?xml version='1.0'?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl">
<xsl:template match="/">
<HTML>
<BODY>
<h1>Employee Details</h1>
<xsl:for-each select="Employees/Empl">
<b>Empl #
<xsl:value-of select="@id" /> </b>
<i>First Name :
<xsl:value-of select="FirstName" /> </i>
<i>Last Name :
<xsl:value-of select="LastName" /> </i>
<i>Dept :
<xsl:value-of select="Dept" /> </i>
</xsl:for-each>
</BODY>
</HTML>
</xsl:template>
</xsl:stylesheet>
Listing 2: Employee.xsl
Figure 1 shows the way the browser interprets the Employee.xml document when combined with the Employee.xsl
document.
DTD
DTD (Document Type Definition) is a set of rules or grammar that we define to construct our own XML rules (also called
a "vocabulary"). In other words, a DTD provides the rules that define the elements and structure of our new
language.
This is comparable to defining table structures in Oracle for a new system. As we define the columns of a table,
determine the datatypes of the columns, determine if the column is 'Null' allowed or not, the DTD defines the
structure for the XML document. Listing 3 is an example of a basic DTD. The detailed syntax of DTD is covered
later in the paper.
<Employees>
<Empl>
<FirstName>
</FirstName>
<LastName>
</LastName>
<Dept>
</Dept>
</Empl>
</Employees>
Listing 3: Employee DTD
DOM
The Document Object Model (DOM) is a simple, hierarchical naming system that makes all of the objects in the
page, such as text, images, forms etc accessible to us. It is merely a set of plans that allow us to reconstruct the
document to a greater or lesser extent.
By definition, a complete model is one that allows us to reconstruct the whole document down to the smallest detail.
An incomplete DOM is anything less than that.
For the reader's information, the W3 DOM recognizes seventeen types of node objects for XML: Attribute,
CDATASection, Comment, DOMImplementation, Data, Document, DocumentType, DocumentFragment, Element,
Entity, EntityReference, NamedNodeMap, Node, NodeList, Notation, ProcessingInstruction, Text
For a detailed description of other node types, the reader is encouraged to visit the W3 web site at
http://www.w3.org/TR/WD-DOM/object-index.html.
SAX
Simple API for XML (SAX) is one of the two basic APIs for manipulating XML. It is used primarily on the server
side because of its characteristics of not storing the entire document in memory and processing it very fast. However,
SAX should be used mainly for reading XML documents or changing simple contents. Using it to do large-scale
manipulations like re-ordering chapters in a book or any such activities will make it extremely complicated, not that it
cannot be done.
SCHEMA
It’s a mechanism by which rules can be defined to govern the structure and content relationship within a document.
XML Schema Structures specifies the XML Schema definition language, which offers facilities for describing the
structure and constraining the contents of XML 1.0 documents. The schema language, which is itself represented in
XML 1.0 and uses namespaces, substantially reconstructs and considerably extends the capabilities, found in XML 1.0
document type definitions (DTDs). This specification depends on XML Schema Part 2: Datatypes.
XML Schema Datatypes is part 2 of the specification of the XML Schema language. It defines facilities for defining
datatypes. The datatype language, which is itself represented in XML 1.0, provides a superset of the capabilities found
in XML 1.0 document type definitions (DTDs) for specifying datatypes on elements and attributes.
NAMESPACES
With XML namespaces developers can qualify element names uniquely on the Web and thus avoid conflicts between
elements with the same name. The association of a Universal Resource Identifier (URI) with a namespace is purely to
ensure that two elements with the same name can remain unambiguous; no matter what the URI points to.
XML IN ORACLE
In order to see and appreciate the implementation of XML in Oracle, we need to have the necessary products and
components installed. The next section briefly looks at the products that are required.
A ROUND TRIP EXAMPLE
WHAT DO WE WANT TO ACHIEVE?
To enjoy all the benefits provided by XML (and XSL, DTD etc.), the least we should be able to do are:
Read data from the database and convert them into an XML document.
Output the XML documents in the appropriate device (we will restrict ourselves to displaying the output in a
browser).
Read XML document and insert the data contained in it into the table in the database.
For any real life application, the first step would be to design the database table/s and the corresponding DTD.
Thereafter, an XSL document will be required for displaying the resultant XML document meaningfully. The
application code to do all these manipulations will then follow.
To start with, lets consider a "Zipcodes" database table with the structure as shown in Table 1:
• Each record in the "Zipcodes" table represents a complete mapping of zip code, the extra four digits of zip
code, the city name and the state abbreviation.
As a first pass at a DTD, we'll create the tags <Zipcodes>, <mappings>, etc., specifying the relationships among
items we just outlined. Before we do that, we'll discuss the basics of DTD syntax.
DTD BASICS
Each statement in a DTD uses the <!XML DTD> syntax. This syntax begins each instruction with a left angle
bracket and an exclamation mark, and ends it with a right angle bracket.
>
<!ELEMENT zip_code_extn (#PCDATA)>
<!ELEMENT city (#PCDATA)>
<!ATTLIST city
city CDATA #REQUIRED
>
Listing 5: Extended DTD
The "#REQUIRED" keyword in the attribute definition means that this attribute must be coded for each and every
<state-abbreviation>, <zipcode> and <city> tag in our document. For attributes that are not required, we can use
the #IMPLIED keyword.
<?xml version='1.0'?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl">
<xsl:template match="/">
<HTML>
<BODY>
<TABLE BORDER="2">
<TR>
<TD>Zipcode</TD>
<TD>Zip Code Extn</TD>
<TD>City</TD>
<TD>State Abbreviation</TD>
</TR>
<xsl:for-each select="Zipcodes/mappings">
<TR>
<TD><xsl:value-of select="ZIPCODE"/></TD>
<TD><xsl:value-of select="ZIP_CODE_EXTN"/></TD>
<TD><xsl:value-of select="CITY"/></TD>
<TD><xsl:value-of select="STATE_ABBREVIATION"/></TD>
</TR>
</xsl:for-each>
</TABLE>
</BODY>
</HTML>
</xsl:template>
</xsl:stylesheet>
Listing 6: XSL Document for the zipcodes XML
This XSL document defines a table with double-lined borders for displaying the XML "zipcodes" document. Note
the headers for the columns and the columns themselves are defined in a different sequence than the one in the XML
document and the corresponding table. This XSL document is generic and will accommodate any number of rows
("mappings") in the XML document. We will see this XSL document in action very soon.
declare
xmlString CLOB := null;
amount integer:= 1000;
position integer := 1;
charString varchar2(1000);
fileHandle UTL_FILE.FILE_TYPE;
begin
--we want the row element to be named "mappings" to follow our DTD structure
xmlgen.setRowTag('mappings');
--set the id attribute in the ROW element to be Record - so that it shows the number
--of records fetched
xmlgen.setRowIdAttrName('Record');
--This gets the XML out - the 0 indicates no DTD in the generated XML document
--a value of 1 will provide a DTD description in the XML document
xmlString := xmlgen.getXML('select * from scott.zipcodes',0);
exception
when no_data_found then
-- end of fetch, free the lob
dbms_lob.close(xmlString);
dbms_lob.freetemporary(xmlString);
xmlgen.resetOptions;
utl_file.fclose(fileHandle);
when others then
xmlgen.resetOptions;
end;
/
Listing 8: Code for creating the XML document from rows in the zipcodes table
Important Note: The above code will create an XML document in the file system. In order to make it display the
output in a browser, this code can be modified to use the 'htp' functions and the call can be made from a Web Server.
Rem PL/SQL code to read an XML file and write the data to the database table
Rem "zipcodes"
Rem It uses UTL_FILE package to access the XML file from the file system
Rem Remember to give access to the directory (D:\test in this example) in the file
Rem system in init.ora
Rem Remember to set the serveroutput to on
declare
charString varchar2(80);
finalStr varchar2(4000) := null;
rowsp integer;
v_FileHandle UTL_FILE.FILE_TYPE;
begin
loop
BEGIN
utl_file.get_line(v_FileHandle, charString);
exception
when no_data_found then
utl_file.fclose(v_FileHandle);
exit;
END;
dbms_output.put_line(charString);
if finalStr is not null then
finalStr := finalStr || charString;
else
finalStr := charString;
end if;
end loop;
CONCLUSION
We have seen what Oracle tools are required to handle XML documents - to read into and out of the database. We
used a simple table to see and understand the construction of DTDs. We used the DTD logic to define an XSL
document for the resultant XML. With the help of PL/SQL codes we saw how records from a table can be converted
to XML documents and how data embedded in an XML document can be written back to the table. These basic
models can be extended to work with complex tables with complex business rules. This will enable XML and the
database to work closely together to facilitate the acquisition, integration, repurposing and exchange of data between
enterprises.
Quovera provides strategy, systems integration and outsourced application management to Fortune
500, high-growth middle market and emerging market companies. The firm specializes in delivering
intelligent solutions for complex enterprises, which improve productivity within the customer's business
and optimize the customer's value chain, through integration of its customers and suppliers. The
company also outsources the management of "best of breed" business applications on a recurring
revenue basis. Quovera refers to its business model as "Intelligent – Application Integration and
Management.
http://www.quovera.com