How Db2 chooses an XML schema from an XML type modifier

You can include more than one XML schema in an XML type modifier. When you insert into or update an XML column, Db2 chooses one XML schema to do validation.

Db2 uses the following process to determine which XML schema to use.

  • If the operation is an update operation, and an XML schema that is specified by the XML type modifier has already been used to validate the original document, Db2 uses the same XML schema to validate the updated document.
  • If there is only one XML schema whose target namespace matches the namespace name of the root element node in the document that is being validated (the XML instance document), Db2 chooses that XML schema to validate the XML document.
  • If there is more than one XML schema with a target namespace that matches the namespace name of the root element, Db2 chooses an XML schema by using the schema location hint. The root element node of an XML instance document can contain an xsi:schemaLocation attribute. That attribute consists of one or more pairs of URI references, separated by white space. The first member of each pair is a namespace name, and the second member of the pair is a URI that describes where to find an appropriate schema document for that namespace. The second member of each pair is the schema location hint for the namespace name that is specified in the first member.

    For example, this is a schema location attribute:

    xsi:schemaLocation="http://www.example.com/PO2 http://www.example.com/PO4.xsd"
    The first member of the pair, http://www.example.com/PO2, is the namespace name. The second member of the pair, http://www.example.com/PO4.xsd, is the URI that provides the schema location hint.

    Db2 uses the schema location hint to choose an XML schema in the following way:

    1. If the root element node contains an xsi:schemaLocation attribute, Db2 searches the attribute value for a schema location hint with a corresponding namespace name that matches the namespace name in the root element node.
    2. If Db2 finds a schema location hint, Db2 uses the hint to identify an XML schema whose schema location URI is identical to the schema location hint. Db2 validates the input document against that schema.
    3. If the root element does not contain an xsi:schemaLocation attribute, or the xsi:schemaLocation attribute does not contain a schema location hint with a corresponding namespace name that matches the namespace name in the root element node, Db2 uses the XML schema with the same target namespace and the latest registration timestamp.
    Recommendation: Include a schema location hint in instance documents, to simplify identification of the correct XML schema.
  • If the root element of the XML instance document does not have a namespace name, only XML schemas with no target namespace are candidates for use in validation. Db2 chooses an XML schema in the following way:
    1. If a single XML schema in the XML type modifier has no target namespace, Db2 uses that XML schema for validation.
    2. If more than one XML schema in the XML type modifier has no target namespace, and the XML instance document contains an xsi:noNamespaceSchemaLocation attribute, Db2 uses the value of xsi:noNamespaceSchemaLocation, which is the schema location hint, to choose an XML schema.
    3. If the root element does not contain the xsi:noNamespaceSchemaLocation attribute, or the schema location hint does not match the schema location URI of any XML schema in the XML type modifier, Db2 uses the XML schema with the latest registration timestamp from those XML schemas that have no target namespace.

Example: Suppose that the XML schema repository (XSR) contains the following XML schemas.

XML schema name Target namespace Schema location Timestamp when schema was registered in the XSR
PO1 http://www.example.com/PO1 http://www.example.com/PO1.xsd 2008-10-01 10:30:59.0100
PO2 http://www.example.com/PO2 http://www.example.com/PO2.xsd 2009-09-25 13:15:00.0200
PO3 No namespace http://www.example.com/PO3.xsd 2009-06-25 13:15:00.0200
PO4 http://www.example.com/PO2 http://www.example.com/PO4.xsd 2009-10-25 13:15:00.0200

Also suppose that table PURCHASEORDERV5 is defined like this:

CREATE TABLE PURCHASEORDERV5(
 ID INT NOT NULL,
 CONTENT XML(XMLSCHEMA ID SYSXSR.PO1, ID SYSXSR.PO2,
 ID SYSXSR.PO3, ID SYSXSR.PO4))

You execute the following INSERT statements:

INSERT INTO PURCHASEORDERV5 VALUES(1,                        1 
 '<po:purchaseOrder xmlns:po="http://www.example.com/PO1">
 …
 </po:purchaseOrder>'
);
INSERT INTO PURCHASEORDERV5 VALUES(2,                        2 
 '<po:purchaseOrder xmlns:po="http://www.example.com/PO2"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://www.example.com/PO2
 http://www.example.com/PO2.xsd">
 …
 </po:purchaseOrder>');                                     
INSERT INTO PURCHASEORDERV5 VALUES(2,                        3 
 '<po:purchaseOrder xmlns:po="http://www.example.com/PO2"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://www.example.com/PO2
 http://www.example.com/PO4.xsd">
 …
 </po:purchaseOrder>');
INSERT INTO purchase_orders VALUES(3,                        4 
 '<purchaseOrder xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:noNamespaceSchemaLocation="http://www.example.com/PO3.xsd">
 …
 </purchaseOrder>');

The following table lists the XML schemas that Db2 uses, and the reasons for those choices.

Insert statement number XML schemas that are used for validation, in order of choice Reason
 1  PO1 The namespace name in the root element of the instance document is http://www.example.com/PO1. This name matches only the target namespace for XML schema PO1.
 2  PO2, PO4 The namespace name in the root element in the instance document is http:// www.example.com/PO2. which matches the target namespace of XML schemas PO2 and PO4. The root element of the instance document also contains an xsi:schemaLocation attribute whose value provides the schema location hint http://www.example.com/PO2.xsd. The schema location hint matches the schema location for XML schema PO2. Therefore Db2 chooses PO2 to validate the instance document. If validation with PO2 fails, Db2 uses PO4.
 3  PO4, PO2 The namespace name in the root element in the instance document is http:// www.example.com/PO2. which matches the target namespace of XML schemas PO2 and PO4. The root element of the instance document also contains an xsi:schemaLocation attribute whose value provides the schema location hint http://www.example.com/PO4.xsd. The schema location hint matches the schema location for XML schema PO4. Therefore Db2 chooses PO4 to validate the instance document. If validation with PO4 fails, Db2 uses PO2.
 4  PO3 The root element of the instance document has no namespace name. XML schema PO3 has no target namespace. Therefore, Db2 uses PO3 for validation.