Decomposition annotation example: A value mapped to multiple tables
A single value from an XML document can be mapped to multiple tables. This example shows how to annotate an XML schema document to map a single value to two tables.
<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>To map a textbook's ISBN to the following two tables, you need to create two mappings on the <isbn> element. This can be done by adding multiple <db2-xdb:rowSetMapping> elements to the <isbn> element declaration in the XML schema document.
| ISBN | TITLE |
|---|---|
| 0-11-011111-0 | Programming with XML |
| ISBN | SCHOOL |
|---|---|
| 0-11-011111-0 | University of London |
The following fragment of the XML schema document shows how two mappings are added to the <isbn> element declaration to specify the mappings to two tables. The value of the title attribute and <university> element are also included in the mappings.
<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>TEXTBOOKS</db2-xdb:rowSet>
<db2-xdb:column>ISBN</db2-xdb:column>
</db2-xdb:rowSetMapping>
<db2-xdb:rowSetMapping>
<db2-xdb:rowSet>SCHOOLPUBS</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:element name="publicationDate" type="xs:gYear"/>
<xs:element name="university" type="xs:string" maxOccurs="unbounded">
<xs:annotation>
<xs:appinfo>
<db2-xdb:rowSetMapping>
<db2-xdb:rowSet>SCHOOLPUBS</db2-xdb:rowSet>
<db2-xdb:column>SCHOOL</db2-xdb:column>
</db2-xdb:rowSetMapping>
</xs:appinfo>
</xs:annotation>
</xs:element>
</xs:sequence>
<xs:attribute name="title" type="xs:string" use="required">
<xs:annotation>
<xs:appinfo>
<db2-xdb:rowSetMapping>
<db2-xdb:rowSet>TEXTBOOKS</db2-xdb:rowSet>
<db2-xdb:column>TITLE</db2-xdb:column>
</db2-xdb:rowSetMapping>
</xs:appinfo>
</xs:annotation>
</xs:attribute>
</xs:complexType>
</xs:element>
Complex types that appear multiple times
If a complex type is referred to in multiple places in an XML schema, you can use the db2-xdb:locationPath annotation to map it to different tables and columns based on its location in the schema.
In this case, the complex type element or attribute declaration needs to be annotated with multiple <db2-xdb:rowSetMapping> annotations (one for each mapping) where each mapping is distinguished by the db2-xdb:locationPath attribute.