DB2 Version 10.1 for Linux, UNIX, and Windows

Scenario: Evolving an XML schema

The following scenario demonstrates the process of evolving an XML schema registered in the XML schema repository (XSR).

Jane, the manager of a small store, maintains a database in which all of the store products are listed in a number of XML documents. These XML product lists conform to the following schema:
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">

<xsd:complexType name="prodType">
  <xsd:sequence>
    <xsd:element name="name" type="xsd:string" />
    <xsd:element name="sku" type="xsd:string" />
    <xsd:element name="price" type="xsd:integer" />
  </xsd:sequence>
  <xsd:attribute name="color" type="xsd:string" />
  <xsd:attribute name="weight" type="xsd:integer" />
</xsd:complexType>

<xsd:element name="products">
  <xsd:complexType>
    <xsd:sequence>
      <xsd:element name="product" type="prodType" maxOccurs="unbounded" />
    </xsd:sequence>
  </xsd:complexType>
</xsd:element>

</xsd:schema> 
The XML schema was initially registered in the XSR using the commands:
REGISTER XMLSCHEMA 'http://product'
FROM 'file://c:/schemas/prod.xsd'
AS STORE.PROD
COMPLETE XMLSCHEMA STORE.PROD
After the XML schema was registered, the XML-formatted product lists were validated against it and inserted into the store database.
Jane decides that it would be better if the lists contained a description along with the name, stock keeping unit (SKU) and price of each product. Rather than create a new XML schema and have to re-validate all of the existing XML documents against it, Jane would prefer to update the original XML schema to accommodate the added product descriptions. A new "description" element needs to be added into the original XML schema:
<xsd:complexType name="prodType">
  <xsd:sequence>
    <xsd:element name="name" type="xsd:string" />
    <xsd:element name="sku" type="xsd:string" />
    <xsd:element name="price" type="xsd:integer" />
    <xsd:element name="description" type="xsd:string" minOccurs="0" />
  </xsd:sequence>
  <xsd:attribute name="color" type="xsd:string" />
  <xsd:attribute name="weight" type="xsd:integer" />
</xsd:complexType>
In the XML schema segment to insert, the "minOccurs" attribute is set to "0". This is important, for otherwise "description" would become a mandatory element as part of the content model, and all of the existing XML documents that were validated against the original schema and inserted into the database tables would no longer be in a valid state. To evolve an XML schema, the original and the new version of that schema must be compatible. For details, see Compatibility requirements for evolving an XML schema.
Before the update can proceed, the new XML schema needs to be registered in the XSR:
REGISTER XMLSCHEMA 'http://newproduct'
FROM 'file://c:/schemas/newprod.xsd'
AS STORE.NEWPROD
COMPLETE XMLSCHEMA STORE.NEWPROD
Jane now performs the update using the XSR_UPDATE stored procedure:
   CALL SYSPROC.XSR_UPDATE(
     'STORE',
     'PROD',
     'STORE',
     'NEWPROD',
     1)

The original XML schema is evolved. All of the external dependencies managed in the XSR for XML instance documents that were previously validated against the XML schema STORE.PROD are updated based on the contents of the XML schema STORE.NEWPROD. Because the dropnewschema parameter is set by passing a non-zero value, the new schema STORE.NEWPROD is dropped after the original schema is updated.

Any existing XML documents that have already been validated against the original XML schema are not validated again as a result of the update procedure. Instead, a check is performed during the update to confirm that the original and new XML schemas are compatible, thereby ensuring that any documents previously validated against the original XML schema will also be valid against the new one. In the previous example, setting the "minOccurs" attribute to "0" in the new "description" element is required for the two XML schemas to be compatible. Any XML documents that are inserted after the schema evolution will be validated against the newly updated version of STORE.PROD, and these documents can now contain "description" elements for each store product.