Definitive XML Schema
Priscilla Walmsley (pwalmsley@datypic.com)
ISBN: 0132886723
2nd edition, , Prentice Hall PTR.
Chapter 12: Complex types
Full example
This example illustrates complex types that are not derived from other specified types (they are by default derived from anyType
). It includes an example of each content type: element-only, simple, empty and mixed.
<items xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="chapter13.xsd" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:oth="http://example.org/oth"> <shirt effDate="2002-04-02"> <number>557</number> <name>Short-Sleeved Linen Blouse</name> <size system="US-DRESS">10</size> <color value="blue"/> <description> This shirt is the <xhtml:b>best-selling</xhtml:b> shirt in our catalog! <xhtml:br/> Note: runs large. </description> </shirt> <hat oth:custom="12"> <number>557</number> <name>Ten-Gallon Hat</name> <color value="red"/> </hat> </items>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="items" type="ItemsType"/> <xs:complexType name="ItemsType"> <xs:choice minOccurs="0" maxOccurs="unbounded"> <xs:element name="shirt" type="ProductType"/> <xs:element name="hat" type="ProductType"/> <xs:element name="umbrella" type="ProductType"/> </xs:choice> </xs:complexType> <!-- Element only content --> <xs:complexType name="ProductType"> <xs:sequence> <xs:element name="number" type="xs:integer"/> <xs:element name="name" type="xs:string"/> <xs:choice minOccurs="0" maxOccurs="unbounded"> <xs:element name="size" type="SizeType"/> <xs:element name="color" type="ColorType"/> <xs:element name="description" type="DescriptionType"/> </xs:choice> </xs:sequence> <xs:attribute name="effDate" type="xs:date" default="1900-01-01"/> <xs:anyAttribute namespace="##other" processContents="lax"/> </xs:complexType> <!-- Simple content --> <xs:complexType name="SizeType"> <xs:simpleContent> <xs:extension base="xs:integer"> <xs:attribute name="system" type="xs:token"/> </xs:extension> </xs:simpleContent> </xs:complexType> <!-- Empty content --> <xs:complexType name="ColorType"> <xs:attribute name="value" type="xs:string"/> </xs:complexType> <!-- Mixed content --> <xs:complexType name="DescriptionType" mixed="true"> <xs:sequence> <xs:any namespace="http://www.w3.org/1999/xhtml" minOccurs="0" maxOccurs="unbounded" processContents="skip"/> </xs:sequence> </xs:complexType> </xs:schema>
Book examples
<size system="US-DRESS">10</size> <product> <number>557</number> <name>Short-Sleeved Linen Blouse</name> </product> <letter>Dear <custName>Priscilla Walmsley</custName>...</letter> <color value="blue"/>
<xs:complexType name="ProductType"> <xs:sequence> <xs:element name="number" type="xs:integer"/> <xs:element name="name" type="xs:string"/> <xs:element name="size" type="SizeType"/> </xs:sequence> </xs:complexType> <xs:element name="product" type="ProductType"/>
<xs:element name="product"> <xs:complexType> <xs:sequence> <xs:element name="number" type="xs:integer"/> <xs:element name="name" type="xs:string"/> <xs:element name="size" type="SizeType"/> </xs:sequence> </xs:complexType> </xs:element>
<size system="US-DRESS">10</size>
<xs:complexType name="SizeType"> <xs:simpleContent> <xs:extension base="xs:integer"> <xs:attribute name="system" type="xs:token"/> </xs:extension> </xs:simpleContent> </xs:complexType>
<product> <number>557</number> <name>Short-Sleeved Linen Blouse</name> <size system="US-DRESS">10</size> <color value="blue"/> </product>
<xs:complexType name="ProductType"> <xs:sequence> <xs:element name="number" type="xs:integer"/> <xs:element name="name" type="xs:string"/> <xs:element name="size" type="SizeType"/> <xs:element name="color" type="ColorType"/> </xs:sequence> </xs:complexType>
<desc>This is our <i>best-selling</i> shirt. <b>Note: </b> runs <u>large</u>.</desc>
<xs:element name="desc" type="DescType"/> <xs:complexType name="DescType" mixed="true"> <xs:choice minOccurs="0" maxOccurs="unbounded"> <xs:element name="i" type="xs:string"/> <xs:element name="b" type="xs:string"/> <xs:element name="u" type="xs:string"/> <!--...--> </xs:choice> </xs:complexType>
<color value="blue"/>
<xs:complexType name="ColorType"> <xs:attribute name="value" type="ColorValueType"/> </xs:complexType>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="number" type="xs:integer"/> <xs:element name="name" type="xs:string"/> <xs:element name="size" type="SizeType"/> <xs:element name="color" type="ColorType"/> <xs:complexType name="ProductType"> <xs:sequence> <xs:element ref="number"/> <xs:element ref="name"/> <xs:element ref="size" minOccurs="0"/> <xs:element ref="color" minOccurs="0"/> </xs:sequence> </xs:complexType> <!--...--> </xs:schema>
<xs:complexType name="ProductType"> <xs:choice> <xs:sequence> <xs:element name="number" type="xs:integer"/> <xs:element name="name" type="xs:string" minOccurs="0"/> </xs:sequence> <xs:element name="name" type="xs:string"/> </xs:choice> </xs:complexType>
<xs:complexType name="ProductType"> <xs:choice> <xs:sequence> <xs:element name="number" type="xs:integer"/> <xs:element name="name" type="xs:string" minOccurs="0"/> </xs:sequence> <xs:element name="name" type="xs:token"/> </xs:choice> </xs:complexType>
<xs:complexType name="ProductType"> <xs:sequence> <xs:element name="number" type="xs:integer"/> <xs:element name="name" type="xs:string"/> <xs:element name="size" type="SizeType" minOccurs="0"/> <xs:element name="color" type="ColorType" minOccurs="0"/> </xs:sequence> </xs:complexType>
<product> <number>557</number> <name>Short-Sleeved Linen Blouse</name> <size system="US-DRESS">10</size> <color value="blue"/> </product> <product> <number>557</number> <name>Short-Sleeved Linen Blouse</name> </product> <product> <number>557</number> <name>Short-Sleeved Linen Blouse</name> <color value="blue"/> </product>
<product> <number>557</number> <size system="US-DRESS">10</size> <name>Short-Sleeved Linen Blouse</name> <color value="blue"/> </product>
<xs:complexType name="ProductType"> <xs:sequence> <xs:element name="number" type="xs:integer"/> <xs:element name="name" type="xs:string"/> <xs:element name="color" type="ColorType" maxOccurs="3"/> </xs:sequence> </xs:complexType>
<xs:complexType name="ItemsType"> <xs:choice> <xs:element name="shirt" type="ShirtType"/> <xs:element name="hat" type="HatType"/> <xs:element name="umbrella" type="UmbrellaType"/> </xs:choice> </xs:complexType>
<items> <shirt>...</shirt> </items> <items> <hat>...</hat> </items>
<xs:complexType name="ItemsType"> <xs:choice minOccurs="0" maxOccurs="unbounded"> <xs:element name="shirt" type="ShirtType"/> <xs:element name="umbrella" type="UmbrellaType"/> <xs:element name="hat" type="HatType"/> </xs:choice> </xs:complexType>
<items> <shirt>...</shirt> <hat>...</hat> <umbrella>...</umbrella> <shirt>...</shirt> <shirt>...</shirt> </items>
<xs:complexType name="ProductType"> <xs:sequence> <xs:element name="number" type="xs:integer"/> <xs:element name="name" type="xs:string"/> <xs:choice minOccurs="0" maxOccurs="unbounded"> <xs:element name="size" type="SizeType"/> <xs:element name="color" type="ColorType"/> </xs:choice> </xs:sequence> </xs:complexType>
<xs:complexType name="ProductType"> <xs:all> <xs:element name="number" type="xs:integer"/> <xs:element name="name" type="xs:string"/> <xs:element name="size" type="SizeType" minOccurs="0"/> <xs:element name="color" type="ColorType" minOccurs="0"/> </xs:all> </xs:complexType>
<product> <color value="blue"/> <size system="US-DRESS">10</size> <number>557</number> <name>Short-Sleeved Linen Blouse</name> </product> <product> <name>Short-Sleeved Linen Blouse</name> <number>557</number> </product>
<xs:complexType name="ProductType"> <xs:sequence> <xs:element name="number" type="xs:integer"/> <xs:element name="name" type="xs:string"/> <xs:all> <xs:element name="size" type="SizeType" minOccurs="0"/> <xs:element name="color" type="ColorType" minOccurs="0"/> </xs:all> </xs:sequence> </xs:complexType>
<xs:complexType name="ProductType"> <xs:sequence> <xs:group ref="DescriptionGroup"/> <xs:element name="number" type="xs:integer"/> <xs:element name="name" type="xs:string"/> </xs:sequence> </xs:complexType>
<xs:complexType name="AOrBOrBothType"> <xs:choice> <xs:element name="a" type="xs:string"/> <xs:element name="b" type="xs:string"/> <xs:sequence> <xs:element name="a" type="xs:string"/> <xs:element name="b" type="xs:string"/> </xs:sequence> </xs:choice> </xs:complexType>
<xs:complexType name="AOrBOrBothType"> <xs:choice> <xs:sequence> <xs:element name="a" type="xs:string"/> <xs:element name="b" type="xs:string" minOccurs="0"/> </xs:sequence> <xs:element name="b" type="xs:string"/> </xs:choice> </xs:complexType>
<xs:complexType name="ProductType"> <xs:sequence> <!--...--> </xs:sequence> <xs:attribute name="effDate" type="xs:date" default="1900-01-01"/> </xs:complexType>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:attribute name="effDate" type="xs:date" default="1900-01-01"/> <xs:complexType name="ProductType"> <xs:sequence> <!--...--> </xs:sequence> <xs:attribute ref="effDate" default="2000-12-31"/> </xs:complexType> </xs:schema>
<xs:complexType name="ProductType"> <xs:sequence> <!--...--> </xs:sequence> <xs:attributeGroup ref="IdentifierGroup"/> <xs:attribute name="effDate" type="xs:date"/> </xs:complexType>
<xs:element name="product" type="ProductType"/> <xs:complexType name="ProductType"> <xs:sequence> <xs:element name="number" type="xs:integer"/> <xs:element name="name" type="xs:string"/> <xs:any minOccurs="0" maxOccurs="unbounded"/> </xs:sequence> </xs:complexType> <xs:element name="color" type="xs:string"/> <xs:element name="desc" type="xs:string"/> <xs:element name="size" type="xs:integer"/>
<product> <number>557</number> <name>Short-Sleeved Linen Blouse</name> <color>blue</color> <size>12</size> <color>red</color> </product>
<xs:complexType name="DescriptionType" mixed="true"> <xs:sequence> <xs:any namespace="http://www.w3.org/1999/xhtml" minOccurs="0" maxOccurs="unbounded" processContents="skip"/> </xs:sequence> </xs:complexType>
<catalog xmlns:xhtml="http://www.w3.org/1999/xhtml"> <description> This shirt is the <xhtml:b>best-selling</xhtml:b> shirt in our catalog! <xhtml:br/> Note: runs large. </description> <!--...--> </catalog>
<xs:element name="product" type="ProductType"/> <xs:complexType name="ProductType"> <xs:sequence> <xs:element name="number" type="xs:integer"/> <xs:element name="name" type="xs:string"/> <xs:any minOccurs="0" maxOccurs="unbounded" notNamespace="http://www.w3.org/1999/xhtml" notQName="##definedSibling desc size" processContents="lax"/> </xs:sequence> </xs:complexType> <xs:element name="color" type="xs:string"/> <xs:element name="size" type="xs:integer"/>
<product> <number>557</number> <name>Short-Sleeved Linen Blouse</name> <color>blue</color> <random>not declared in the schema</random> <color>red</color> </product>
<product xmlns:xhtml="http://www.w3.org/1999/xhtml"> <number>557</number> <name>Short-Sleeved Linen Blouse</name> <size>12</size> <desc>Our best-selling shirt</desc> <number>12345</number> <xhtml:p>Our best-selling shirt</xhtml:p> </product>
<xs:element name="product" type="OpenProductType"/> <xs:complexType name="OpenProductType"> <xs:openContent> <xs:any namespace="##other" processContents="lax"/> </xs:openContent> <xs:sequence> <xs:element name="number" type="xs:integer"/> <xs:element name="name" type="xs:string"/> </xs:sequence> </xs:complexType>
<product xmlns:ext="http://datypic.com/extension"> <ext:something>an extension element</ext:something> <number>557</number> <ext:something_else>another extension element</ext:something_else> <name>Short-Sleeved Linen Blouse</name> <ext:something_else>another extension element</ext:something_else> <ext:something>an extension element</ext:something> </product>
<xs:element name="product" type="OpenProductType"/> <xs:complexType name="OpenProductType"> <xs:openContent mode="suffix"> <xs:any namespace="##local" processContents="strict"/> </xs:openContent> <xs:sequence> <xs:element name="number" type="xs:integer"/> <xs:element name="name" type="xs:string"/> </xs:sequence> </xs:complexType> <xs:element name="something" type="xs:string"/>
<product> <number>557</number> <name>Short-Sleeved Linen Blouse</name> <something>an extension element</something> <something>an extension element</something> </product>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:defaultOpenContent mode="suffix"> <xs:any namespace="##local"/> </xs:defaultOpenContent> <xs:element name="catalog" type="CatalogType"/> <xs:complexType name="CatalogType"> <xs:sequence> <xs:element name="product" type="ProductType" maxOccurs="unbounded"/> </xs:sequence> </xs:complexType> <xs:complexType name="ProductType"> <xs:sequence> <xs:element name="number" type="xs:integer"/> <xs:element name="name" type="xs:string"/> </xs:sequence> </xs:complexType> <xs:element name="something" type="xs:string"/> </xs:schema>
<catalog> <product> <number>557</number> <name>Short-Sleeved Linen Blouse</name> <something>an extension element</something> <something>an extension element</something> </product> <something>an extension element</something> <something>an extension element</something> </catalog>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:defaultOpenContent mode="suffix"> <xs:any namespace="##local"/> </xs:defaultOpenContent> <xs:element name="catalog" type="CatalogType"/> <xs:complexType name="CatalogType"> <xs:openContent mode="none"/> <xs:sequence> <xs:element name="product" type="ProductType" maxOccurs="unbounded"/> </xs:sequence> </xs:complexType> <xs:complexType name="ProductType"> <xs:sequence> <xs:element name="number" type="xs:integer"/> <xs:element name="name" type="xs:string"/> </xs:sequence> </xs:complexType> </xs:schema>
<xs:complexType name="ProductType"> <xs:sequence> <!--...--> </xs:sequence> <xs:anyAttribute namespace="##other" processContents="lax"/> </xs:complexType>