Definitive XML Schema

Definitive XML Schema

(pwalmsley@datypic.com)

ISBN: 0132886723

2nd edition, , Prentice Hall PTR.

Chapter 10: Union and list types

Full example

This example illustrates union and list types.

Instance (chapter10.xml)
<sizes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:noNamespaceSchemaLocation="chapter11.xsd">
  <size>12</size>
  <size>medium</size>
  <smallSize>6</smallSize>
  <smallSize>small</smallSize>
  <internationalSize>12</internationalSize>
  <internationalSize>24</internationalSize>
  <internationalSize>large</internationalSize>
  <availableSizes>10 large 2</availableSizes>
  <applicableSizes>small medium large</applicableSizes>
</sizes>
Schema (chapter10.xsd)
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="sizes" type="SizesType"/>
  <xs:complexType name="SizesType">
    <xs:choice maxOccurs="unbounded">
      <xs:element name="size" type="SizeType"/>
      <xs:element name="smallSize" type="SmallSizeType"/>
      <xs:element name="internationalSize"
                   type="InternationalSizeType"/>
      <xs:element name="availableSizes"
                   type="AvailableSizesType"/>
      <xs:element name="applicableSizes"
                   type="ApplicableSizesType"/>
    </xs:choice>
  </xs:complexType>
  <xs:simpleType name="SizeType">
    <xs:union memberTypes="DressSizeType">
      <xs:simpleType>
        <xs:restriction base="xs:token">
          <xs:enumeration value="small"/>
          <xs:enumeration value="medium"/>
          <xs:enumeration value="large"/>
        </xs:restriction>
      </xs:simpleType>
    </xs:union>
  </xs:simpleType>
  <xs:simpleType name="DressSizeType">
    <xs:restriction base="xs:integer">
      <xs:minInclusive value="2"/>
      <xs:maxInclusive value="18"/>
    </xs:restriction>
  </xs:simpleType>
  <xs:simpleType name="SmallSizeType">
    <xs:restriction base="SizeType">
      <xs:enumeration value="2"/>
      <xs:enumeration value="4"/>
      <xs:enumeration value="6"/>
      <xs:enumeration value="small"/>
    </xs:restriction>
  </xs:simpleType>
  <xs:simpleType name="InternationalSizeType">
    <xs:union memberTypes="SizeType">
      <xs:simpleType>
        <xs:restriction base="xs:integer">
          <xs:minInclusive value="24"/>
          <xs:maxInclusive value="54"/>
        </xs:restriction>
      </xs:simpleType>
    </xs:union>
  </xs:simpleType>
  <xs:simpleType name="AvailableSizesType">
    <xs:list itemType="SizeType"/>
  </xs:simpleType>
  <xs:simpleType name="ApplicableSizesType">
    <xs:restriction>
      <xs:simpleType>
        <xs:list itemType="SizeType"/>
      </xs:simpleType>
      <xs:enumeration value="small medium large"/>
      <xs:enumeration value="2 4 6 8 10 12 14 16 18"/>
    </xs:restriction>
  </xs:simpleType>
</xs:schema>
Datypic XML Schema Services

Book examples

Example 10-1. Defining a union type
<xs:simpleType name="SizeType">
  <xs:union>
    <xs:simpleType>
      <xs:restriction base="xs:integer">
        <xs:minInclusive value="2"/>
        <xs:maxInclusive value="18"/>
      </xs:restriction>
    </xs:simpleType>
    <xs:simpleType>
      <xs:restriction base="xs:token">
        <xs:enumeration value="small"/>
        <xs:enumeration value="medium"/>
        <xs:enumeration value="large"/>
      </xs:restriction>
    </xs:simpleType>
  </xs:union>
</xs:simpleType>
Example 10-2. Using the memberTypes attribute
<xs:simpleType name="SizeType">
  <xs:union memberTypes="DressSizeType SMLSizeType"/>
</xs:simpleType>
Example 10-3. Combining memberTypes and simpleType
<xs:simpleType name="SizeType">
  <xs:union memberTypes="DressSizeType">
    <xs:simpleType>
      <xs:restriction base="xs:token">
        <xs:enumeration value="small"/>
        <xs:enumeration value="medium"/>
        <xs:enumeration value="large"/>
      </xs:restriction>
    </xs:simpleType>
  </xs:union>
</xs:simpleType>
Example 10-4. Restricting a union
<xs:simpleType name="SmallSizeType">
  <xs:restriction base="SizeType">
    <xs:enumeration value="2"/>
    <xs:enumeration value="4"/>
    <xs:enumeration value="6"/>
    <xs:enumeration value="small"/>
  </xs:restriction>
</xs:simpleType>
Example 10-5. A union of a union
<xs:simpleType name="InternationalSizeType">
  <xs:union memberTypes="SizeType">
    <xs:simpleType>
      <xs:restriction base="xs:integer">
        <xs:minInclusive value="24"/>
        <xs:maxInclusive value="54"/>
      </xs:restriction>
    </xs:simpleType>
  </xs:union>
</xs:simpleType>
Example 10-6. Specifying the member type in the instance
<size xsi:type="DressSizeType">12</size>
Example 10-7. Defining a list type using an itemType attribute
<xs:simpleType name="AvailableSizesType">
  <xs:list itemType="DressSizeType"/>
</xs:simpleType>
Example 10-8. List instance
<availableSizes>10 12  14</availableSizes>
Example 10-9. Defining a list type using a simpleType child
<xs:simpleType name="AvailableSizesType">
  <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 10-10. Length facet applied to a list
<xs:simpleType name="AvailableSizesType">
  <xs:restriction>
    <xs:simpleType>
      <xs:list itemType="SMLSizeType"/>
    </xs:simpleType>
    <xs:maxLength value="3"/>
  </xs:restriction>
</xs:simpleType>
Example 10-11. Valid instance of a length-restricted list
<availableSizes>medium large</availableSizes>
Example 10-12. Enumeration applied inappropriately to a list type
<xs:simpleType name="AvailableSizesType">
  <xs:restriction>
    <xs:simpleType>
      <xs:list itemType="xs:token"/>
    </xs:simpleType>
    <xs:enumeration value="small"/>
    <xs:enumeration value="medium"/>
    <xs:enumeration value="large"/>
  </xs:restriction>
</xs:simpleType>
Example 10-13. Enumeration applied to the item type of a list
<xs:simpleType name="AvailableSizesType">
  <xs:list>
    <xs:simpleType>
      <xs:restriction base="xs:token">
        <xs:enumeration value="small"/>
        <xs:enumeration value="medium"/>
        <xs:enumeration value="large"/>
      </xs:restriction>
    </xs:simpleType>
  </xs:list>
</xs:simpleType>
Example 10-14. Enumeration correctly applied to a list type
Schema:
<xs:simpleType name="ApplicableSizesType">
  <xs:restriction>
    <xs:simpleType>
      <xs:list itemType="SizeType"/>
    </xs:simpleType>
    <xs:enumeration value="small medium large"/>
    <xs:enumeration value="2 4 6 8 10 12 14 16 18"/>
  </xs:restriction>
</xs:simpleType>
Instance:
<applicableSizes>small medium large</applicableSizes>
<applicableSizes>2 4 6 8 10 12 14 16 18</applicableSizes>
Example 10-15. Pattern applied to a list type
<xs:simpleType name="VectorType">
  <xs:restriction>
    <xs:simpleType>
      <xs:list itemType="xs:unsignedInt"/>
    </xs:simpleType>
    <xs:pattern value="\d+\s+\d+\s+((\d+\s+){3})*\d+"/>
  </xs:restriction>
</xs:simpleType>
Example 10-16. Defining a list of a string-based type
<xs:simpleType name="AvailableSizesType">
  <xs:list itemType="SMLXSizeType"/>
</xs:simpleType>
<xs:simpleType name="SMLXSizeType">
  <xs:restriction base="xs:token">
    <xs:enumeration value="small"/>
    <xs:enumeration value="medium"/>
    <xs:enumeration value="large"/>
    <xs:enumeration value="extra large"/>
  </xs:restriction>
</xs:simpleType>
Example 10-17. Invalid instance of AvailableSizesType
<availableSizes>
small
extra large
</availableSizes>
Example 10-18. Defining a list of a union
<xs:simpleType name="SizeType">
  <xs:union memberTypes="DressSizeType SMLXSizeType"/>
</xs:simpleType>
<xs:simpleType name="AvailableSizesType">
  <xs:list itemType="SizeType"/>
</xs:simpleType>
Example 10-19. Instance of a list of a union
<availableSizes>10 large 2</availableSizes>
Example 10-20. Illegal list of lists
<xs:simpleType name="RowType">
  <xs:list itemType="xs:integer"/>
</xs:simpleType>
<xs:simpleType name="TwoDimensionalArrayType">
  <xs:list itemType="RowType"/>
</xs:simpleType>
Example 10-21. An array using markup
Schema:
<xs:complexType name="VectorType">
  <xs:sequence maxOccurs="unbounded">
    <xs:element name="e" type="xs:integer"/>
  </xs:sequence>
</xs:complexType>
<xs:complexType name="ArrayType">
  <xs:sequence maxOccurs="unbounded">
    <xs:element name="r" type="VectorType"/>
  </xs:sequence>
</xs:complexType>
<xs:element name="array" type="ArrayType"/>
Instance:
<array>
  <r>  <e>1</e>  <e>12</e> <e>15</e> </r>
  <r>  <e>44</e> <e>2</e>  <e>3</e>  </r>
</array>
Datypic XML Schema Services