Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How handle null/empty fields in XML?

Status
Not open for further replies.

slicendice

Programmer
Jun 28, 2002
164
GB
Hi
I have a bunch of XML files that need reading/processing and I've constructed something that uses xmltype, dbms_xmlschema, etc. I've loaded the associated XSD schema into the database using the following:

SQL:
-- Read the XSD file and load it into an xmltype
-- xtXSD is declared as xmltype
xtXSD := xmltype(dbms_xslprocessor.read2Clob(gccWIFDir, p_vcXSDFilename));

BEGIN
    -- If a schema with this name already exists then get rid of it
    -- i.e. this new schema will overwrite any existing schema
    dbms_xmlschema.deleteSchema(gcvcSchemaURL,
                                dbms_xmlschema.delete_cascade_force);
EXCEPTION
    WHEN schema_doesnt_exist THEN
        NULL;
END;

dbms_xmlschema.registerSchema(schemaurl => gcvcSchemaURL,
                              schemadoc => xtXSD,
                              local     => TRUE,
                              gentypes  => FALSE,
                              genbean   => FALSE,
                              gentables => FALSE);

When processing the XML files, the following is used:

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 sometimes happen), then the call to schemaValidate above produces the following error:

SQL:
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 that contains a blank value:

XML:
<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...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top