Displaying detailed XML parsing and validation errors

You can use the information in the errorDialog output parameter from the XSR_GET_PARSING_DIAGNOSTICS stored procedure to resolve XML parsing and validation errors.

If errors occur when validating an XML document using the XMLVALIDATE scalar function, use the XSR_GET_PARSING_DIAGNOSTICS stored procedure to generate detailed error information. The following example uses the XSR_GET_PARSING_DIAGNOSTICS stored procedure with a simple XML schema and XML document to generate detailed validation error information.

Sample XML schema

The example uses the following XML schema definition (XSD). The schema has various elements, of which a type attribute of integer is assigned to the Age element.
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           targetNamespace="http://my.simpletest"
           xmlns="http://my.simpletest"
           elementFormDefault="qualified">
<xs:element name="Person">
 <xs:complexType>
   <xs:sequence>
     <xs:element name="Name">
      <xs:complexType>
       <xs:sequence>
        <xs:element name="FirstName" type="xs:string"/>
        <xs:element name="LastName" type="xs:string"/>
       </xs:sequence>
      </xs:complexType>
     </xs:element>
    <xs:element name="Age" type="xs:integer"/>
   </xs:sequence>
  </xs:complexType>
 </xs:element>
</xs:schema> 

Sample XML document

The following XML document will be validated by the sample XML schema. The document does not conform to some of the schema rules. The Age element is not numeric and the Notes® element is not defined in the schema as a subelement of the Person element.
<?xml version="1.0"?>
<Person xmlns="http://my.simpletest"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://my.simpletest http://my.simpletest/simple">
  <Name>
   <FirstName>Thomas</FirstName>
   <LastName>Watson</LastName>
  </Name>
  <Age>30x</Age>
  <Notes/>
<Person> 

Command to register the XML schema

Before using the XSR_GET_PARSING_DIAGNOSTICS stored procedure to validate an XML document, the XML schema used for validation must be registered in Db2® XML schema repository (XSR). The following REGISTER XMLSCHEMA command assumes the schema is in c:\temp\simpleschema.xsd and the SQL schema is USER1:
REGISTER XMLSCHEMA 'http://my.simpletest/simple'
   FROM 'file:///c:/temp/simpleschema.xsd'
   AS user1.simple COMPLETE

Call to generate detailed validation error information

The following call uses the XSR_GET_PARSING_DIAGNOSTICS stored procedure from the CLP to generate validation error information:
CALL XSR_GET_PARSING_DIAGNOSTICS(
blob('<?xml version="1.0"?>
<Person xmlns="http://my.simpletest"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://my.simpletest http://my.simpletest/simple">
<Name>
  <FirstName>Thomas</FirstName>
  <LastName>Watson</LastName>
 </Name>
 <Age>30x</Age>
 <Notes />
</Person>
'),'','','',1,?,?)@

Detailed validation error information

The call to the XSR_GET_PARSING_DIAGNOSTICS stored procedure returns the following output. The value of the errorDialog parameter is an XML document that contains the detailed validation error information. In the XML document, the contents of the errText, location, lineNum and colNum elements identifies the specific errors and the location of the errors. The following output is written to standard output when the previous call is run from a CLP command line:
Value of output parameters
--------------------------
Parameter Name  : ERRORDIALOG
Parameter Value : <ErrorLog>
<XML_Error parser="XML4C">
  <errCode>238</errCode>
  <errDomain>http://apache.org/xml/messages/XML4CErrors</errDomain>
  <errText>Datatype error: Type:InvalidDatatypeValueException, 
    Message:Value '30x' does not match regular expression facet '[+\-]?[0-9]+' .
  </errText>
  <lineNum>1</lineNum>
  <colNum>272</colNum>
  <location>/Person/Age</location>
  <schemaType>http://www.w3.org/2001/XMLSchema:integer</schemaType>
  <tokenCount>2</tokenCount>
  <token1>30x</token1>
  <token2>13</token2>
</XML_Error>
<XML_Error parser="XML4C">
  <errCode>2</errCode>
  <errDomain>http://apache.org/xml/messages/XMLValidity</errDomain>
  <errText>Unknown element 'Notes' </errText>
  <lineNum>1</lineNum>
  <colNum>282</colNum>
  <location>/Person</location>
  <schemaType>http://www.w3.org/2001/XMLSchema:integer</schemaType>
  <tokenCount>2</tokenCount>
  <token1>Notes</token1>
  <token2>37</token2>
</XML_Error>
<XML_Error parser="XML4C">
  <errCode>7</errCode>
  <errDomain>http://apache.org/xml/messages/XMLValidity</errDomain>
  <errText>Element 'Notes' is not valid for content model: '(Name,Age)' 
  </errText>
  <lineNum>1</lineNum>
  <colNum>292</colNum>
  <location>/Person</location>
  <schemaType>http://www.w3.org/2001/XMLSchema:anyType</schemaType>
  <tokenCount>2</tokenCount>
  <token1>Notes</token1>
  <token2>31</token2>
</XML_Error>
<DB2_Error>
  <sqlstate>2200M</sqlstate>
  <sqlcode>-16210</sqlcode>
  <errText>
    [IBM][CLI Driver][DB2/NT] SQL16210N  XML document contained a value "30x"
    that violates a facet constraint. Reason code = "13".  SQLSTATE=2200M
    </errText>
</DB2_Error>
</ErrorLog>

Parameter Name  : ERRORCOUNT
Parameter Value : 3
Return Status = 0