Wednesday, March 26, 2008

XMLTypes in Oracle 10g

I had a chance to make use of XMLType in the Oracle Database and was really annoyed at the restrictions put in by the database. I did encounter a forum which indicated that these issues would be fixed in 11g. I hope that is true.


ISSUES with XMLType in Oracle 10g

1. When registering a schema for the XML data the element name should be restricted to less than 30 characters. This is because, as I understand it, tables are created for each of those elements and the table names in Oracle cannot be greater than 30 characters.

2. Each element node cannot have data whose size is greater than 64kb.

SOLUTION:
store XML data in the database as CLOB's - which is what we ended up doing. or wait for the next release and upgrade.


HANDY NOTES WHEN USING XMLTypes

creating a table of XMLType

CREATE TABLE Example1 ( 
KEYVALUE varchar2(10) primary key, XMLCOLUMN xmltype);

Stored Procedure to register a schema
BEGIN

declare
doc CLOB =sample XSD
begin
dbms_xmlschema.registerSchema('test1.xsd', doc,false,true,false,true);
end;

privileges required in order to register an XML SCHEMA:

I understand from the dba person here that the following privileges are required
create session
alter session
create table
create type
create trigger

If the XML SCHEMA is to be registered globally,
the following role is required:
xdbadmin

TO view the XML data in the table

select a.col1.getStringVal() from Example1 a;

If using clobs - create a XMLType from the clob
xmlMessage := sys.xmltype.createXML(clobmessage);


extract an element in the xmlMessage
xmlTest := xmlMessage.extract(xpath, xmlnamesp)

xmlnamespace eg: urn:blah

or
xmlTest := xmlMessage.extract(xpathExtract) if the XML has no namespace defined


append a child node to the XML data

update Example1 SET XMLCOLUMN=(
appendChildXML(sys.xmltype.createXML(XMLCOLUMN),
xpathInsert,
xmlTest,
xmlnamesp)).getClobVal() WHERE KEYVALUE = 1;


Referred Documentation
XML DB Developers Guide