Lesson 7: Validating XML documents against XML schemas

This lesson shows you how to validate XML documents. You can validate your XML documents against XML schemas only; validation against DTDs is not supported. However, although you cannot validate against DTDs, you can insert documents that contain a DOCTYPE or that refer to DTDs.

You can use tools such as those in IBM® Rational® Application Developer to help generate XML schemas from various sources, including DTDs, tables, and XML documents.

Before you can validate, you must register your XML schema with the built-in XML schema repository (XSR). This process involves registering each XML schema document that makes up the XML schema and then completing the registration. One method of registering an XML schema is through commands.

To register the schema document and complete the registration of the posample.customer XML schema, run the following command. (Because this XML schema consists of only one schema document, you can use a single command to both register the document and complete the registration.) The command specifies the absolute path to the sqllib/samples/xml directory. If the path on your system does not begin with c:/sqllib/, modify the file path in the command accordingly.
REGISTER XMLSCHEMA 'http://posample.org' 
FROM 'file:///c:/sqllib/samples/xml/customer.xsd' AS posample.customer COMPLETE~
You can verify that the XML schema was successfully registered by querying the SYSCAT.XSROBJECTS catalog view, which contains information about objects stored in the XSR. This query and its result, formatted for clarity, are as follows:

--------------------   --------------------

You can now use the XML schema for validation. You typically perform validation during an INSERT or UPDATE operation, using the XMLVALIDATE function. The INSERT or UPDATE operation for which you specify XMLVALIDATE occurs only if the validation succeeds.

To insert an XML document into the INFO column of the CUSTOMER table if the document is valid according to the posample.customer XML schema, issue the following statement:
'<customerinfo xmlns="http://posample.org" Cid="1003"> 
  <name>Robert Shoemaker</name> 
  <addr country="Canada"> 
    <street>1596 Baseline</street> 
    <pcode-zip>N8X 7F8</pcode-zip> 
  <phone type="work">905-555-7258</phone> 
  <phone type="home">416-555-2937</phone> 
  <phone type="cell">905-555-8743</phone> 
  <phone type="cottage">613-555-3278</phone> 
</customerinfo>' PRESERVE WHITESPACE )
ACCORDING TO XMLSCHEMA ID posample.customer ))~

The XML document in this example is passed as character data. However, XMLVALIDATE operates only on XML data. Because the XML document is passed as character data, you must explicitly parse the data using the XMLPARSE function. The XMLPARSE function parses its argument as an XML document and returns an XML value.

The Db2® database server performs implicit parsing for some operations, For example, implcit parsing occurs when you assign a host variable, parameter marker, or SQL expression with a string data type (character, graphic or binary) to an XML column in an INSERT, UPDATE, DELETE, or MERGE statement. For information about XML parsing, see the related link.

To verify that the validation and insert were successful, query the INFO column:
SELECT Info FROM Customer~
This query should return three XML documents, one of which is the document that you just inserted.

Return to the tutorial