Definitive XML Schema

Definitive XML Schema

(pwalmsley@datypic.com)

ISBN: 0132886723

2nd edition, , Prentice Hall PTR.

Chapter 6: Element declarations

Full example

This example illustrates various element declarations. It contains global and local element declarations, named and anonymous types, and fixed and default values (which will be applied in this case.)

Instance (chapter06.xml)
<product xmlns="http://example.org/prod"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://example.org/prod chapter07.xsd">
  <name/>
  <size/>
</product>
Schema (chapter06.xsd)
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
            xmlns="http://example.org/prod"
            targetNamespace="http://example.org/prod"
            elementFormDefault="qualified">
  <xs:element name="product" type="ProductType"/>
  <xs:element name="size" type="xs:integer" fixed="10"/>
  <xs:complexType name="ProductType">
    <xs:sequence>
      <xs:element name="name" default="N/A">
        <xs:simpleType>
          <xs:restriction base="xs:string">
            <xs:minLength value="1"/>
          </xs:restriction>
        </xs:simpleType>
      </xs:element>
      <xs:element ref="size" minOccurs="0"/>
    </xs:sequence>
  </xs:complexType>
</xs:schema>
Datypic XML Schema Services

Book examples

Example 6-1. Global element declarations
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           xmlns="http://datypic.com/prod"
           targetNamespace="http://datypic.com/prod">
  <xs:element name="name" type="xs:string"/>
  <xs:element name="size" type="xs:integer"/>
  <xs:complexType name="ProductType">
    <xs:sequence>
      <xs:element ref="name"/>
      <xs:element ref="size" minOccurs="0"/>
    </xs:sequence>
  </xs:complexType>
</xs:schema>
Example 6-2. Illegal attempt to prefix an element name
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           xmlns:prod="http://datypic.com/prod"
           targetNamespace="http://datypic.com/prod">
  <xs:element name="name" type="xs:string"/>
  <xs:element name="prod:size" type="xs:integer"/>
</xs:schema>
Example 6-3. Local element declarations
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           xmlns="http://datypic.com/prod"
           targetNamespace="http://datypic.com/prod">
  <xs:complexType name="ProductType">
    <xs:sequence>
      <xs:element name="name" type="xs:string"/>
      <xs:element name="size" type="xs:integer" minOccurs="0"/>
    </xs:sequence>
  </xs:complexType>
</xs:schema>
Example 6-4. Assigning types to elements
<xs:element name="size" type="SizeType"/>
<xs:element name="name" type="xs:string"/>
<xs:element name="product">
  <xs:complexType>
    <xs:sequence>
      <xs:element ref="name"/>
      <xs:element ref="size"/>
    </xs:sequence>
  </xs:complexType>
</xs:element>
<xs:element name="anything"/>
Example 6-5. Qualified local names
<prod:product xmlns:prod="http://datypic.com/prod">
  <prod:number>557</prod:number>
  <prod:size>10</prod:size>
</prod:product>
Example 6-6. Unqualified local names
<prod:product xmlns:prod="http://datypic.com/prod">
  <number>557</number>
  <size>10</size>
</prod:product>
Example 6-7. Schema for qualified local element names
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           xmlns="http://datypic.com/prod"
           targetNamespace="http://datypic.com/prod"
           elementFormDefault="qualified">
  <xs:element name="product" type="ProductType"/>
  <xs:complexType name="ProductType">
    <xs:sequence>
      <xs:element name="number" type="xs:integer"/>
      <xs:element name="size" type="xs:integer"/>
    </xs:sequence>
  </xs:complexType>
</xs:schema>
Example 6-8. Using the form attribute
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           xmlns="http://datypic.com/prod"
           targetNamespace="http://datypic.com/prod"
           elementFormDefault="qualified">
  <xs:element name="product" type="ProductType"/>
  <xs:complexType name="ProductType">
    <xs:sequence>
      <xs:element name="number" type="xs:integer"
                        form="unqualified"/>
      <xs:element name="size" type="xs:integer"/>
    </xs:sequence>
  </xs:complexType>
</xs:schema>
Example 6-9. Overridden form
<prod:product xmlns:prod="http://datypic.com/prod">
  <number>557</number>
  <prod:size>10</prod:size>
</prod:product>
Example 6-10. Invalid mixing of unqualified names and a default namespace
<product xmlns="http://datypic.com/prod">
  <number>557</number>
  <size>10</size>
</product>
Example 6-11. Specifying an element's default value
<xs:element name="product">
  <xs:complexType>
    <xs:choice minOccurs="0" maxOccurs="unbounded">
      <xs:element name="name" type="xs:string" default="N/A"/>
      <xs:element name="size" type="xs:integer" default="12"/>
    </xs:choice>
  </xs:complexType>
</xs:element>
Example 6-12. Missing values
<order>
  <giftWrap>ADULT BDAY</giftWrap>
  <customer>
    <name>
      <first>Priscilla</first>
      <middle/>
      <last>Walmsley</last>
    </name>
  </customer>
  <items>
    <shirt>
      <giftWrap/>
      <number>557</number>
      <name>Short-Sleeved Linen Blouse</name>
      <size></size>
    </shirt>
    <umbrella>
      <number>443</number>
      <name>Deluxe Golf Umbrella</name>
      <size></size>
    </umbrella>
  </items>
</order>
Example 6-13. Missing values, revisited
<order xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <giftWrap>ADULT BDAY</giftWrap>
  <customer>
    <name>
      <title/>              <!--default will be filled in-->
      <first>Priscilla</first>
      <middle xsi:nil="true"/>
      <last>Walmsley</last>
    </name>
  </customer>
  <items>
    <shirt>
      <giftWrap/>
      <number>557</number>
      <name>Short-Sleeved Linen Blouse</name>
      <size></size>                  <!--INVALID! -->
    </shirt>
    <umbrella>
      <number>443</number>
      <name>Deluxe Golf Umbrella</name>
    </umbrella>
  </items>
</order>
Example 6-14. xsi:nil in instance elements
<size xsi:nil="true"/>
<size xsi:nil="true"></size>
<size xsi:nil="true" system="US-DRESS"/>
<size xsi:nil="false">10</size>
<size xsi:nil="true">10</size> <!--INVALID! -->
Example 6-15. Making size elements nillable
<xs:element name="size" type="xs:integer" nillable="true"/>
Datypic XML Schema Services