D A T Y P I C
home
services
books
Overview
Table of Contents
Sample Chapter
Examples
1. Intro
2. Quick tour
3. Namespaces
4. Include/Import
5. Instances
6. Annotations
7. Element Decls
8. Attribute Decls
9. Simple types
10. Regexes
11. Union/List types
12. Built-in types
13. Complex types
14. Type derivation
15. Groups
16. Subst. groups
17. ID constraints
18. Redefines
19. DTDs topics
20. Naming
21. Extensibility
Errata
about
Definitive XML Schema

Definitive XML Schema

Priscilla Walmsley (pwalmsley@datypic.com)
1st edition (December 7, 2001)
Prentice Hall PTR; ISBN: 0130655678
Amazon.com
Buy at 30% off list price


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>


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>