DB2 Version 9.7 for Linux, UNIX, and Windows

Decomposition annotation example: Mapping to an XML column

In annotated XML schema decomposition, you can map an XML fragment to a column defined using the XML data type.

Consider the following XML document:
<publications>
  <textbook title="Programming with XML">
    <isbn>0-11-011111-0<⁄isbn>
    <author>Mary Brown<⁄author>
    <author>Alex Page<⁄author>
    <publicationDate>2002<⁄publicationDate>
    <university>University of London<⁄university>
  <⁄textbook>
<⁄publications>

If you wanted to store the <textbook> XML element and book title as follows, you would add annotations to the declarations of the <textbook> element and title attribute in the corresponding XML schema document. The annotations should specify the DETAILS and TITLE columns, where the DETAILS column has been defined with the XML type, as well as the TEXTBOOKS table.

Table 1. TEXTBOOKS
TITLE DETAILS
Programming with XML
<textbook title="Programming with XML">
  <isbn>0-11-011111-0<⁄isbn>
  <author>Mary Brown<⁄author>
  <author>Alex Page<⁄author>
  <publicationDate>2002<⁄publicationDate>
  <university>University of London<⁄university>
<⁄textbook>

Depending on the annotation, an annotation can be specified in the schema document as an attribute or an element. Some annotations can be specified as either. Refer to the documentation for each specific annotation to determine how a particular annotation can be specified.

Specify the target table and column using either db2-xdb:rowSet and db2-xdb:column as attributes of <xs:element> or <xs:attribute> or the <db2-xdb:rowSet> and <db2-xdb:column> children elements of <db2-xdb:rowSetMapping>. Specifying these mappings as elements or attributes are equivalent.

The following fragment of the XML schema document shows how two mappings are added to the <textbook> element and title attribute by specifying annotations as attributes.
<xs:element name="publications">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="textbook" maxOccurs="unbounded"
                 db2-xdb:rowSet="TEXTBOOKS" db2-xdb:column="DETAILS">
        <xs:complexType>
          <xs:sequence>
            <xs:element name="isbn" type="xs:string"⁄>
            <xs:element name="author" type="xs:string" maxOccurs="unbounded"⁄>
            <xs:element name="publicationDate" type="xs:gYear"⁄>
            <xs:element name="university" type="xs:string" maxOccurs="unbounded"⁄>
          <⁄xs:sequence>
         <xs:attribute name="title" type="xs:string" use="required"
                       db2-xdb:rowSet="TEXTBOOKS" db2-xdb:column="TITLE"⁄>
        <⁄xs:complexType>
      <⁄xs:element>
    <⁄xs:sequence>
  <⁄xs:complexType>
<⁄xs:element>

The db2-xdb:rowSet annotations specify the name of the target table, and the db2-xdb:column annotations specify the name of the target column. Because the <textbook> element is of complex type and contains complex content, and the db2-xdb:contentHandling annotation was not specified, by default, all markup within the element (including its start and end tags) is inserted into the XML column according to the serializeSubtree setting of db2-xdb:contentHandling. Whitespace within the XML document is preserved. Refer to the db2-xdb:contentHandling documentation for more detail.