Definitive XML Schema
Priscilla Walmsley (pwalmsley@datypic.com)
ISBN: 0132886723
2nd edition, , Prentice Hall PTR.
Chapter 6: Element declarations
Full example
This example illustrates various element declarations. It contains global and local element declarations, named and anonymous types, and fixed and default values (which will be applied in this case.)
<product xmlns="http://example.org/prod" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://example.org/prod chapter07.xsd"> <name/> <size/> </product>
<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="product" type="ProductType"/> <xs:element name="size" type="xs:integer" fixed="10"/> <xs:complexType name="ProductType"> <xs:sequence> <xs:element name="name" default="N/A"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:minLength value="1"/> </xs:restriction> </xs:simpleType> </xs:element> <xs:element ref="size" minOccurs="0"/> </xs:sequence> </xs:complexType> </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:element name="name" type="xs:string"/> <xs:element name="size" type="xs:integer"/> <xs:complexType name="ProductType"> <xs:sequence> <xs:element ref="name"/> <xs:element ref="size" minOccurs="0"/> </xs:sequence> </xs:complexType> </xs:schema>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:prod="http://datypic.com/prod" targetNamespace="http://datypic.com/prod"> <xs:element name="name" type="xs:string"/> <xs:element name="prod:size" type="xs:integer"/> </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="ProductType"> <xs:sequence> <xs:element name="name" type="xs:string"/> <xs:element name="size" type="xs:integer" minOccurs="0"/> </xs:sequence> </xs:complexType> </xs:schema>
<xs:element name="size" type="SizeType"/> <xs:element name="name" type="xs:string"/> <xs:element name="product"> <xs:complexType> <xs:sequence> <xs:element ref="name"/> <xs:element ref="size"/> </xs:sequence> </xs:complexType> </xs:element> <xs:element name="anything"/>
<prod:product xmlns:prod="http://datypic.com/prod"> <prod:number>557</prod:number> <prod:size>10</prod:size> </prod:product>
<prod:product xmlns:prod="http://datypic.com/prod"> <number>557</number> <size>10</size> </prod:product>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://datypic.com/prod" targetNamespace="http://datypic.com/prod" elementFormDefault="qualified"> <xs:element name="product" type="ProductType"/> <xs:complexType name="ProductType"> <xs:sequence> <xs:element name="number" type="xs:integer"/> <xs:element name="size" type="xs:integer"/> </xs:sequence> </xs:complexType> </xs:schema>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://datypic.com/prod" targetNamespace="http://datypic.com/prod" elementFormDefault="qualified"> <xs:element name="product" type="ProductType"/> <xs:complexType name="ProductType"> <xs:sequence> <xs:element name="number" type="xs:integer" form="unqualified"/> <xs:element name="size" type="xs:integer"/> </xs:sequence> </xs:complexType> </xs:schema>
<prod:product xmlns:prod="http://datypic.com/prod"> <number>557</number> <prod:size>10</prod:size> </prod:product>
<product xmlns="http://datypic.com/prod"> <number>557</number> <size>10</size> </product>
<xs:element name="product"> <xs:complexType> <xs:choice minOccurs="0" maxOccurs="unbounded"> <xs:element name="name" type="xs:string" default="N/A"/> <xs:element name="size" type="xs:integer" default="12"/> </xs:choice> </xs:complexType> </xs:element>
<order> <giftWrap>ADULT BDAY</giftWrap> <customer> <name> <first>Priscilla</first> <middle/> <last>Walmsley</last> </name> </customer> <items> <shirt> <giftWrap/> <number>557</number> <name>Short-Sleeved Linen Blouse</name> <size></size> </shirt> <umbrella> <number>443</number> <name>Deluxe Golf Umbrella</name> <size></size> </umbrella> </items> </order>
<order xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <giftWrap>ADULT BDAY</giftWrap> <customer> <name> <title/> <!--default will be filled in--> <first>Priscilla</first> <middle xsi:nil="true"/> <last>Walmsley</last> </name> </customer> <items> <shirt> <giftWrap/> <number>557</number> <name>Short-Sleeved Linen Blouse</name> <size></size> <!--INVALID! --> </shirt> <umbrella> <number>443</number> <name>Deluxe Golf Umbrella</name> </umbrella> </items> </order>
<size xsi:nil="true"/> <size xsi:nil="true"></size> <size xsi:nil="true" system="US-DRESS"/> <size xsi:nil="false">10</size> <size xsi:nil="true">10</size> <!--INVALID! -->
<xs:element name="size" type="xs:integer" nillable="true"/>