Datypic Logo
Definitive XML Schema

Definitive XML Schema

Priscilla Walmsley (pwalmsley@datypic.com)

2nd edition (September 2012)

Prentice Hall PTR; ISBN: 0132886723

Chapter 2: A quick tour of XML Schema

Book examples

Example 2-1. Product instance

<product effDate="2001-04-12">
  <number>557</number>
  <size>10</size>
</product>

Example 2-2. Product schema

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="product" type="ProductType"/>
  <xs:complexType name="ProductType">
    <xs:sequence>
      <xs:element name="number" type="xs:integer"/>
      <xs:element name="size" type="SizeType"/>
    </xs:sequence>
    <xs:attribute name="effDate" type="xs:date"/>
  </xs:complexType>
  <xs:simpleType name="SizeType">
    <xs:restriction base="xs:integer">
      <xs:minInclusive value="2"/>
      <xs:maxInclusive value="18"/>
    </xs:restriction>
  </xs:simpleType>
</xs:schema>

Example 2-3. Elements with simple types

<size>10</size>
<comment>Runs large.</comment>
<availableSizes>10 large 2</availableSizes>

Example 2-4. Elements with complex types

<size system="US-DRESS">10</size>
<comment>Runs <b>large</b>.</comment>
<availableSizes><size>10</size><size>2</size></availableSizes>

Example 2-5. Attributes with simple types

<system="US-DRESS">availableSizes="10 large 2"

Example 2-6. Anonymous type

<xs:element name="size">
  <xs:simpleType>
    <xs:restriction base="xs:integer">
      <xs:minInclusive value="2"/>
      <xs:maxInclusive value="18"/>
    </xs:restriction>
  </xs:simpleType>
</xs:element>

Example 2-7. Elements with complex types

<size system="US-DRESS">10</size>
<product>
  <number>557</number>
  <size>10</size>
</product>
<letter>Dear <custName>Priscilla Walmsley</custName>...</letter>
<color value="blue"/>

Example 2-8. More complicated content model

<xs:complexType name="ProductType">
  <xs:sequence>
    <xs:element name="number" type="xs:integer"/>
    <xs:choice minOccurs="0" maxOccurs="3">
      <xs:element name="size" type="SizeType"/>
      <xs:element name="color" type="ColorType"/>
    </xs:choice>
    <xs:any namespace="##other"/>
  </xs:sequence>
  <xs:attribute name="effDate" type="xs:date"/>
</xs:complexType>

Example 2-9. Complex type extension

<xs:complexType name="ShirtType">
  <xs:complexContent>
    <xs:extension base="ProductType">
      <xs:sequence>
        <xs:element name="color" type="ColorType"/>
      </xs:sequence>
      <xs:attribute name="id" type="xs:ID" use="required"/>
    </xs:extension>
  </xs:complexContent>
</xs:complexType>

Example 2-10. Product schema document with target namespace

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           targetNamespace="http://datypic.com/prod"
           xmlns:prod="http://datypic.com/prod">
  <xs:element name="product" type="prod:ProductType"/>
  <xs:complexType name="ProductType">
    <xs:sequence>
      <xs:element name="number" type="xs:integer"/>
      <xs:element name="size" type="prod:SizeType"/>
    </xs:sequence>
    <xs:attribute name="effDate" type="xs:date"/>
  </xs:complexType>
  <xs:simpleType name="SizeType">
    <xs:restriction base="xs:integer">
      <xs:minInclusive value="2"/>
      <xs:maxInclusive value="18"/>
    </xs:restriction>
  </xs:simpleType>
</xs:schema>

Example 2-11. Instance with namespace

<prod:product xmlns:prod="http://datypic.com/prod"
              effDate="2001-04-12">
  <number>557</number>
  <size>10</size>
</prod:product>

Example 2-12. Schema composition using include and import

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           xmlns="http://datypic.com/ord"
           targetNamespace="http://datypic.com/ord">
  <xs:include schemaLocation="moreOrderInfo.xsd"/>
  <xs:import namespace="http://datypic.com/prod"
              schemaLocation="productInfo.xsd"/>
  <!--...-->
</xs:schema>

Example 2-13. Using xsi:schemaLocation

<prod:product xmlns:prod="http://datypic.com/prod"
              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xsi:schemaLocation="http://datypic.com/prod prod.xsd"
              effDate="2001-04-12">
  <number>557</number>
  <size>10</size>
</prod:product>

Example 2-14. Annotation

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           xmlns:doc="http://datypic.com/doc">
  <xs:element name="product" type="ProductType">
    <xs:annotation>
      <xs:documentation xml:lang="en"
        source="http://datypic.com/prod.html#product">
        <doc:description>This element represents a product.
        </doc:description>
      </xs:documentation>
    </xs:annotation>
  </xs:element>
</xs:schema>