DB2 Version 9.7 for Linux, UNIX, and Windows

Decomposition annotation example: Multiple values from different contexts mapped to a single table

In annotated XML schema decomposition, you can map multiple values to the same table and column, such that a single column can contain values that have come from different parts of a document. This is possible by declaring multiple rowSets, as shown in this example.

For example, consider the following XML document:
<publications>
  <textbook title="Principles of Mathematics">
    <isbn>1-11-111111-1</isbn>
    <author>Alice Braun</author>
    <publisher>Math Pubs</publisher>
    <publicationDate>2002</publicationDate>
    <university>University of London</university>
  </textbook>
</publications>

You can map both the author and the publisher to the same table that contains contacts for a particular book.

Table 1. BOOKCONTACTS
ISBN CONTACT
1-11-111111-1 Alice Braun
1-11-111111-1 Math Pubs

The values in the CONTACT column of the resulting table come from different parts of the XML document: one row might contain an author's name (from the <author> element, while another row contains a publisher's name (from the <publisher> element).

The following XML schema document shows how multiple rowSets can be used to generate this table.

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           xmlns:db2-xdb="http://www.ibm.com/xmlns/prod/db2/xdb1"
           elementFormDefault="qualified" attributeFormDefault="unqualified">
  <xs:annotation>
    <xs:appinfo>
      <db2-xdb:table>
        <db2-xdb:name>BOOKCONTACTS</db2-xdb:name>
        <db2-xdb:rowSet>author_rowSet</db2-xdb:rowSet>
        <db2-xdb:rowSet>publisher_rowSet</db2-xdb:rowSet>
      </db2-xdb:table>
    </xs:appinfo>
  </xs:annotation>
  <xs:element name="publications">
     <xs:complexType>
       <xs:sequence>
         <xs:element name="textbook" maxOccurs="unbounded">
           <xs:complexType>
             <xs:sequence>
               <xs:element name="isbn" type="xs:string">
                 <xs:annotation>
                   <xs:appinfo>
                     <db2-xdb:rowSetMapping>
                       <db2-xdb:rowSet>author_rowSet</db2-xdb:rowSet>
                       <db2-xdb:column>ISBN</db2-xdb:column>
                     </db2-xdb:rowSetMapping>
                     <db2-xdb:rowSetMapping>
                       <db2-xdb:rowSet>publisher_rowSet</db2-xdb:rowSet>
                       <db2-xdb:column>ISBN</db2-xdb:column>
                     </db2-xdb:rowSetMapping>
                   </xs:appinfo>
                 </xs:annotation>
               </xs:element>
               <xs:element name="author" type="xs:string" maxOccurs="unbounded">
                 <xs:annotation>
                   <xs:appinfo>
                     <db2-xdb:rowSetMapping>
                       <db2-xdb:rowSet>author_rowSet</db2-xdb:rowSet>
                       <db2-xdb:column>CONTACT</db2-xdb:column>
                     </db2-xdb:rowSetMapping>
                   </xs:appinfo>
                 </xs:annotation>
               </xs:element>
               <xs:element name="publisher" type="xs:string">
                 <xs:annotation>
                   <xs:appinfo>
                     <db2-xdb:rowSetMapping>
                       <db2-xdb:rowSet>publisher_rowSet</db2-xdb:rowSet>
                       <db2-xdb:column>CONTACT</db2-xdb:column>
                     </db2-xdb:rowSetMapping>
                   </xs:appinfo>
                 </xs:annotation>
               </xs:element>
               <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"/>
           </xs:complexType>
         </xs:element>
       </xs:sequence>
     </xs:complexType>
  </xs:element>
</xs:schema>

Notice how the db2-xdb:rowSet mappings in each of the element declarations do not specify the name of a table, but rather the name of a rowSet. The rowSets are associated with the BOOKCONTACTS table in the <db2-xdb:table> annotation, which must be specified as a child of <xs:schema>.