Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How to handle null/empty values in XML

Ed_1973Jan 12 2015 — edited Jan 20 2015

Hi

I have a bunch of XML files that need reading/processing and I've constructed something that uses xmltype, dbms_xmlschema, etc to do this. I've loaded the associated XSD schema into the database using the following:

PL/SQL

dbms_xmlschema.registerSchema(schemaurl => gcvcSchemaURL,

                              schemadoc => xtXSD,

                              local     => TRUE,

                              gentypes  => FALSE,

                              genbean   => FALSE,

                              gentables => FALSE);

When processing the XML files, the following is used:

PL/SQL

-- Read the XML file and load it into an xmltype

xtXML := xmltype(dbms_xslprocessor.read2Clob(gccWIFDir, p_vcFilename));

-- Check to make sure the XML contents conform to the schema

xtXML := xtXML.createSchemaBasedXML(gcvcSchemaURL);

xtXML.schemaValidate;

It all works fine as long as every field in the XML file has a value; if one of the fields has no value (which can legitimately happen sometimes), then the call to schemaValidate above produces the following error:

SQL error

ORA-31154: invalid XML document

ORA-19202: Error occurred in XML processing

LSX-00009: data missing for type "short"

ORA-06512: at "SYS.XMLTYPE", line 354

The XSD file contains the following definition for the field (index_2) that contains a blank value:

XSD

<xs:element name="index_2">

<xs:complexType>

  <xs:sequence>

   <xs:element type="xs:string" name="id"/>

   <xs:element type="xs:short" name="value"/>

  </xs:sequence>

</xs:complexType>

</xs:element>

...and the section in the XML file that contains the field with no value is:

XML

<index_2>

  <id>ICIS_EVENT_ID</id>

  <value />

</index_2>

Is there any way, either in the XSD or in the way the XML file is processed, to cleanly handle empty values? And by that I mean to somehow indicate that it's OK for a field to contain no value and therefore prevent an error being generated?

Thanks...

Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Feb 17 2015
Added on Jan 12 2015
3 comments
3,460 views