Definitive XML Schema

Definitive XML Schema

(pwalmsley@datypic.com)

ISBN: 0130655678

1st edition, , Prentice Hall PTR.

Chapter 7: 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 (chapter07.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 (chapter07.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 7-1. Global element declarations
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            xmlns="http://example.org/prod"
            targetNamespace="http://example.org/prod">
  <xsd:element name="name" type="xsd:string"/>
  <xsd:element name="size" type="xsd:integer"/>
  <xsd:complexType name="ProductType">
    <xsd:sequence>
      <xsd:element ref="name"/>
      <xsd:element ref="size" minOccurs="0"/>
    </xsd:sequence>
  </xsd:complexType>
</xsd:schema>
Example 7-2. Illegal attempt to prefix an element-type name
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            xmlns:prod="http://example.org/prod"
            targetNamespace="http://example.org/prod">
  <xsd:element name="name" type="xsd:string"/>
  <xsd:element name="prod:size" type="xsd:integer"/>
</xsd:schema>
Example 7-3. Local element declarations
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            xmlns="http://example.org/prod"
            targetNamespace="http://example.org/prod">
  <xsd:complexType name="ProductType">
    <xsd:sequence>
      <xsd:element name="name" type="xsd:string"/>
      <xsd:element name="size" type="xsd:integer" minOccurs="0"/>
    </xsd:sequence>
  </xsd:complexType>
</xsd:schema>
Example 7-4. Declaring the data types of elements
<xsd:element name="size" type="SizeType"/>
<xsd:element name="name" type="xsd:string"/>
<xsd:element name="product">
  <xsd:complexType>
    <xsd:sequence>
      <xsd:element ref="name"/>
      <xsd:element ref="size"/>
    </xsd:sequence>
  </xsd:complexType>
</xsd:element>
<xsd:element name="anything"/>
Example 7-5. Specifying an element default value
<xsd:element name="product">
  <xsd:complexType>
    <xsd:choice minOccurs="0" maxOccurs="unbounded">
      <xsd:element name="name" type="xsd:string" default="N/A"/>
      <xsd:element name="size" type="xsd:integer" default="12"/>
    </xsd:choice>
  </xsd:complexType>
</xsd:element>
Example 7-6. A default value with a string type
<xsd:element name="name" default="N/A">
  <xsd:simpleType>
    <xsd:restriction base="xsd:token">
      <xsd:minLength value="1"/>
    </xsd:restriction>
  </xsd:simpleType>
</xsd:element>
Example 7-7. Missing values
<order>
  <giftWrap>ADULT BDAY</giftWrap>
  <customer>
    <name>
      <first>Priscilla</first>
      <middle/>
      <last>Walmsley</last>
    </name>
  </customer>
  <items>
    <shirt xsi:type="ProductType">
      <giftWrap/>
      <number>557</number>
      <name>Short-Sleeved Linen Blouse</name>
      <size></size>
    </shirt>
    <umbrella xsi:type="ProductType">
      <number>443</number>
      <name>Deluxe Golf Umbrella</name>
      <size></size>
    </umbrella>
  </items>
</order>
Example 7-8. 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 xsi:type="ShirtType">
      <giftWrap/>
      <number>557</number>
      <name>Short-Sleeved Linen Blouse</name>
      <size></size>                  <!--INVALID! -->
    </shirt>
    <umbrella xsi:type="UmbrellaType">
      <number>443</number>
      <name>Deluxe Golf Umbrella</name>
    </umbrella>
  </items>
</order>
Example 7-9. 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 7-10. Making size elements nillable
<xsd:element name="size" type="xsd:integer" nillable="true"/>
Datypic XML Schema Services