XML Schema Validation in an Oracle Database

The database can validate the the structure of an XML instance document against an XML schema. Schemas must be registered in the database prior to use. The following code registers an xml schema to validate database connect strings. Each connect string should have a serviceName, listenerPort and oracleHome. The listenerPort value must fall in the range 1521 to 15999. Schemas are registered by calling dbms_xmlschema.registerSchema. This procedure accepts a schema url and xml schema document as input parameters. The schema URL acts as a unique identifier for the schema. By convention these are created as a urls. This url must be unique but does not need to accessible. The example calls registerSchema with genTypes, genBeans and genTables = FALSE. This tells the database not to create object relational items to support the schema.

declare
  xml_schema_text  varchar(4000) :=
'<xsd:schema  xmlns:xsd="http://www.w3.org/2001/XMLSchema">

   <xsd:element name="databaseProperties" type="databasePropertiesType"/>
   <xsd:complexType name="databasePropertiesType">
      <xsd:sequence>
         <xsd:element    name="serviceName"   type="xsd:string"/>

         <xsd:element    name="listenerPort"  type="listenerPortType"/>
         <xsd:element    name="oracleHome"    type="xsd:string"/>
      </xsd:sequence>

   </xsd:complexType>
   <xsd:simpleType name="listenerPortType">
      <xsd:restriction   base="xsd:positiveInteger">
          <xsd:minInclusive value="1521"/>
          <xsd:maxInclusive value="15999"/>

      </xsd:restriction>
   </xsd:simpleType>
</xsd:schema>';
  xml_schema_doc          sys.xmltype;
begin
  xml_schema_doc :=   xmltype(xml_schema_text);
  dbms_xmlschema.registerSchema(schemaURL =>
                     'http://goldthorp.com/xmlTypes/databaseProperties2.xsd'
                              , schemaDoc => xml_schema_doc
                              , local     => TRUE
                              , genTypes  => FALSE
                              , genbean   => FALSE
                              , genTables => FALSE);
end;
/

XMLTypes can be associated with registered schemas during installation. The schemaValidate() or isSchemaValid() member functions can be called to verify the document structure. The isSchemaValid member function returns 1 for valid documents and 0 for invalid. schemaValidate returns throws an error for invalid documents. The following example shows an schema validation error.

declare
  xml_instance_text       varchar2(32767);
  xml_instance_doc        sys.xmltype;
begin
  xml_instance_text   :=
'<?xml version="1.0"?>
  <databaseProperties>
    <serviceName>DEV115</serviceName>
    <listenerPort>1520</listenerPort>

    <oracleHome>/oracle/db/dev115</oracleHome>
  </databaseProperties>';
  xml_instance_doc :=
             xmltype(xmldata=>xml_instance_text
                    , schema =>'http://goldthorp.com/xmlTypes/databaseProperties2.xsd');
  if xml_instance_doc.isSchemaValid() = 1
     then dbms_output.put_line('Schema is Valid');
  else
    xml_instance_doc.schemaValidate();
  end if;
end;
/
ERROR at line 1:
ORA-31154: invalid XML document
ORA-19202: Error occurred in XML processing
LSX-00291: value "1520" is less than minimum "1521" (inclusive)
ORA-06512: at "SYS.XMLTYPE", line 345
ORA-06512: at line 70

Read the W3C XML Schema Primer - http://www.w3.org/TR/xmlschema-0/ for an overview of XML Schema validation. There's an open source utility called Trang that can be used to generate an XML schema from an instance document. It can be downloaded from http://www.thaiopensource.com/download/.