Definitive XML Schema
Priscilla Walmsley (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.)
<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
<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>
<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>
<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"/>
<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>
<prod:size xmlns:prod="http://datypic.com/prod" prod:global="x" unqual="x" prod:qual="x" unspec="x"/>
<xs:element name="size"> <xs:complexType> <xs:attribute name="dim" type="xs:integer" default="1"/> </xs:complexType> </xs:element>
<chapter language="en"> <p>This is not a pipe.</p> <p language="fr">Ceci n'est pas une pipe.</p> </chapter>
<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>