Definitive XML Schema

Definitive XML Schema

(pwalmsley@datypic.com)

ISBN: 0132886723

2nd edition, , Prentice Hall PTR.

Chapter 7: Attribute declarations

Full example

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

Instance (chapter07.xml)
<size xmlns="http://example.org/prod"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://example.org/prod chapter08.xsd"
      system="US-DRESS"/>
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="size" type="SizeType"/>
  <xs:complexType name="SizeType">
    <xs:attribute name="system" type="xs:string" use="required"/>
    <xs:attribute ref="dim"/>
    <xs:attribute name="value" default="10">
      <xs:simpleType>
        <xs:restriction base="xs:integer">
          <xs:minInclusive value="2"/>
          <xs:maxInclusive value="20"/>
        </xs:restriction>
      </xs:simpleType>
    </xs:attribute>
  </xs:complexType>
  <xs:attribute name="dim" type="xs:integer" fixed="1"/>
</xs:schema>
Datypic XML Schema Services

Book examples

Example 7-1. Global attribute declarations
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           xmlns="http://datypic.com/prod"
           targetNamespace="http://datypic.com/prod">
  <xs:attribute name="system" type="xs:string"/>
  <xs:attribute name="dim" type="xs:integer"/>
  <xs:complexType name="SizeType">
    <xs:attribute ref="system" use="required"/>
    <xs:attribute ref="dim"/>
  </xs:complexType>
</xs:schema>
Example 7-2. Local attribute declarations
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           xmlns="http://datypic.com/prod"
           targetNamespace="http://datypic.com/prod">
  <xs:complexType name="SizeType">
    <xs:attribute name="system" type="xs:string" use="required"/>
    <xs:attribute name="dim" type="xs:integer"/>
  </xs:complexType>
</xs:schema>
Example 7-3. Declaring the types of attributes
<xs:attribute name="color" type="ColorType"/>
<xs:attribute name="dim" type="xs:integer"/>
<xs:attribute name="system">
  <xs:simpleType>
    <xs:restriction base="xs:string">
      <xs:enumeration value="US-DRESS"/>
      <!--...-->
    </xs:restriction>
  </xs:simpleType>
</xs:attribute>
<xs:attribute name="anything"/>
Example 7-4. Qualified and unqualified attribute names
Schema:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           xmlns="http://datypic.com/prod"
           targetNamespace="http://datypic.com/prod">
  <xs:attribute name="global" type="xs:string"/>
  <xs:element name="size" type="SizeType"/>
  <xs:complexType name="SizeType">
    <xs:attribute ref="global"/>
    <xs:attribute name="unqual" form="unqualified"/>
    <xs:attribute name="qual" form="qualified"/>
    <xs:attribute name="unspec"/>
  </xs:complexType>
</xs:schema>
Valid instance:
<prod:size xmlns:prod="http://datypic.com/prod"
           prod:global="x" unqual="x" prod:qual="x" unspec="x"/>
Example 7-5. Declaring a default value for an attribute
<xs:element name="size">
  <xs:complexType>
    <xs:attribute name="dim" type="xs:integer" default="1"/>
  </xs:complexType>
</xs:element>
Example 7-6. Instance containing an inherited attribute
<chapter language="en">
  <p>This is not a pipe.</p>
  <p language="fr">Ceci n'est pas une pipe.</p>
</chapter>
Example 7-7. Declaring an inheritable attribute
<xs:element name="chapter" type="ChapterType"/>
<xs:complexType name="ChapterType">
  <xs:sequence>
    <xs:element name="p" type="ParaType" maxOccurs="unbounded"/>
  </xs:sequence>
  <xs:attribute name="language" type="xs:language"
                inheritable="true"/>
</xs:complexType>
<xs:complexType name="ParaType">
  <xs:simpleContent>
    <xs:extension base="xs:string">
      <xs:attribute name="language" type="xs:language"/>
    </xs:extension>
  </xs:simpleContent>
</xs:complexType>
Datypic XML Schema Services