Definitive XML Schema

Definitive XML Schema

(pwalmsley@datypic.com)

ISBN: 0130655678

1st edition, , Prentice Hall PTR.

Chapter 8: 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 (chapter08.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 (chapter08.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 8-1. Global attribute declarations
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            xmlns="http://example.org/prod"
            targetNamespace="http://example.org/prod">
  <xsd:attribute name="system" type="xsd:string"/>
  <xsd:attribute name="dim" type="xsd:integer"/>
  <xsd:complexType name="SizeType">
    <xsd:attribute ref="system" use="required"/>
    <xsd:attribute ref="dim"/>
  </xsd:complexType>
</xsd:schema>
Example 8-2. Local attribute declarations
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            xmlns="http://example.org/prod"
            targetNamespace="http://example.org/prod">
  <xsd:complexType name="SizeType">
    <xsd:attribute name="system" type="xsd:string" use="required"/>
    <xsd:attribute name="dim" type="xsd:integer"/>
  </xsd:complexType>
</xsd:schema>
Example 8-3. Assigning types to attributes
<xsd:attribute name="color" type="ColorType"/>
<xsd:attribute name="dim" type="xsd:integer"/>
<xsd:attribute name="system">
  <xsd:simpleType>
    <xsd:restriction base="xsd:string">
      <xsd:enumeration value="US-DRESS"/>
      <!--...-->
    </xsd:restriction>
  </xsd:simpleType>
</xsd:attribute>
<xsd:attribute name="anything"/>
Example 8-4. Declaring a default value for an attribute
<xsd:element name="size">
  <xsd:complexType>
    <xsd:attribute name="dim" type="xsd:integer" default="1"/>
  </xsd:complexType>
</xsd:element>
Datypic XML Schema Services