Definitive XML Schema
Priscilla Walmsley (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.)
<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"/>
<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
<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>
<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>
<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"/>
<xsd:element name="size"> <xsd:complexType> <xsd:attribute name="dim" type="xsd:integer" default="1"/> </xsd:complexType> </xsd:element>