Definitive XML Schema

Definitive XML Schema

(pwalmsley@datypic.com)

ISBN: 0132886723

2nd edition, , Prentice Hall PTR.

Chapter 20: XML information modeling

Book examples

Example 20-1. A simple representation of relational entities in XML
<model>
  <order>
    <number>1234</number>
    <total>213.12</total>
  </order>
  <customer>
    <number>12345</number>
    <firstName>Priscilla</firstName>
    <lastName>Walmsley</lastName>
  </customer>
  <address>
    <type>Billing</type>
    <line1>123 Main Street</line1>
    <line2>Apartment 2B</line2>
    <city>Traverse City</city>
    <state>MI</state>
    <zip>49684</zip>
  </address>
  <address>
    <type>Shipping</type>
    <line1>PO Box 9999999</line1>
    <city>Traverse City</city>
    <state>MI</state>
    <zip>49686</zip>
  </address>
  <lineItem>
    <giftWrap>bday</giftWrap>
    <number>557</number>
    <size>12</size>
    <color>blue</color>
    <quantity>1</quantity>
  </lineItem>
  <lineItem>
    <number>443</number>
    <size>L</size>
    <color>tan</color>
    <quantity>2</quantity>
  </lineItem>
  <product>
    <number>557</number>
    <name>Short-Sleeved Linen Blouse</name>
    <availableSizes>2 4 6 8 10 12 14</availableSizes>
    <availableColors>blue red</availableColors>
    <desc>Our best-selling shirt!</desc>
  </product>
  <product>
    <number>563</number>
    <name>Ten-Gallon Hat</name>
    <availableSizes>S M L</availableSizes>
  </product>
  <product>
    <number>443</number>
    <name>Deluxe Golf Umbrella</name>
    <availableColors>tan black</availableColors>
    <desc>Protect yourself from the rain!</desc>
  </product>
</model>
Example 20-2. Relationship with repetition
<report>
  <order>
    <number>1234</number>
    <total>213.12</total>
    <lineItem>
      <giftWrap>bday</giftWrap>
      <size>12</size>
      <color>blue</color>
      <quantity>1</quantity>
       <product>
        <number>557</number>
        <name>Short-Sleeved Linen Blouse</name>
        <availableColors>blue red</availableColors>
      </product>
    </lineItem>
    <lineItem>
      <size>L</size>
      <color>tan</color>
      <quantity>2</quantity>
      <product>
        <number>443</number>
        <name>Deluxe Golf Umbrella</name>
        <availableColors>tan black</availableColors>
      </product>
    </lineItem>
  </order>
  <order>
    <number>5678</number>
    <total>245.55</total>
    <lineItem>
      <giftWrap>bday</giftWrap>
      <size>12</size>
      <color>blue</color>
      <quantity>1</quantity>
      <product>
        <number>557</number>
        <name>Short-Sleeved Linen Blouse</name>
        <availableColors>blue red</availableColors>
      </product>
    </lineItem>
    <lineItem>
      <size>L</size>
      <quantity>1</quantity>
      <product>
        <number>563</number>
        <name>Ten-Gallon Hat</name>
        <availableSizes>S M L</availableSizes>
      </product>
    </lineItem>
  </order>
</report>
Example 20-3. Relationship via reference
<report>
  <order>
    <number>1234</number>
    <total>213.12</total>
    <lineItem>
      <giftWrap>bday</giftWrap>
      <size>12</size>
      <color>blue</color>
      <quantity>1</quantity>
      <productRef ref="557"/>
    </lineItem>
    <lineItem>
      <size>L</size>
      <color>tan</color>
      <quantity>2</quantity>
      <productRef ref="443"/>
    </lineItem>
  </order>
  <order>
    <number>5678</number>
    <total>245.55</total>
    <lineItem>
      <giftWrap>bday</giftWrap>
      <size>12</size>
      <color>blue</color>
      <quantity>1</quantity>
      <productRef ref="557"/>
    </lineItem>
    <lineItem>
      <size>L</size>
      <quantity>1</quantity>
      <productRef ref="563"/>
    </lineItem>
  </order>
  <product>
    <number>443</number>
    <name>Deluxe Golf Umbrella</name>
    <availableColors>tan black</availableColors>
  </product>
  <product>
    <number>557</number>
    <name>Short-Sleeved Linen Blouse</name>
    <availableColors>blue red</availableColors>
  </product>
  <product>
    <number>563</number>
    <name>Ten-Gallon Hat</name>
    <availableSizes>S M L</availableSizes>
  </product>
</report>
Example 20-4. Using identity constraints to validate references
<xs:element name="report" type="ReportType">
  <xs:key name="productKey">
    <xs:selector xpath=".//product"/>
    <xs:field xpath="number"/>
  </xs:key>
  <xs:keyref name="productKeyRef" refer="productKey">
    <xs:selector xpath=".//productRef"/>
    <xs:field xpath="@ref"/>
  </xs:keyref>
</xs:element>
Example 20-5. Using a separate relationship element
<report>
  <order>
    <number>1234</number>
    <total>213.12</total>
  </order>
  <order>
    <number>5678</number>
    <total>245.55</total>
  </order>
  <product>
    <number>443</number>
    <name>Deluxe Golf Umbrella</name>
    <availableColors>tan black</availableColors>
  </product>
  <product>
    <number>557</number>
    <name>Short-Sleeved Linen Blouse</name>
    <availableColors>blue red</availableColors>
  </product>
  <product>
    <number>563</number>
    <name>Ten-Gallon Hat</name>
    <availableSizes>S M L</availableSizes>
  </product>
  <orderProductRelationship>
    <orderRef ref="1234"/>
    <productRef ref="557"/>
    <giftWrap>bday</giftWrap>
    <size>12</size>
    <color>blue</color>
    <quantity>1</quantity>
  </orderProductRelationship>
  <orderProductRelationship>
    <orderRef ref="1234"/>
    <productRef ref="443"/>
    <size>L</size>
    <color>tan</color>
    <quantity>2</quantity>
  </orderProductRelationship>
  <orderProductRelationship>
    <orderRef ref="5678"/>
    <productRef ref="557"/>
    <giftWrap>bday</giftWrap>
    <size>12</size>
    <color>blue</color>
    <quantity>1</quantity>
  </orderProductRelationship>
  <orderProductRelationship>
    <orderRef ref="5678"/>
    <productRef ref="563"/>
    <size>L</size>
    <quantity>1</quantity>
  </orderProductRelationship>
</report>
Example 20-6. An abstract product type
<xs:complexType name="ProductType" abstract="true">
  <xs:sequence>
    <xs:element name="number" type="xs:integer"/>
    <xs:element name="name" type="xs:string"/>
    <xs:element name="desc" type="xs:string"/>
  </xs:sequence>
</xs:complexType>
Example 20-7. A derived shirt type
<xs:element name="shirt" type="ShirtType"/>
<xs:complexType name="ShirtType">
  <xs:complexContent>
    <xs:extension base="ProductType">
      <xs:sequence>
        <xs:element name="fabric" type="xs:string"/>
        <xs:element name="availableSizes"
                    type="AvailableShirtSizesType"/>
      </xs:sequence>
    </xs:extension>
  </xs:complexContent>
</xs:complexType>
<xs:simpleType name="AvailableShirtSizesType">
  <xs:list>
    <xs:simpleType>
      <xs:restriction base="xs:integer">
        <xs:minInclusive value="2"/>
        <xs:maxInclusive value="18"/>
      </xs:restriction>
    </xs:simpleType>
  </xs:list>
</xs:simpleType>
Example 20-8. Instance of the derived shirt type
<shirt>
  <number>557</number>
  <name>Short-Sleeved Linen Blouse</name>
  <desc>Our best-selling shirt!</desc>
  <fabric>linen</fabric>
  <availableSizes>2 4 6 8 10 12 14</availableSizes>
</shirt>
Example 20-9. A derived hat type
<xs:element name="hat" type="HatType"/>
<xs:complexType name="HatType">
  <xs:complexContent>
    <xs:extension base="ProductType">
      <xs:sequence>
        <xs:element name="spfRating" type="xs:integer"/>
        <xs:element name="availableSizes"
                    type="AvailableHatSizesType"/>
      </xs:sequence>
    </xs:extension>
  </xs:complexContent>
</xs:complexType>
<xs:simpleType name="AvailableHatSizesType">
  <xs:list>
    <xs:simpleType>
      <xs:restriction base="xs:string"/>
    </xs:simpleType>
  </xs:list>
</xs:simpleType>
Example 20-10. Product property group
<xs:group name="ProductProperties">
  <xs:sequence>
    <xs:element name="number" type="xs:integer"/>
    <xs:element name="name" type="xs:string"/>
    <xs:element name="desc" type="xs:string"/>
  </xs:sequence>
</xs:group>
Example 20-11. Shirt type that uses the product property group
<xs:complexType name="ShirtType">
  <xs:sequence>
    <xs:group ref="ProductProperties"/>
    <xs:element name="fabric" type="xs:string"/>
    <xs:element name="availableSizes"
                type="AvailableShirtSizesType"/>
  </xs:sequence>
</xs:complexType>
Example 20-12. Using a child element for composition
<xs:complexType name="ProductType">
  <xs:sequence>
    <xs:element name="number" type="xs:integer"/>
    <xs:element name="name" type="xs:string"/>
    <xs:element name="desc" type="xs:string"/>
  </xs:sequence>
</xs:complexType>
<xs:complexType name="ShirtType">
  <xs:sequence>
    <xs:element name="productProperties" type="ProductType"/>
    <xs:element name="fabric" type="xs:string"/>
    <xs:element name="availableSizes"
                type="AvailableShirtSizesType"/>
  </xs:sequence>
</xs:complexType>
Example 20-13. Instance of shirt type using child element
<shirt>
  <productProperties>
    <number>557</number>
    <name>Short-Sleeved Linen Blouse</name>
    <desc>Our best-selling shirt!</desc>
  </productProperties>
  <fabric>linen</fabric>
  <availableSizes>2 4 6 8 10 12 14</availableSizes>
</shirt>
Example 20-14. A flat order example
<order>
  <number>12345</number>
  <date>2012-10-31</date>
  <customerNumber>12345</customerNumber>
  <customerName>Priscilla Walmsley</customerName>
  <billToAddressLine>123 Main Street</billToAddressLine>
  <billToCity>Traverse City</billToCity>
  <billToState>MI</billToState>
  <billToZip>49684</billToZip>
  <shipToAddressLine>5100 Garfield Road</shipToAddressLine>
  <shipToCity>Hillsborough</shipToCity>
  <shipToState>NJ</shipToState>
  <shipToZip>08876</shipToZip>
  <!--...-->
  <lineItem>
    <number>557</number>
    <!--...-->
  </lineItem>
  <lineItem>
    <number>443</number>
    <!--...-->
  </lineItem>
</order>
Example 20-15. A flat order schema
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="order" type="OrderType"/>
  <xs:complexType name="OrderType">
    <xs:sequence>
      <xs:element name="number" type="xs:integer"/>
      <xs:element name="date" type="xs:date"/>
      <xs:element name="customerNumber" type="xs:integer"/>
      <xs:element name="customerName" type="xs:string"/>
      <xs:element name="billToAddressLine" type="xs:string"
                  maxOccurs="unbounded"/>
      <xs:element name="billToCity" type="xs:string"/>
      <xs:element name="billToState" type="xs:string"/>
      <xs:element name="billToZip" type="xs:string"/>
      <xs:element name="shipToAddressLine" type="xs:string"
                  maxOccurs="unbounded"/>
      <xs:element name="shipToCity" type="xs:string"/>
      <xs:element name="shipToState" type="xs:string"/>
      <xs:element name="shipToZip" type="xs:string"/>
      <xs:element name="lineItem" type="LineItemType"
                  maxOccurs="unbounded"/>
    </xs:sequence>
  </xs:complexType>
  <xs:complexType name="LineItemType">
    <xs:sequence>
      <xs:element name="number" type="xs:integer"/>
      <!--...-->
    </xs:sequence>
  </xs:complexType>
</xs:schema>
Example 20-16. More structured address information
<order>
  <!--...-->
  <billToAddress>
    <addressLine>123 Main St.</addressLine>
    <city>Traverse City</city>
    <state>MI</state>
    <zip>49684</zip>
  </billToAddress>
  <shipToAddress>
    <addressLine>5100 Garfield Road</addressLine>
    <city>Hillsborough</city>
    <state>NJ</state>
    <zip>08876</zip>
  </shipToAddress>
  <!--...-->
</order>
Example 20-17. AddressType and revised OrderType definitions
<xs:complexType name="OrderType">
  <xs:sequence>
    <xs:element name="number" type="xs:integer"/>
    <xs:element name="date" type="xs:date"/>
    <xs:element name="customerNumber" type="xs:integer"/>
    <xs:element name="customerName" type="xs:string"/>
    <xs:element name="billToAddress" type="AddressType"/>
    <xs:element name="shipToAddress" type="AddressType"/>
    <xs:element name="lineItem" type="LineItemType"
                maxOccurs="unbounded"/>
  </xs:sequence>
</xs:complexType>
<xs:complexType name="AddressType">
  <xs:sequence>
    <xs:element name="addressLine" type="xs:string"/>
    <xs:element name="city" type="xs:string"/>
    <xs:element name="state" type="xs:string"/>
    <xs:element name="zip" type="xs:string"/>
  </xs:sequence>
</xs:complexType>
Example 20-18. A repeating container element lineItems
<order>
  <!--...-->
  <lineItems>
    <lineItem>
      <number>557</number>
      <!--...-->
    </lineItem>
    <lineItem>
      <number>443</number>
      <!--...-->
    </lineItem>
  </lineItems>
</order>
Example 20-19. Specific element names
<product>
  <!--...-->
  <availableColors>blue red</availableColors>
  <availableSizes>S M L</availableSizes>
  <monogrammable>true</monogrammable>
  <weight units="g">113</weight>
</product>
Example 20-20. Generic element names
<product>
  <!--...-->
  <feature name="availableColors">blue red</feature>
  <feature name="availableSizes">S M L</feature>
  <feature name="monogrammable">true</feature>
  <feature name="weight">113</feature>
</product>
Example 20-21. ProductType with generic feature capability
<xs:complexType name="ProductType">
  <xs:sequence>
    <xs:element name="number" type="xs:integer"/>
    <xs:element name="name" type="xs:string"/>
    <xs:element name="desc" type="xs:string"/>
    <xs:element name="feature" maxOccurs="unbounded"
                type="FeatureType"/>
  </xs:sequence>
</xs:complexType>
<xs:complexType name="FeatureType">
  <xs:simpleContent>
    <xs:extension base="xs:string">
      <xs:attribute name="name" type="xs:string"/>
    </xs:extension>
  </xs:simpleContent>
</xs:complexType>
Datypic XML Schema Services