Definitive XML Schema

Definitive XML Schema

(pwalmsley@datypic.com)

ISBN: 0130655678

1st edition, , Prentice Hall PTR.

Chapter 11: Union and list types

Full example

This example illustrates union and list types.

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