Revalidation after XML document updates

After you update an XML document in a column that has an XML type modifier, Db2 revalidates all or part of the document.

If the XML type modifier includes several XML schemas, Db2 uses the same XML schema for revalidation that it used for the original validation.

If you update an entire document, Db2 revalidates the entire document. However, if you use the XMLMODIFY function to update only a portion of the document, Db2 might need to validate only the updated portion.

The following table lists the rules that Db2 uses for determining how much of a document to revalidate.

XMLMODIFY option Revalidation behavior
insert nodes Revalidation behavior depends on the source expression and the type of insert operation:
  • If the source expression is a sequence of attribute nodes, Db2 revalidates each attribute in that sequence under its new parent and the parent of the target node.
  • If the operation is insert nodes…into, Db2 revalidates the parent of the target node.
  • If the operation is insert nodes…before or insert nodes…after, Db2 revalidates the parent of the target node.
delete nodes Db2 revalidates from the common ancestor of the deleted nodes.
replace node Db2 revalidates the parent of the target node.
replace value of node Db2 revalidates the target node and the parent of the target node.
Any update operation Db2 revalidates from the target node's topmost ancestor with an xsi:type attribute.

Because Db2 revalidates only the changed portion of an XML document, some constraints that are defined in the XML schema on the instance document cannot be enforced during revalidation if they require other portions of the instance document. Those constraints are:

  • Indicators that enforce uniqueness of elements or attributes

    If a uniqueness constraint is specified on an ancestor of a node that is to be revalidated, the constraint is not enforced.

  • ID and IDREF attributes

    Db2 does not validate ID and IDREF attributes during revalidation.

  • Key and keyref elements

    If a key element and a corresponding keyref element appear in the node that is to be revalidated, Db2 validates them. If the key element or the keyref element appear elsewhere in the document, Db2 does not validate them.

Example: Suppose that table PURCHASEORDERV4 is defined like this:

CREATE TABLE PURCHASEORDERV4(
 ID INT NOT NULL,
 CONTENT XML(XMLSCHEMA ID SYSXSR.PO1
 ELEMENT "purchaseOrder"))

Insert a row into the PURCHASEORDERV4 table:

INSERT INTO PURCHASEORDERV4 VALUES(1,
 '<po:purchaseOrder xmlns:po="http://www.example.com/PO1">
   <shipTo exportCode="1" xsi:type="ipo:UKAddress">
    <name>Helen Zoe</name>
    <street>55 Eden Street</street>
    <city>San Jose</city>
    <state>CA</state>
    <postcode>95160</postcode>
   </shipTo>
 …
   <items>
    <item partNum="833-AA">
      <productName>Lapis necklace</productName>
      <quantity>1</quantity>
      <USPrice>99.95</USPrice>
      <ipo:comment>Want this for the holidays!</ipo:comment>
    </item>
    <item partNum="945-ZG">
      <productName>Sapphire Bracelet</productName>
      <quantity>2</quantity>
      <USPrice>178.99</USPrice>
      <shipDate>2009-01-03</shipDate>
    </item>
   </items>
 </po:purchaseOrder>
);

Use XMLMODIFY to make the following updates:

XMLMODIFY operation: Add a <shipDate> element to the first <item> element in the document in column CONTENT:

UPDATE PURCHASEORDERV4
 SET CONTENT = 
  XMLMODIFY ('declare namespace po="http://www.example.com/PO1";
  insert node $x into /po:purchaseOrder/items/item[1]',
  XMLELEMENT(name "shipDate", '2009-01-20') as "x")
 WHERE ID = 1;

Resulting revalidation: Db2 revalidates the <items> element in the document.

XMLMODIFY operation: Add a country attribute to the <shipTo> element in the document in column CONTENT:

UPDATE PURCHASEORDERV4
 SET CONTENT = 
  XMLMODIFY (
  'declare namespace po="http://www.example.com/PO1";
  insert node $x/@country into /po:purchaseOrder/shipTo',
  XMLELEMENT(name "shipTo",
  XMLATTRIBUTES('US' as "country")) as "x") 
 WHERE ID = 1;

Resulting revalidation: Db2 revalidates only the shipTo element in the document.

XMLMODIFY operation: Replace the value of the <shipDate> element in the first <item> element:

UPDATE PURCHASEORDERV4
 SET content = XMLMODIFY (
  'declare namespace po="http://www.example.com/PO1";
  replace value of node
  /po:purchaseOrder/items/item[1]/shipDate
  with "2009-02-15"') 
 WHERE ID = 1;

Resulting revalidation: Db2 revalidates only the first <item> element.

XMLMODIFY operation: Delete the second <item> element in the document.

UPDATE PURCHASEORDERV4
 SET content = XMLMODIFY('delete nodes //item[2]')
 WHERE ID = 1;

Resulting revalidation: Db2 revalidates only the <items> element, which is the parent node of the deleted <item> element.