Sei sulla pagina 1di 51

Oracle9i XML Developer's Kits

XML Enhancements 1- 1
Objectives

After this lesson, you should be able to:


• Identify the new XDK Components in Oracle9i
• Identify the features of the new XDK
Components
• Identify the Uses of the new XDK Components

XML Enhancements 1- 2
Overview

• XML is rapidly becoming the standard for


information exchange on the Internet.
• The ability to read/write XML data to and from
databases and integrate this data into existing
applications is a key requirement for any
e-commerce application.
• XDK is designed for building database applications
that can read/write data formatted as XML just as
easily as reading or writing data in any other form.

Reference
Note: For more information on these topics please refer to
• Oracle9i XML Users guide
• Oracle9i Intermedia module
• Oracle9i Business Intelligence Module

XML Enhancements 1- 3
What are the Oracle9i XDK Components?

XML components, libraries and utilities


• Implementing XML standards-based
technologies
• Supporting deployment architecture flexibility
• Supporting multiple languages/platforms
• Implemented with enterprise scalability and
performance
• With comprehensive application and server
support

Deployment Architecture Flexibility


The Oracle XDK components are designed for flexible deployment to optimize performance and
scalability. The XDK for Java can be loaded into the Oracle9i JVM, run on the middle tier in iAS
or on the client. The XDKs for C, C++, are linked into the kernel and are available as standalone
libraries. The XDK for PL/SQL can be used through either the database or iAS through
mod_plsql. This architectural flexibility along with XML’s independence from specific protocols
gives application developers powerful functionality.
Support for Multiple Languages/Platforms
The XML components have been packaged into XDKs based upon their languages. The Java
components are platform independent having a JDK 1.1.6 requirement while the JavaBeans XDK
additionally requires Swing. The PL/SQL XDK was previously built upon the Java one but is
using the C XDK in Oracle9i. The platform dependent C and C++ XDKs are being ported to all
Oracle9i platforms.
Scalability and Performance
In the Oracle XDKs we have focused on not only providing a conformant set of components, but
on delivering enterprise level performance and scalability as well. This requirement is important
because of the need to deliver on-demand rendering of content in XML as well as providing
client-server performance which in many cases was the previous application architecture.
To this end we have incorporated numerous strategies including caching content on the middle
tier through Oracle iCache, building into our XSL processor the ability to cache stylesheets, and
output DOM objects ready to be transformed directly from SQL queries. To our XSQL servlet we
have also added integrated load balancing and connection pooling to enhance scalability.

XML Enhancements 1- 4
XML Standards-Based Technologies

• Prior releases of Oracle have supported the


following W3C XML Standards Support:
– XML 1.0 – The original XML Recommendation
– DOM 1.0 – Document Object Model
– SAX 1.0 – Simple API for XML
– XML Namespaces 1.0
– XSLT 1.0 and XPath 1.0
• Oracle9i has added support for
– XML Schema 1.0
– DOM 2.0 and SAX 2.0

XML Standards-Based Technologies


Oracle’s XDK’s are built to conform to these W3C XML standards:
• XML: The original recommendation.
• DOM: Document Object Model – Programmatic access to the in-memory document “tree”.
• SAX: Simple API for XML – Event driven document access.
• Namespaces: URIs for uniquely qualifying tags and attributes.
• XSLT: eXtensible Stylesheet Language Transformations for transforming XML documents to
any text-based format.
• Xpath: The recommendation that specifies the navigation syntax of XML documents.
New for Oracle9i is the support for the following W3C standards:
• XMLSchema – The latest recommendation (currently a Candidate Recommendation) that
specifies simple and complex data types for XML.
• DOM 2.0: The next generation DOM standard that includes a DOM 2.0 CORE with
enhancements to 1.0 for document creation and namespace support. Additionally the Parser
supports the 2.0 Traversal specification adding support for Treewalkers, Node Filters and
Node Iterators.
• SAX 2.0 in its entirety is also supported which adds namespace support through the addition
of new XMLReader, ContentHandler, and Attributes interfaces.

XML Enhancements 1- 5
XML Developer’s Kit Components

• The Oracle9i XML developer’s kits have the


following new components:
– XML schema processors
– XDK Java Beans
• The following components have been
enhanced in Oracle9i
– XML parsers
– XSL processors
– XML class generators
– XML SQL utility
– XSQL servlet

XML Developer’s Kit Components


Oracle has packaged its generic XML components and utilities into a series of XML Developer’s
Kits or XDKs. These XDKS are available in the platform independent languages of Java,
JavaBeans, and PL/SQL. As XDKs these components are included with a variety of Oracle
products including Oracle8i and now Oracle9i, Oracle Internet Application Server, Oracle Internet
Developer’s Suite and JDeveloper.
The Java XDKs can be used with JDK 1.1.6 or greater. The C/C++ XDKs include the appropriate
dependent CORE and NLS libraries. In prior releases the PL/SQL XDK required the Java parser
but in Oracle9i is built upon the C parser. Included with the XDKs are the following W3C
standard components.
Oracle9i XML components continue to be supported in the four major platforms: HP, Solaris, NT
and Linux.

XML Enhancements 1- 6
XML Schema Processors

New in Oracle9i is support for XML schema


• XML schema processors validate the use of
datatypes within XML documents
• Oracle XML schema processors are available
for Java, C, and C++
• Processors are compliant with W3C XML
schema 1.0 candidate recommendation
• Support for simple datatypes and structures
within XML documents is included
• Support for XML schema definition files (.xsd)
is also included replacing DTDs

XML Schema Processors


The XML family of technologies had no way of representing datatypes in a standards-based
format until the introduction of XML Schema. While DTDs served the document community
well, they had no mechanism to specify datatypes in a way the database community required. The
XML Schema definition file builds upon the DTD functionality while providing datatype support
and doing this in an XML file format.
The Oracle XML Schema Processor validates XML documents against their schema definitions,
adding default values where necessary, and when used with the XML Parser give programmatic
access for use in applications.

XML Enhancements 1- 7
Oracle9i XDK for Java Beans
• Set of XML visual and non-visual JavaBeans for
building XML-enabled applications
• XDK includes the Transviewer Beans from Oracle8i
• New in Oracle9i: DBAccess Bean and DBView Bean
• DBAccess Bean supports reading and writing XML
to the database
– Supports storing XML documents in XML CLOB
tables
– Supports submitting SQL queries and storing
the results in XML in CLOB tables
• XML DBView Bean provides a control panel for both
database and file XML operations

Oracle9i XDK for Java Beans


The Oracle9i XDK for JavaBeans are a set of visual and non-visual beans that are useful in
creating a variety of XML-enabled Java applications or applets. The bean encapsulation includes
documentation and descriptors that may be accessed directly from Java Integrated Development
Environments like JDeveloper. The visual beans extend JPanel and thus are dependent upon
Swing. The following describes the functionality of the new Beans in Oracle9i.
DBAccess Bean
The DBAccess Bean is the final non-visual bean which is used to read and write XML data to an
Oracle database that supports CLOB tables. This bean is used by the DBView bean to provide a
database interface to augment its file one when processing XML documents.
DBView Bean
The DBView Bean is a visual bean which can be used in any application that requires
visualization of database information using XML and stylesheet transformations. It includes all of
the functionality of the TransformPanel bean with the addition of a DB tab that enables database
access to submit queries, return results in XML, optionally transform the results with an XSL
stylesheet and store the resulting document in CLOB tables built and maintained by the DBAccess
bean.

XML Enhancements 1- 8
XML DBView Bean

XML DBView Bean


This is a screenshot of the DBView Bean. Notice that it lets the user set all of the database
connection parameters, enter queries and store the results in CLOB tables. Also note the DB and
File TAB in the lower left as this bean also includes all of the functionality of the Transform Panel
bean as well. JDeveloper can be used to create XDK components. The above screen shot is taken
from JDeveloper.

XML Enhancements 1- 9
XML Parsers

• XML parsers are the component that provides


programmatic access to XML documents
• Oracle XML parsers are available in Java, C, C++
and PL/SQL
• For Oracle9i the parsers support W3C standard
DOM 2.0 CORE specifications
• Oracle9i supports SAX 2.0 which adds
namespace support
• The C and C++ parsers have been re-designed
for improved performance and memory usage
• The PL/SQL parser is built on C for improved
performance

XML Parsers
The foundation component of any XML development is the XML parser. The parser provides
programmatic access to XML documents enabling them to be used in many ways within an
application. Oracle’s parsers support the two industry-standard specifications for XML document
access – DOM and SAX.
New for Oracle9i is support for DOM 2.0 and SAX 2.0 interfaces to go along with the 1.0 support
previously provided.
The support for DOM 2.0 includes the 2.0 CORE specification which standardizes many
previously custom interfaces primarily for document creation and adds support for XML
namespaces. Additionally, the optional DOM 2.0 traversal-range specification is supported added
signification document navigation functionality through the addition of Treewalkers, Node
Iterators and Node Filters.
Support is also added for SAX 2.0 which adds namespace support through the addition of new
XMLReader, ContentHandler, and Attributes interfaces.
Beyond the additional interfaces, the C and C++ parsers have been redesigned to improve
performance and decrease memory usage.
Finally, the PL/SQL parser is now built upon the C parser significantly improving performance
and resource usage when run within Oracle9i.

XML Enhancements 1-10


XSL Processors
• XSL processors transform one XML document to
another through stylesheets
• Oracle XSL processors are available in Java, C,
C++, and PL/SQL
• The Java processor supports the W3C XSL
transformations 1.1 specification with
– Support for standard extension functions in
Java and Ecmascript (Java script)
• The C and C++ processors have been improved
for performance
• The PL/SQL processor is built upon the C one for
improved performance

XSL Processors
The eXtensible Stylesheet Language, XSL, is the XML-based technology responsible for
transforming or formatting an input XML document into a different output one or rendered into
virtually any text-based output. Integrated into all of the Oracle XML Parsers are XSL Processors
that perform these transformations.
For Oracle9i the Java Processor supports the W3C XSL Transformations 1.1 specification adding
support for standard extension functions in Java and ECMAScript (Java Script).
For Oracle9i the C and C++ Processors have been redesigned for improved performance and
resource usage.
For Oracle9i the PL/SQL Processor is built upon the C one for improved performance.

XML Enhancements 1-11


XML Class Generators

• XML class generators create the XML classes


used to construct XML documents
• Versions are available for Java and C++
• Support for XML schema definition files (.xsd)
along with DTDs is available
• Generate class files to construct XML
documents with datatype support based upon
the DOM APIs.
• The validation mode available in Oracle8i has
been enhanced to support XML Schema and
aids debugging

XML Class Generators


The XML Class Generators in C++ and Java are utilities that create C++ and Java Classes from
DTDs and in Oracle9i, XML Schema definitions (.xsd) to enable the programmatic construction
of XML documents. The Class Generator works in conjunction with the Oracle XML Parser, and
provides complete character set support and optional validation mode for ease of debugging.
Documents created by these classes are fully compliant with the XML 1.0 W3C standard. These
classes can be easily used in applications, applets, JSPs and CGI to add XML generation
capability.

XML Enhancements 1-12


XML SQL Utility

• The XML SQL Utility generates XML results


from SQL queries
• Prior releases included Java and PL/SQL
versions
• Output XML Schema definition files (.xsd)
• Additional configuration options have been
added to enhance functionality

XML SQL Utility


Modern databases understand SQL and JDBC. As such the XML SQL Utility has been created to
support reading and writing XML documents to the database from SQL statements. The XML
SQL Utility is also capable of DML. It can insert/update/delete data into a target table from an
XML document or vice versa.
New for Oracle9i is support for generating XML Schema definition files (.xsd) besides DTDs
from database schemas which can convey both the structure and the datatypes of the schema.
This data model can then be input into the XML Class Generator described previously to deliver
the Java or C++ classes that can then be used in an application, applet, JSP, CGI, etc. to create
conforming XML documents that can be sent to the database and their data decomposed and
inserted into the schema.
Besides the previous released Java and PL/SQL versions, Oracle9i includes a new C version of
the XML SQL Utility.

XML Enhancements 1-13


XSQL Servlet

• The XSQL Servlet produces dynamic XML


documents from one or more SQL Queries
• The following new features have been added:
– Support for Java Web Server and Weblogic
servlet engines
– Dynamically assign stylesheets via
parameters passed to the XSQL page
– Declaratively update rows using XML
– (Continued on the next page)

XSQL Servlet
The XSQL Servlet is a server component written to assist in producing dynamic XML documents
from one or more SQL queries of data objects. It has built-in capabilities to transform the XML
result via XSL to a custom rendering appropriate to the requesting client. The XSQL Servlet uses
a simple declarative .xsql XML file format as its processing instructions. Not only does it support
retrieving data but inserting, updating, and deleting data as well.
It is now possible in Oracle9i to include one or more XSQL parameter references in the value of
the href attribute for an <?xml-stylesheet?> processing instruction. This allows the uri (uniform
resource indicator) for the XSLT stylesheet to be determined dynamically at runtime. The
parameter value may be passed in from the client, as can any parameter value, or assigned to a
page-private parameter whose value is set at runtime using the <xsql:set-page-param> action.
Previously, users relied on <xsql:dml> to accomplish table updates. This release of XSQL adds a
new action handler called <xsql:update-request> which offers a new, declarative option. It offers
the parallel update functionality to complement the declarative insert functionality provided
already with <xsql:insert-request>. The <xsql:update-request> works identically to the
<xsql:insert-request>, in that the posted XML document (optionally transformed by an indicated
XSLT stylesheet) will be processed by the underlying XML SQL Utility.

XML Enhancements 1-14


XSQL Servlet

• The following new features have been added:


– Declaratively delete rows using XML
– Limit the list of columns updated in an
xsql:insert-request operation
– Additional methods added to the
XSQL-PageRequest interface for using Java
objects
– Support for a namespace-qualified
database connection

XSQL Servlet (continued)


Previously, users relied on <xsql:dml> to accomplish table row deletes. This release of XSQL
adds a new action handler called <xsql:delete-request> which offers a new, declarative option. It
offers the parallel delete functionality to complement the declarative insert and update
functionality provided with <xsql:insert-request> and <xsql:update-request>. The <xsql:delete-
request> works like to the <xsql:insert-request>, in that the posted XML document (optionally
transformed by an indicated XSLT stylesheet) will be processed by the underlying XML SQL
Utility.
Previously, an <xsql:insert-request> would insert null for columns missing in the posted XML
document. To limit the list of columns whose values should be inserted, provide the optional
columns attribute with a space-separated (or comma-separated) list of column names. If this new,
optional attribute is not supplied, behavior remains as it was previously.
Using the new methods setRequestObject() and getRequestObject() methods on the
XSQLPageRequest interface, your custom action handlers can associate arbitrary Java objects
with the page request context. This allows an object to be created in one action and then
retrieved/accessed from another action on the same or nested page. If the object added to the page
request context implements the optional XSQLRequestObjectListener interface, then the object
will receive notifcation when page processing is completed so that it may clean up any internal
resources it may be using.
For additional clarity, your XSQL pages can now indicate their optional database connection to be
used using the namespace-qualified attribute xsql:connection="connname" as an alternative to the
unqualified connection="connname" attribute on the document element.

XML Enhancements 1-15


XSQL File Example

<?xml
<?xml version="1.0"?>
version="1.0"?>
<?xml-stylesheet
<?xml-stylesheet type="text/xsl"
type="text/xsl" media="MSIE
media="MSIE 5"
5"
href="doyouxml-msie.xsl" ?>
href="doyouxml-msie.xsl" ?>
<?xml-stylesheet
<?xml-stylesheet type="text/xsl"
type="text/xsl" media="Mozilla/3.0“
media="Mozilla/3.0“
href="doyouxml-hw.xsl"
href="doyouxml-hw.xsl" ?>
?>
<?xml-stylesheet
<?xml-stylesheet type="text/xsl"
type="text/xsl" media="Lynx"
media="Lynx"
href="doyouxml-lynx.xsl" ?>
href="doyouxml-lynx.xsl" ?>
<?xml-stylesheet
<?xml-stylesheet type="text/xsl"
type="text/xsl"
href="doyouxml.xsl"
href="doyouxml.xsl" ?>
?>
<query
<query connection="xmldemo">
connection="xmldemo">
select
select value(c)
value(c) as
as Claim
Claim from
from insurance_claim_view
insurance_claim_view cc
where
where c.claimpolicy.primaryinsured.lastname
c.claimpolicy.primaryinsured.lastname == 'Doe'
'Doe'
</query>
</query>

XSQL File Example


This is an example of an xsql file that searches for insurance claims submitted by Mr. Doe. Note
that the query may be written as regular SQL or, as in this case, using object dot notation.
The servlet uses Oracle’s XML Parser to process this file and pass any XSL processing statements
to its internal XSLT Processor while passing the parameters and SQL statements between the
<query> tags to the XML SQL Utilities. Results from those queries are then received as either
XML formatted text or a JDBC ResultSet object.
If necessary, the query results may be further transformed into any desired format using the built-
in XSLT processor. The code shows how the XSQL Servlet utilizes the "media" tag in HTTP
request headers to associate a stylesheet to the requesting browsers thereby allowing the website
designer to dynamically render content in accordance to the display capabilities of the user.
Note when a media attribute is not specified the declaration becomes the default stylesheet for all
unrecognized browsers. Also note the connection to the database, xmldemo, is an alias to the
connection string stored securely on the server.

XML Enhancements 1-16


Summary

In this lesson, you should have learned how to:


• Identify the XDK Components in Oracle9i
• Identify the new Oracle9i features of the XDK
Components
• Identify the Uses of the XDK Components

XML Enhancements 1-17


Oracle9i New Database XML Features

XML Enhancements 2- 1
Objectives

After this lesson, you should be able to:


• Identify the new database XML features in
Oracle9i
• Understand how the new features work
• Identify what benefits they provide, and where
they might be used

XML Enhancements 2- 2
What are the new Database XML features?

• A new native XMLType datatype


– Queryable from SQL with XPath support
– Member functions that perform useful XML
operations
• Native XML generation and aggregation
– New operators and packages to generate
and aggregate XML
• New native URI-Ref datatypes
– References to XML document fragments,
inside or outside a database

What are the new Database XML features?


In Oracle8i, the focus was on enabling XML and the database to work together,
integrating these technologies wherever possible. As XML has grown in importance and
as standards have matured, we have focused on providing more native support.
In Oracle9i, there are some fundamental advances in the database, which help it
understand XML natively and speak XML natively.
• A new native XMLType datatype
• Native XML generation and aggregation
• A new URI-Ref datatype
Reference
Note: For more information on these topics please refer to
• Oracle9i XML Users guide
• Oracle9i Intermedia module
• Oracle9i Business Intelligence Module

XML Enhancements 2- 3
XMLType

XMLType is a server datatype that can be used


as columns in tables

CREATE
CREATE TABLE
TABLE warehouses(
warehouses(
warehouse_id
warehouse_id NUMBER(3),
NUMBER(3),
warehouse_spec
warehouse_spec SYS.XMLTYPE,
SYS.XMLTYPE,
warehouse_name
warehouse_name VARCHAR2(35),
VARCHAR2(35),
location_id
location_id NUMBER(4));
NUMBER(4));

XMLType
Oracle9i has a new native XMLType for storing XML content. This new type is similar
to the other new types that have appeared in the Oracle server over versions 8.0 and 8i.
XMLType gives developers a convenient abstraction to store XML content. There are a
number of built-in member functions on this type that enable developers to manipulate
XML content. Today, the data stored in an XMLType is stored as a character LOB
(CLOB).

XML Enhancements 2- 4
XMLType

• Variables of this type can be used in PL/SQL


stored procedures as parameters, return
values, and so on.
• Contains built-in member functions for
performing useful operations on the XML
content
• Provides abstraction for XML storage
• For example: the function Extract(...)
extracts a specific node from a XMLType

XML Enhancements 2- 5
Using XMLType

• XMLTypes can be used in database columns


• The XMLType constructor can be used to
create XMLType instances for insertion
• The XML content can be easily queried from
SQL

Using XMLType
The big benefit of a native XMLType is seamless operation with SQL and the rest of the
database platform.
You first define a column of XMLType. You can specify optional storage characteristics
along with the definition.
Use the XMLType constructor to create the XMLType instance before inserting into the
columns. You can also use the SYS_XMLGEN and SYS_XMLAGG operators (we will
look at these later, too), to directly create instances of XMLType.
You can select the XMLType instance from the column. The XMLType offers a choice
of functions and operators, extract() and existsNode() which extract a
particular Node or check to see if a particular node exists in the XMLType, respectively.
You can also create a text index on the XMLType column. We will look at indexing
later.
You can insert and delete from tables using the operators above. Updates are currently
only supported at the document level. You cannot update only part of an XML
document. In order to update part of a document, you have to update the entire
document.

XML Enhancements 2- 6
Example of Using XMLType
CREATE
CREATE TABLE
TABLE warehouses(
warehouses(
warehouse_id
warehouse_id NUMBER(3),
NUMBER(3),
warehouse_spec
warehouse_spec SYS.XMLTYPE,
SYS.XMLTYPE,
warehouse_name
warehouse_name VARCHAR2(35),
VARCHAR2(35),
location_id
location_id NUMBER(4));
NUMBER(4));
INSERT
INSERT INTO
INTO warehouses
warehouses
VALUES
VALUES (5,
(5,
sys.xmltype.createxml('<?xml
sys.xmltype.createxml('<?xml version="1.0"?>
version="1.0"?>
<Warehouse>
<Warehouse>
<Building>Owned</Building>
<Building>Owned</Building>
<Area>25000</Area>
<Area>25000</Area>
<Docks>2</Docks>
<Docks>2</Docks>
<DockType>Rear
<DockType>Rear load</DockType>
load</DockType>
<WaterAccess>Y</WaterAccess>
<WaterAccess>Y</WaterAccess>
<RailAccess>N</RailAccess>
<RailAccess>N</RailAccess>
<Parking>Street</Parking>
<Parking>Street</Parking>
<VClearance>10
<VClearance>10 ft</VClearance>
ft</VClearance>
</Warehouse>'),
</Warehouse>'),
'Toronto',1800);
'Toronto',1800);

Example of Using XMLType


In the above example we create the WAREHOUSES table with a couln of type XMLType.
We then insert a record into it.

XML Enhancements 2- 7
XMLType Storage Characteristics

CREATE
CREATE TABLE
TABLE warehouses(
warehouses(
warehouse_id
warehouse_id NUMBER(3),
NUMBER(3),
warehouse_spec
warehouse_spec SYS.XMLTYPE,
SYS.XMLTYPE,
warehouse_name
warehouse_name VARCHAR2(35),
VARCHAR2(35),
location_id
location_id NUMBER(4))
NUMBER(4))
XMLType
XMLType COLUMN
COLUMN warehouse_spec
warehouse_spec
STORE
STORE AS
AS CLOB
CLOB
(TABLESPACE
(TABLESPACE lob_seg_ts
lob_seg_ts
STORAGE
STORAGE (INITIAL
(INITIAL 4096
4096 NEXT
NEXT 4096)
4096)
CHUNK
CHUNK 4096
4096 NOCACHE
NOCACHE LOGGING);
LOGGING);

XMLType Storage Characteristics


Creating the column warehouse_spec creates a virtual column of XMLType, and a real,
hidden column of type CLOB.
You can specify the storage characteristics of the underlying CLOB column using the
STORE AS clause.
The arguments to the storage clause are the same for regular LOBs.
As XMLType is enhanced in the future to support other storage models, other storage
clauses will be supported. You thus leave the storage options to the DBA, and from the
development standpoint use only XMLType, independent of the storage.

XML Enhancements 2- 8
XMLType Functions

XMLType Function Description


Return the contents of the XMLType
getClobVal
as a CLOB value
isFragment Is the document really a fragment?

getStringVal Gets a string value from a XML Node


Gets a numeric value from a XML
getNumVal
Node
Extracts a portion of the document
extract using a XPath-like syntax, returning a
XMLType
Checks if there are any resultant
existsNode
Nodes in the XPath expression

XMLType Functions
isFragment() – A fragment might be present if an extract or other operation was
done on a document that resulted in many nodes.
getNumVal() – the call works only if the target node really contains a number.
ExistsNode() – note that the XPath expression must point to a single node, or
multiple nodes, or text values. If it returns a boolean expression, then the
getStringVal returns the literal ‘true’or ‘false’as applicable.

XML Enhancements 2- 9
Example of Using XMLType

SELECT
SELECT w.warehouse_spec.extract
w.warehouse_spec.extract
('/Warehouse/Building/text()').getStringVal()
('/Warehouse/Building/text()').getStringVal()
"Building"
"Building"
FROM
FROM warehouses
warehouses ww

Building
Building
-----------------
-----------------
Owned
Owned

Example of Using XMLType


In the above example we are extracting the value of the "Building" node and printing it
using getStringVal(). The correlation variable (alias) in the query "w" is required for the
query to complete

XML Enhancements 2-10


Indexing XMLTypes
• In Oracle9i, the ExistsNode, Extract functions have a
functional implementation
• They can be speeded up by building functional
indexes

CREATE
CREATE INDEX
INDEX wh_idx
wh_idx ON
ON warehouses
warehouses
(warehouse_spec.extract
(warehouse_spec.extract
('/Warehouse/Building').getStringVal());
('/Warehouse/Building').getStringVal());
• Indexes can be created on the stored CLOB content
CREATE
CREATE INDEX
INDEX wh_spec_idx
wh_spec_idx ON
ON
warehouses((warehouse_spec).getStringVal());
warehouses((warehouse_spec).getStringVal());

Indexing XMLTypes
The above index will help any query like:
SELECT * FROM warehouses
w WHERE
w.warehouse_spec.extract('/Warehouse/Building'). getStringV
al() LIKE 'Owned';
If you want to speed up a query like:
SELECT * FROM warehouses
WHERE warehouse_spec.existsNode('/Warehouse/Area') = 1
then you have to build a functional index on existsNode:
CREATE INDEX wh_idx2 ON
warehouses(warehouse_spec.existsNode('/Warehouse/Area'));
The text index support XPath expressions in Oracle9i. This allows the full-text search
capabilities to be integrated with XML.

XML Enhancements 2-11


Using Indexes on XMLTypes

• The index created will help a query such as follows:

SELECT
SELECT ** FROM
FROM warehouse
warehouse ww
WHERE
WHERE w.warehouse_spec.extract
w.warehouse_spec.extract
('/Warehouse/Building').getStringVal()
('/Warehouse/Building').getStringVal()
LIKE
LIKE 'Owned';
'Owned';

XML Enhancements 2-12


Benefits of XMLType

• Brings the XML and SQL worlds together


• Enables:
– SQL operations on XML content
– XML operations on SQL content
• Convenient – built in functions, indexing,
navigation, and so on.

XML Enhancements 2-13


Native XML Generation

• DBMS_XMLGEN package in PL/SQL for getting XML


from SQL queries
– Written in C and linked to server for better
performance
• SQL functions for getting XML from SQL queries
– SYS_XMLGEN operates on rows, generating
XML documents
– SYS_XMLAGG operates on groups of rows,
aggregating several XML documents into one

Native XML Generation


These effectively move the XML SQL Utility functionality in the server. The Oracle
8.1.7 XML SQL Utility functionality is still available to generate XML at the client.

XML Enhancements 2-14


DBMS_XMLGEN
• Creates an XML document from any SQL query
and gets the document as a CLOB
• Provides a "fetch" interface with maximum
rows and rows to skip – useful for pagination
in web applications
• Provides options for changing tag names for
ROW, ROWSET etc.

DBMS_XMLGEN
Fetch Interface: for example, the first fetch can retrieve a maximum of 10 rows, after
skipping the first two rows.

XML Enhancements 2-15


DBMS_XMLGEN Example
CREATE
CREATE TABLE
TABLE temp_clob_tab(result
temp_clob_tab(result CLOB);
CLOB);
DECLARE
DECLARE
qryCtx
qryCtx DBMS_XMLGEN.ctxHandle;
DBMS_XMLGEN.ctxHandle;
result
result CLOB;
CLOB;
BEGIN
BEGIN
qryCtx
qryCtx :=
:= dbms_xmlgen.newContext
dbms_xmlgen.newContext
('SELECT
('SELECT ** from
from employees;');
employees;');
DBMS_XMLGEN.setRowTag(qryCtx,
DBMS_XMLGEN.setRowTag(qryCtx, 'EMPLOYEE');
'EMPLOYEE');
DBMS_XMLGEN.setMaxRows(qryCtx,
DBMS_XMLGEN.setMaxRows(qryCtx, 5);
5);
LOOP
LOOP
result
result :=
:= DBMS_XMLGEN.getXML(qryCtx);
DBMS_XMLGEN.getXML(qryCtx);
EXIT
EXIT WHEN
WHEN
DBMS_XMLGEN.getNumRowsProcessed((qryCtx)=0);
DBMS_XMLGEN.getNumRowsProcessed((qryCtx)=0);
INSERT
INSERT INTO
INTO temp_clob_tab
temp_clob_tab VALUES(result);
VALUES(result);
END
END LOOP;
LOOP;
END;
END;
//

DBMS_XMLGEN Example
Here is the calling sequence for DBMS_XMLGEN:
Get the context from the package by supplying a SQL query and calling
newContext().
1. Pass the context to all the procedures/functions. For example, you can set the ROW
element’s name using the setRowTag(ctx) call.
2. The XML is generated by calling getXML. By setting the maximum number of
rows to be retrieved per fetch using the setMaxRows() call, you can invoke this
function repeatedly, getting the maximum no. of rows set per call.
3. You can reset the query to start step 2 again.

XML Enhancements 2-16


Generated XML

<?xml
<?xml version=''1.0''?>
version=''1.0''?>
<ROWSET>
<ROWSET>
<EMPLOYEE>
<EMPLOYEE>
<EMPLOYEE_ID>30</EMPLOYEE_ID>
<EMPLOYEE_ID>30</EMPLOYEE_ID>
<LAST_NAME>SCOTT</LAST_NAME>
<LAST_NAME>SCOTT</LAST_NAME>
<SALARY>20000<SALARY>
<SALARY>20000<SALARY>
</EMPLOYEE>
</EMPLOYEE>
<EMPLOYEE>
<EMPLOYEE>
<EMPLOYEE_ID>31</EMPLOYEE_ID>
<EMPLOYEE_ID>31</EMPLOYEE_ID>
<LAST_NAME>MARY</LAST_NAME>
<LAST_NAME>MARY</LAST_NAME>
<AGE>25</AGE>
<AGE>25</AGE>
</EMPLOYEE>
</EMPLOYEE>
</ROWSET>
</ROWSET>

Note: You can also change the tag associated with ROWSET by calling setRowsetTag.

XML Enhancements 2-17


Generating Complex XML
Complex XML can be generated using Object
types to represent nested structures
CREATE
CREATE TABLE
TABLE new_departments
new_departments ((
department_id
department_id NUMBER
NUMBER PRIMARY
PRIMARY KEY,
KEY,
department_name
department_name VARCHAR2(20));
VARCHAR2(20));
CREATE
CREATE TABLE
TABLE new_employees
new_employees ((
employee_id
employee_id NUMBER
NUMBER PRIMARY
PRIMARY KEY,
KEY,
last_name
last_name VARCHAR2(20),
VARCHAR2(20),
department_id
department_id NUMBER
NUMBER REFERENCES
REFERENCES departments);
departments);
CREATE
CREATE TYPE
TYPE emp_t
emp_t AS
AS OBJECT
OBJECT
("@employee_id"
("@employee_id" NUMBER,
NUMBER, last_name
last_name VARCHAR2(20));
VARCHAR2(20));
CREATE
CREATE TYPE
TYPE emplist_t
emplist_t AS
AS TABLE
TABLE OF
OF emp_t;
emp_t;
CREATE
CREATE TYPE
TYPE dept_t
dept_t AS
AS OBJECT(
OBJECT(
"@department_id"
"@department_id" NUMBER,
NUMBER,
department_name
department_name VARCHAR2(20),
VARCHAR2(20),
emplist
emplist emplist_t);
emplist_t);

Generating Complex XML


When you input an object type to DBMS_XMLGEN functions, appropriate structures are
generated in XML. Any attributes of the Object Type that begin with "@" become
attributes of the preceding element in XML; the rest are mapped to elements. Object
Types can be used to get nested structures in XML documents.
Given the two tables departments and employees, we can generate a hierarchical view of
the data in which each departments has a nested structure of employees. The department
type represents the department as containing a list of employees.

XML Enhancements 2-18


Generating Complex XML

qryCtx
qryCtx :=
:= dbms_xmlgen.newContext
dbms_xmlgen.newContext
('SELECT
('SELECT dept_t(department_id,
dept_t(department_id, department_name,
department_name,
CAST(MULTISET
CAST(MULTISET
(SELECT
(SELECT e.employee_id,
e.employee_id, e.last_name
e.last_name
FROM
FROM employees
employees ee
WHERE
WHERE e.department_id
e.department_id == d.department_id)
d.department_id)
AS
AS emplist_t))
emplist_t))
AS
AS deptxml
deptxml
FROM
FROM departments
departments d');
d');

DBMS_XMLGEN.setRowTag(qryCtx,
DBMS_XMLGEN.setRowTag(qryCtx, NULL);
NULL);

Generating Complex XML (continued)


The MULTISET operator treats the result of the subset of employees working in the
department as a list and the CAST around it casts this list to the appropriate collection
type. You then create a departmental instance around this, and call the usual
DBMS_XMLGEN routines to create the XML for the object instance. Note that the default
name ROW is not present as we set this tag to NULL.The complete example is as
follows:
DECLARE
qryCtx dbms_xmlgen.ctxHandle;
result CLOB;
BEGIN
-- get the query context;
qryCtx := dbms_xmlgen.newContext
('SELECT dept_t(department_id,
department_name,
CAST(MULTISET
(SELECT e.employee_id, e.last_name
FROM employees e
WHERE e.department_id =
d.department_id)
AS emplist_t))
AS deptxml
FROM departments d');
-- set the maximum number of rows to be 5,
dbms_xmlgen.setMaxRows(qryCtx, 5);
LOOP
-- now get the result..
result := dbms_xmlgen.getXMLClob(qryCtx);
-- do some processing with the lob data..! Here, we are
inserting the results
-- into a table. You can print the lob out, output it to a
stream, put it in a queure
XML Enhancements
-- or do any other processing. 2-19
INSERT INTO temp_clob_tab VALUES(result);
Generating Complex XML

<ROWSET>
<ROWSET>
<DEPTXML
<DEPTXML DEPARTMENT_ID="10">
DEPARTMENT_ID="10">

<DEPARTMENT_NAME>SALES</DEPARTMENT_NAME>
<DEPARTMENT_NAME>SALES</DEPARTMENT_NAME>
<EMPLIST>
<EMPLIST>
<EMP_T
<EMP_T EMPLOYEE_ID="30">
EMPLOYEE_ID="30">
<LAST_NAME>Scott</LAST_NAME>
<LAST_NAME>Scott</LAST_NAME>
</EMP_T>
</EMP_T>
<EMP_T
<EMP_T EMPLOYEE_ID="31">
EMPLOYEE_ID="31">
<LAST_NAME>Mary</LAST_NAME>
<LAST_NAME>Mary</LAST_NAME>
</EMP_T>
</EMP_T>
</EMPLIST>
</EMPLIST>
</DEPTXML>
</DEPTXML>
<DEPTXML
<DEPTXML DEPARTMENT_ID="20">
DEPARTMENT_ID="20">
...
...
</ROWSET>
</ROWSET>

XML Enhancements 2-20


SYS_XMLGEN

• Takes a scalar value, object type or XMLType


instance to be converted to an XML document
• Takes an optional XMLGenFormatType object
to specify formatting options for the result
• Returns an XMLType
• Is used to create and query XML instances
within SQL queries.
SQL>
SQL> SELECT
SELECT SYS_XMLGEN(employee_id)
SYS_XMLGEN(employee_id)
22 FROM
FROM employees
employees WHERE
WHERE last_name
last_name LIKE
LIKE
'Scott%';
'Scott%';
<?xml
<?xml version=''1.0''?>
version=''1.0''?>
<employee_id>60</employee_id
<employee_id>60</employee_id
>>

SYS_XMLGEN
DBMS_XMLGEN and other packages operate at a Query level, giving out aggregated
results for an entire query. SYS_XMLGEN, on the other hand, can take a single argument
within a SQL query and convert it to XML.
SYS_XMLGEN works inside SQL queries and operates on expressions and columns
within the row. DBMS_XMLGEN works on the entire resultset.

XML Enhancements 2-21


SYS_XMLAGG

Aggregates all the input documents and


produces a single XML document
• Used to aggregate (concatenate) fragments
SQL>
SQL> SELECT
SELECT SYS_XMLAGG(SYS_XMLGEN(last_name)
SYS_XMLAGG(SYS_XMLGEN(last_name)
22 ,, SYS.XMLGENFORMATTYPE.createFormat
SYS.XMLGENFORMATTYPE.createFormat
33 ('EmployeeGroup')).getClobVal()
('EmployeeGroup')).getClobVal()
44 FROM employees
FROM employees
55 GROUP
GROUP BY
BY department_id;
department_id;

SYS_XMLAGG
Here, we are aggregating data together pretty much as in a GROUP BY operation, with
the GROUP BY also showing up on the XML document produced.
SYS_XMLAGG also works with OLAP aggregation functionality such as ROLLUP and
CUBE. You can thus create different XML documents based on ROLLUP and CUBE
operations.
Oracle provides windowing functions that can be used to compute cumulative, moving
and centered aggregates. SYS_XMLAGG can be used with these as well, to create
documents based on rank and partition.

XML Enhancements 2-22


SYS_XMLAGG Output

<EmployeeGroup>
<EmployeeGroup>
<last_name>Scott</last_name>
<last_name>Scott</last_name>
<last_name>Mary<last_name>
<last_name>Mary<last_name>
</EmployeeGroup
</EmployeeGroup >>
<EmployeeGroup
<EmployeeGroup >>
<last_name>Jack</last_name>
<last_name>Jack</last_name>
<last_name>John>/last_name>
<last_name>John>/last_name>
</EmployeeGroup
</EmployeeGroup >>

XML Enhancements 2-23


TABLE Functions

• TABLE functions are new in Oracle9i


– Can be used to model any arbitrary data
(internal to the database or from an external
source) as a collection of SQL rows.
– Are executed pipelined and in parallel for
performance.
• Can be used to break XML into SQL rows
which can be consumed by regular SQL
queries and inserted into regular relational or
object-relational tables.

TABLE Functions
A TABLE function can be used to take an external purchase order document and, using
the ODCITable interface, convert it into a set of SQL rows. The server can perform this
conversion with high efficiency. The SQL rows can then be stored in regular tables. The
mapping between the external XML document and the database table then helps in the
definition of the table function.
Reference
Note: Please refer the Extensibility module and SQL/PLSQL module for more
information on table functions.

XML Enhancements 2-24


URI-References

Generalization of the URL concept


• Consists of two parts:
– URL part
http://www.oracle.com/xml/doc1#//
– Fragment part
warehouse_spec/custno

Note: Oracle9i does not support fragments.

XML Enhancements 2-25


DBURI-Ref

A DBURI-Ref is an intra-database URI


• Specifies a URI for columns and rows inside a
database
– /SCOTT/EMPLOYEES/ROW[ADDRESS/STATE='CA']/CITY
• A DBURI-Ref can address different granularities;
for example, a table, a row, a column within a row, a
particular attribute of an object column, and so on.
• Together, HTTPURI-Refs and DBURI-Refs provide
an unified model of looking up documents on the
web or in a database.

Database URI-References
Another DBURI-Ref example:
employees table has a column called address, which contains state, city, street and zip attributes
/SCOTT/EMPLOYEES/ROW[ADDRESS/STATE='CA' OR
STATE='OR']/ADDRESS[CITY='PORTLAND' OR ./ZIPCODE =94404]/CITY
The above DBURI-Ref identifies the city attribute of the address column in the employees table in
scott’s schema whose state is either California or Oregon or the city name is Portland or the zip code is
94404.
DBURI-Ref is a simplified XPath format. For this release Oracle does not support the full flavor of
XPath or XPointer for a DBURI-Ref.

XML Enhancements 2-26


The New URI-Ref Datatypes

• HTTPURIType: references remote data via


HTTP
• DBURIType: references local data in a
database
• URIType: An abstract type that can store
instances of either of the above, useful when
your references can be either local or remote

The New URI-Ref Datatypes


These datatypes are object types with member functions that can be used to access data
in a database or pointed-to from a database. So, using the URITypes, you can create
columns that can point to data inside or outside the database. You can also query the
database columns uniformly, irrespective of whether the data is inside or outside the
database, using the abstract functions provided by URIType.
DBURIRef has column and attribute level access and is loosely typed. Interestingly, the
Object-Relational Reference (REF) mechanism in Oracle8i provided the capability to
‘point to’a row of an object table in the database. DBURI-Ref can not only identify a
particular row, but also provide access to a column or object attribute of the row. In
addition, it is loosely typed (it can point to a variety of data types), unlike object
references. In this way, DBURI's are a superset of object references.

XML Enhancements 2-27


Using URITypes

SQL>
SQL> CREATE
CREATE TABLE
TABLE tax_deductible_tab
tax_deductible_tab ((
22 uri_col
uri_col UriType,
UriType,
33 max_deduction
max_deduction NUMBER(7,2),
NUMBER(7,2),
44 description
description VARCHAR2);
VARCHAR2);

SQL>
SQL> INSERT
INSERT INTO
INTO tax_deductible_tab
tax_deductible_tab VALUES
VALUES ((
22 UriFactory.getURL
UriFactory.getURL
33 ('/SCOTT/Warehouses/ROW[Area=2500'),
('/SCOTT/Warehouses/ROW[Area=2500'),
44 2500.00,
2500.00, 'Scott''s
'Scott''s Warehouse
Warehouse Info');
Info');

SQL>
SQL> INSERT
INSERT INTO
INTO tax_deductible_tab
tax_deductible_tab VALUES
VALUES ((
22 UriFactory.getURL
UriFactory.getURL
33 ('http://proxy.oracle.com/webaccts/pos/scott/po1'),
('http://proxy.oracle.com/webaccts/pos/scott/po1'),
44 1000.00,
1000.00, 'Scott''s
'Scott''s Web
Web PO');
PO');

Using URITypes
The UriFactory package contains methods to generate the appropriate kind of URI
without having to hard code the implementation in the program
The factory method can take strings representing the various URLs and return the
appropriate subtype instance.
For example, if the prefix starts with a http://, it creates a HTTPUriType and returns a
reference to that instance, after stripping out the http:// prefix. If the string starts with a
/oradb/ or similar prefix, it returns a DBUriType instance.
The factory also provides the ability to register new subtypes of the URiType to handle
other protocols not supported by Oracle9i. If you wish, you can invent a new protocol
b2b://, create a subtype of UriType to handle that protocol and register this protocol with
the UriFactory. The factory can then generate instance of your subtype whenever it saw
a URI with the b2b:// prefix.

XML Enhancements 2-28


Using URITypes

SELECT
SELECT e.ur_col.getClob()
e.ur_col.getClob()
FROM
FROM tax_deductible_tab
tax_deductible_tab e;
e;

--PL/SQL
--PL/SQL
DECLARE
DECLARE
V_uri
V_uri UriType;
UriType;
BEGIN
BEGIN
SELECT
SELECT uri_col
uri_col into
into v_uri
v_uri
FROM
FROM tax_deductible_tab
tax_deductible_tab
WHERE
WHERE Description
Description LIKE
LIKE 'Scott%';
'Scott%';
printDataOut(v_uri.getClob());
printDataOut(v_uri.getClob());
END;
END;

Using URITypes (continued)


This shows you can query a URIType column without knowing where the referenced
document lies (inside or outside a database.)
The same functionality is also available from PL/SQL.

XML Enhancements 2-29


URIType Methods

URIType Method Description

getClob Returns the value pointed to by the URL as


a character LOB value.

getURL Returns the URL stored in the URIType.

Similar to getURL, but calls escaping


getExternalURL mechanism to escape characters according
to the URL specification.

URIType Methods
getClob: The character set of the encoding will be the database character set.
getURL: URIType has a hidden attribute called URL. Do not use this directly; use
getURL instead.
getExternalURL: For example, spaces are converted to the escaped value %20.

XML Enhancements 2-30


Benefits of URI-Ref Types

• Uniform access to data inside or outside the


database
• Improved mapping of XML documents to
columns
• Better datatypes leads to efficient indexing,
navigation, querying

Benefits of URI-Ref Types


Improved mapping of XML documents: URI-Ref support is needed when storing XML
documents into columns, so that we can view the database as an XML store and easily
navigate it, “walking a tree” of XML nodes.
Better typing leads to efficiencies: It is possible to build URL-aware indexing schemes,
better navigation and query capabilities by knowing that a column is of type URI (as
opposed to, say, VARCHAR.)

XML Enhancements 2-31


SYS_DBURIGEN()

• Provides support for dynamic DBURI references


• Accepts a set of PK or ROWID columns, as well
as a column/attribute to which a reference is
needed.
• Returns a DBUriType that points to the
particular column.
CREATE
CREATE TABLE
TABLE doc_ownership_tab
doc_ownership_tab ((
docno
docno NUMBER,
NUMBER,
doc_owner
doc_owner DBUriType);
DBUriType);

INSERT
INSERT INTO
INTO doc_list_tab(
doc_list_tab(
1001,
1001,
SELECT
SELECT SYS_DBURIGEN(employee_id,last_name)
SYS_DBURIGEN(employee_id,last_name)
FROM
FROM employees
employees WHERE
WHERE employee_id
employee_id == 7369);
7369);

SYS_DBURIGEN()
DBURIs can be created statically using path expressions in a constructor of the
UriFactory
The result of the SYS_DBURIGEN looks like:
/SCOTT/EMPLOYEES/ROW[employee_id=7369]/last_name

XML Enhancements 2-32


HTTP Access for DBURIRefs
• Access DB references from the external world
• Servlet support for DBURIRefs
– A default servlet runs in the Oracle Servlet
Engine, making JNI calls to DBUri resolution
code in the server
oracle.xml.dburi.OraDbUriServlet()
– Takes DBUri and sends to the output stream
the data pointed-to
– http://MACHINE.ORACLE.COM:8080/ORADB/S
COTT/EMPLOYEES/ROW[employee_id=7369]/l
ast_name generates xml content in output
stream
– Runs in SYS/DBUser realm
• You can also write a servlet that runs on other
servlet engines, to execute DBUriRef calls

HTTP Access for DBURIRefs


Security: If you publish the servlet under a realm other than the SYS/DbUserRealm,
then the queries are executed under the privileges of the published user.
Take care that the servlet is not published under a realm other than the DBUser realm for
the SYS user, otherwise users accessing the servlet will have access to all the database
data.

XML Enhancements 2-33


Summary

In this lesson, you should have learned how to:


• Identify the new database XML features in
Oracle9i
• Understand how the new features work
• Identify what benefits they provide, and where
they might be used

XML Enhancements 2-34

Potrebbero piacerti anche