Definitive XML Schema
Priscilla Walmsley (pwalmsley@datypic.com)
ISBN: 0130655678
1st edition, , Prentice Hall PTR.
Chapter 13: 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"/>
<xsd:complexType name="ProductType"> <xsd:sequence> <xsd:element name="number" type="ProdNumType"/> <xsd:element name="name" type="xsd:string"/> <xsd:element name="size" type="SizeType"/> </xsd:sequence> </xsd:complexType> <xsd:element name="product" type="ProductType"/>
<xsd:element name="product"> <xsd:complexType> <xsd:sequence> <xsd:element name="number" type="ProdNumType"/> <xsd:element name="name" type="xsd:string"/> <xsd:element name="size" type="SizeType"/> </xsd:sequence> </xsd:complexType> </xsd:element>
<size system="US-DRESS">10</size>
<xsd:complexType name="SizeType"> <xsd:simpleContent> <xsd:extension base="xsd:integer"> <xsd:attribute name="system" type="xsd:token"/> </xsd:extension> </xsd:simpleContent> </xsd:complexType>
<product> <number>557</number> <name>Short-Sleeved Linen Blouse</name> <size system="US-DRESS">10</size> <color value="blue"/> </product>
<xsd:complexType name="ProductType"> <xsd:sequence> <xsd:element name="number" type="ProdNumType"/> <xsd:element name="name" type="xsd:string"/> <xsd:element name="size" type="SizeType"/> <xsd:element name="color" type="ColorType"/> </xsd:sequence> </xsd:complexType>
<letter>Dear <custName>Priscilla Walmsley</custName>, Unfortunately, we are out of stock of the <prodName>Short-Sleeved Linen Blouse</prodName> in size <prodSize>10</prodSize> that you ordered...</letter>
<xsd:complexType name="LetterType" mixed="true"> <xsd:sequence> <xsd:element name="custName" type="CustNameType"/> <xsd:element name="prodName" type="xsd:string"/> <xsd:element name="prodSize" type="SizeType"/> <!--...--> </xsd:sequence> </xsd:complexType>
<color value="blue"/>
<xsd:complexType name="ColorType"> <xsd:attribute name="value" type="ColorValueType"/> </xsd:complexType>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <xsd:element name="number" type="ProdNumType"/> <xsd:element name="name" type="xsd:string"/> <xsd:element name="size" type="SizeType"/> <xsd:element name="color" type="ColorType"/> <xsd:complexType name="ProductType"> <xsd:sequence> <xsd:element ref="number"/> <xsd:element ref="name"/> <xsd:element ref="size" minOccurs="0"/> <xsd:element ref="color" minOccurs="0"/> </xsd:sequence> </xsd:complexType> </xsd:schema>
<xsd:complexType name="DescriptionType" mixed="true"> <xsd:sequence> <xsd:any namespace="http://www.w3.org/1999/xhtml" minOccurs="0" maxOccurs="unbounded" processContents="skip"/> </xsd:sequence> </xsd: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>
<catalog xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.w3.org/1999/xhtml xhtml.xsd"> <description> This shirt is the <xhtml:b>best-selling</xhtml:b> shirt in our catalog! <xhtml:br/> Note: runs large. </description> <!--...--> </catalog>
<xsd:complexType name="ProductType"> <xsd:choice> <xsd:sequence> <xsd:element name="number" type="ProdNumType"/> <xsd:element name="name" type="xsd:string" minOccurs="0"/> </xsd:sequence> <xsd:element name="name" type="xsd:string"/> </xsd:choice> </xsd:complexType>
<xsd:complexType name="ProductType"> <xsd:choice> <xsd:sequence> <xsd:element name="number" type="ProdNumType"/> <xsd:element name="name" type="xsd:string" minOccurs="0"/> </xsd:sequence> <xsd:element name="name" type="xsd:token"/> </xsd:choice> </xsd:complexType>
<xsd:complexType name="ProductType"> <xsd:sequence> <xsd:element name="number" type="ProdNumType"/> <xsd:element name="name" type="xsd:string"/> <xsd:element name="size" type="SizeType" minOccurs="0"/> <xsd:element name="color" type="ColorType" minOccurs="0"/> </xsd:sequence> </xsd: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>
<xsd:complexType name="ItemsType"> <xsd:choice> <xsd:element name="shirt" type="ShirtType"/> <xsd:element name="hat" type="HatType"/> <xsd:element name="umbrella" type="UmbrellaType"/> </xsd:choice> </xsd:complexType>
<items> <shirt>...</shirt> </items> <items> <hat>...</hat> </items>
<xsd:complexType name="ItemsType"> <xsd:choice minOccurs="0" maxOccurs="unbounded"> <xsd:element name="shirt" type="ShirtType"/> <xsd:element name="umbrella" type="UmbrellaType"/> <xsd:element name="hat" type="HatType"/> </xsd:choice> </xsd:complexType>
<items> <shirt>...</shirt> <hat>...</hat> <umbrella>...</umbrella> <shirt>...</shirt> <shirt>...</shirt> </items>
<xsd:complexType name="ProductType"> <xsd:sequence> <xsd:element name="number" type="ProdNumType"/> <xsd:element name="name" type="xsd:string"/> <xsd:choice minOccurs="0" maxOccurs="unbounded"> <xsd:element name="size" type="SizeType"/> <xsd:element name="color" type="ColorType"/> </xsd:choice> </xsd:sequence> </xsd:complexType>
<xsd:complexType name="ProductType"> <xsd:all> <xsd:element name="number" type="ProdNumType"/> <xsd:element name="name" type="xsd:string"/> <xsd:element name="size" type="SizeType" minOccurs="0"/> <xsd:element name="color" type="ColorType" minOccurs="0"/> </xsd:all> </xsd: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>
<xsd:complexType name="ProductType"> <xsd:sequence> <xsd:element name="number" type="ProdNumType"/> <xsd:element name="name" type="xsd:string"/> <xsd:all> <xsd:element name="size" type="SizeType" minOccurs="0"/> <xsd:element name="color" type="ColorType" minOccurs="0"/> </xsd:all> </xsd:sequence> </xsd:complexType>
<xsd:complexType name="ProductType"> <xsd:sequence> <xsd:group ref="DescriptionGroup"/> <xsd:element name="number" type="ProdNumType"/> <xsd:element name="name" type="xsd:string"/> </xsd:sequence> </xsd:complexType>
<xsd:complexType name="AOrBOrBothType"> <xsd:choice> <xsd:element name="a" type="xsd:string"/> <xsd:element name="b" type="xsd:string"/> <xsd:sequence> <xsd:element name="a" type="xsd:string"/> <xsd:element name="b" type="xsd:string"/> </xsd:sequence> </xsd:choice> </xsd:complexType>
<xsd:complexType name="AOrBOrBothType"> <xsd:choice> <xsd:sequence> <xsd:element name="a" type="xsd:string"/> <xsd:element name="b" type="xsd:string" minOccurs="0"/> </xsd:sequence> <xsd:element name="b" type="xsd:string"/> </xsd:choice> </xsd:complexType>
<xsd:complexType name="ProductType"> <xsd:sequence> <!--...--> </xsd:sequence> <xsd:attribute name="effDate" type="xsd:date" default="1900-01-01"/> </xsd:complexType>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <xsd:attribute name="effDate" type="xsd:date" default="1900-01-01"/> <xsd:complexType name="ProductType"> <xsd:sequence> <!--...--> </xsd:sequence> <xsd:attribute ref="effDate" default="2000-12-31"/> </xsd:complexType> </xsd:schema>
<xsd:complexType name="ProductType"> <xsd:sequence> <!--...--> </xsd:sequence> <xsd:anyAttribute namespace="##other" processContents="lax"/> </xsd:complexType>
<xsd:complexType name="ProductType"> <xsd:sequence> <!--...--> </xsd:sequence> <xsd:attributeGroup ref="IdentifierGroup"/> <xsd:attribute name="effDate" type="xsd:date"/> </xsd:complexType>