How Db2 chooses an XML schema for DSN_XMLVALIDATE

When you execute DSN_XMLVALIDATE as part of inserting into or updating 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 DSN_XMLVALIDATE invocation includes an XML schema name, Db2 uses the XML schema that is uniquely identified by the XML schema name.
  • If the DSN_XMLVALIDATE invocation includes a target namespace or a schema location hint, or both, Db2 searches the Db2 XML schema repository (XSR) for an XML schema name that corresponds to the combination of the target namespace and schema location hint.
    • If exactly one XML schema name corresponds to the combination of the target namespace and schema location hint, Db2 uses that XML schema name.
    • If multiple XML schema names correspond to the combination of the target namespace and schema location hint, Db2 uses the XML schema with the most recent registration timestamp.
    • If no XML schema name corresponds to the combination of the target namespace and schema location hint, Db2 issues an error.
  • If the DSN_XMLVALIDATE invocation does not specify an XML schema name or target namespace and schema location hint, Db2 examines the instance document (the document that is being validated) to determine the XML schema.
    1. Db2 determines a target namespace and a schema location hint from the instance document, in the following way:
      • If the root element node in the instance document contains a namespace name, Db2 uses that namespace name as the target namespace name.
      • If the root element node in the instance document does not contain a namespace name, Db2 does not use a target namespace name.
      • If the root element node in the instance document contains an xsi:schemaLocation attribute, Db2 uses its value as a schema location hint.
      • If the root element node in the instance document contains an xsi:noNamespaceSchemaLocation attribute, Db2 uses its value as the schema location hint for schemas with no target namespace.
    2. Db2 searches the Db2 XML schema repository (XSR) for an XML schema name that corresponds to the target namespace, schema location hint, or both, from the instance document.
      • If exactly one XML schema name corresponds to the combination of the target namespace and schema location hint, Db2 uses that XML schema name.
      • If multiple XML schema names correspond to the combination of the target namespace and schema location hint, Db2 uses the XML schema with the most recent registration timestamp.
      • If no XML schema name corresponds to the combination of the target namespace and schema location hint, Db2 issues an error.

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)

You execute the following INSERT statements:

INSERT INTO PURCHASEORDERV5 VALUES(1,                        1 
 DSN_XMLVALIDATE('<po:purchaseOrder xmlns:po="http://www.example.com/PO1">
 …
 </po:purchaseOrder>')
);
INSERT INTO PURCHASEORDERV5 VALUES(2,                        2 
 DSN_XMLVALIDATE('<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>','SYSXSR.PO2')
);                                     
INSERT INTO PURCHASEORDERV5 VALUES(2,                        3 
 DSN_XMLVALIDATE('<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>',
 'http://www.example.com/PO2')
);
INSERT INTO purchase_orders VALUES(3,                        4 
 DSN_XMLVALIDATE(
 '<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 schema that Db2 uses, and the reasons for that choice.

Insert statement number XML schemas that are used for validation, in order of choice Reason
 1  PO1 The DSN_XMLVALIDATE invocation does not specify an XML schema or target namespace and schema location hint, so Db2 uses the information in the instance document. 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 The DSN_XMLVALIDATE invocation specifies XML schema SYSXSR.PO2.
 3  PO4 The DSN_XMLVALIDATE invocation specifies namespace http://www.example.com/PO2. Two XML schemas, PO2 and PO4, have that target namespace. Db2 uses PO4, because it has the later timestamp.
 4  PO3 The DSN_XMLVALIDATE invocation does not specify an XML schema or target namespace and schema location hint, so Db2 uses the information in the instance document. The root element node in the instance document contains an xsi:noNamespaceSchemaLocation attribute with value http://www.example.com/PO3.xsd, so Db2 uses XML schema PO3, which has no target namespace, and the schema location http://www.example.com/PO3.xsd.