Definitive XML Schema
Priscilla Walmsley (pwalmsley@datypic.com)
ISBN: 0130655678
1st edition, , Prentice Hall PTR.
Chapter 7: 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
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://example.org/prod" targetNamespace="http://example.org/prod"> <xsd:element name="name" type="xsd:string"/> <xsd:element name="size" type="xsd:integer"/> <xsd:complexType name="ProductType"> <xsd:sequence> <xsd:element ref="name"/> <xsd:element ref="size" minOccurs="0"/> </xsd:sequence> </xsd:complexType> </xsd:schema>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:prod="http://example.org/prod" targetNamespace="http://example.org/prod"> <xsd:element name="name" type="xsd:string"/> <xsd:element name="prod:size" type="xsd:integer"/> </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="ProductType"> <xsd:sequence> <xsd:element name="name" type="xsd:string"/> <xsd:element name="size" type="xsd:integer" minOccurs="0"/> </xsd:sequence> </xsd:complexType> </xsd:schema>
<xsd:element name="size" type="SizeType"/> <xsd:element name="name" type="xsd:string"/> <xsd:element name="product"> <xsd:complexType> <xsd:sequence> <xsd:element ref="name"/> <xsd:element ref="size"/> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:element name="anything"/>
<xsd:element name="product"> <xsd:complexType> <xsd:choice minOccurs="0" maxOccurs="unbounded"> <xsd:element name="name" type="xsd:string" default="N/A"/> <xsd:element name="size" type="xsd:integer" default="12"/> </xsd:choice> </xsd:complexType> </xsd:element>
<xsd:element name="name" default="N/A"> <xsd:simpleType> <xsd:restriction base="xsd:token"> <xsd:minLength value="1"/> </xsd:restriction> </xsd:simpleType> </xsd:element>
<order> <giftWrap>ADULT BDAY</giftWrap> <customer> <name> <first>Priscilla</first> <middle/> <last>Walmsley</last> </name> </customer> <items> <shirt xsi:type="ProductType"> <giftWrap/> <number>557</number> <name>Short-Sleeved Linen Blouse</name> <size></size> </shirt> <umbrella xsi:type="ProductType"> <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 xsi:type="ShirtType"> <giftWrap/> <number>557</number> <name>Short-Sleeved Linen Blouse</name> <size></size> <!--INVALID! --> </shirt> <umbrella xsi:type="UmbrellaType"> <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! -->
<xsd:element name="size" type="xsd:integer" nillable="true"/>