Definitive XML Schema
Priscilla Walmsley (pwalmsley@datypic.com)
ISBN: 0132886723
2nd edition, , Prentice Hall PTR.
Chapter 13: Deriving complex types
Full example
This example illustrates complex types that are derived from other specified types.
<items xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="chapter14.xsd"> <hat routingNum="123456" effDate="2002-04-02" lang="en-US"> <number>557</number> <name>Ten-Gallon Hat</name> <description>This is a great hat!</description> </hat> <umbrella routingNum="123" effDate="2002-04-02"> <number>557</number> <name>Ten-Gallon Hat</name> </umbrella> <shirt routingNum="124" effDate="2002-04-02" sleeve="17"> <number>557</number> <name>Short-Sleeved Linen Blouse</name> <color value="blue"/> <size system="US-DRESS">6</size> </shirt> </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="hat" type="ProductType"/> <xs:element name="umbrella" type="RestrictedProductType"/> <xs:element name="shirt" type="ShirtType"/> </xs:choice> </xs:complexType> <!--Empty Content Type--> <xs:complexType name="ItemType" abstract="true"> <xs:attribute name="routingNum" type="xs:integer"/> </xs:complexType> <!--Empty Content Extension (with Attribute Extension)--> <xs:complexType name="ProductType"> <xs:complexContent> <xs:extension base="ItemType"> <xs:sequence> <xs:element name="number" type="xs:integer"/> <xs:element name="name" type="xs:string"/> <xs:element name="description" type="xs:string" minOccurs="0"/> </xs:sequence> <xs:attribute name="effDate" type="xs:date"/> <xs:attribute name="lang" type="xs:language"/> </xs:extension> </xs:complexContent> </xs:complexType> <!--Complex Content Restriction--> <xs:complexType name="RestrictedProductType"> <xs:complexContent> <xs:restriction base="ProductType"> <xs:sequence> <xs:element name="number" type="xs:integer"/> <xs:element name="name" type="xs:token"/> </xs:sequence> <xs:attribute name="routingNum" type="xs:short" use="required"/> <xs:attribute name="effDate" type="xs:date" default="1900-01-01"/> <xs:attribute name="lang" use="prohibited"/> </xs:restriction> </xs:complexContent> </xs:complexType> <!--Complex Content Extension--> <xs:complexType name="ShirtType"> <xs:complexContent> <xs:extension base="RestrictedProductType"> <xs:choice maxOccurs="unbounded"> <xs:element name="size" type="SmallSizeType"/> <xs:element name="color" type="ColorType"/> </xs:choice> <xs:attribute name="sleeve" type="xs:integer"/> </xs:extension> </xs:complexContent> </xs:complexType> <!--Simple Content Extension--> <xs:complexType name="SizeType"> <xs:simpleContent> <xs:extension base="xs:integer"> <xs:attribute name="system" type="xs:token"/> </xs:extension> </xs:simpleContent> </xs:complexType> <!--Simple Content Restriction--> <xs:complexType name="SmallSizeType"> <xs:simpleContent> <xs:restriction base="SizeType"> <xs:minInclusive value="2"/> <xs:maxInclusive value="6"/> <xs:attribute name="system" type="xs:token" use="required"/> </xs:restriction> </xs:simpleContent> </xs:complexType> <xs:complexType name="ColorType"> <xs:attribute name="value" type="xs:string"/> </xs:complexType> </xs:schema>
Book examples
<xs:complexType name="SizeType"> <xs:simpleContent> <xs:extension base="xs:integer"> <xs:attribute name="system" type="xs:token"/> </xs:extension> </xs:simpleContent> </xs:complexType>
<size system="US-DRESS">10</size>
<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:complexType name="ShirtType"> <xs:complexContent> <xs:extension base="ProductType"> <xs:choice maxOccurs="unbounded"> <xs:element name="size" type="xs:integer"/> <xs:element name="color" type="xs:string"/> </xs:choice> </xs:extension> </xs:complexContent> </xs:complexType>
<xs:complexType name="ShirtType"> <xs:sequence> <xs:sequence> <xs:element name="number" type="xs:integer"/> <xs:element name="name" type="xs:string"/> </xs:sequence> <xs:choice maxOccurs="unbounded"> <xs:element name="size" type="xs:integer"/> <xs:element name="color" type="xs:string"/> </xs:choice> </xs:sequence> </xs:complexType>
<xs:complexType name="ItemsType"> <xs:choice maxOccurs="unbounded"> <xs:element ref="shirt"/> <xs:element ref="hat"/> <xs:element ref="umbrella"/> </xs:choice> </xs:complexType> <xs:complexType name="ExpandedItemsType"> <xs:complexContent> <xs:extension base="ItemsType"> <xs:choice maxOccurs="unbounded"> <xs:element ref="sweater"/> <xs:element ref="suit"/> </xs:choice> </xs:extension> </xs:complexContent> </xs:complexType>
<xs:complexType name="ProductType"> <xs:all> <xs:element name="number" type="xs:integer"/> <xs:element name="name" type="xs:string"/> </xs:all> </xs:complexType> <xs:complexType name="ShirtType"> <xs:complexContent> <xs:extension base="ProductType"> <xs:all> <xs:element name="size" type="xs:integer"/> <xs:element name="color" type="xs:string"/> </xs:all> </xs:extension> </xs:complexContent> </xs:complexType>
<xs:complexType name="ShirtType"> <xs:all> <xs:element name="number" type="xs:integer"/> <xs:element name="name" type="xs:string"/> <xs:element name="size" type="xs:integer"/> <xs:element name="color" type="xs:string"/> </xs:all> </xs:complexType>
<xs:complexType name="ProductType"> <xs:openContent mode="suffix"> <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> <xs:complexType name="ShirtType"> <xs:complexContent> <xs:extension base="ProductType"> <xs:openContent mode="suffix"> <xs:any namespace="##any" processContents="lax"/> </xs:openContent> <xs:sequence> <xs:element name="size" type="xs:integer"/> <xs:element name="color" type="xs:string"/> </xs:sequence> </xs:extension> </xs:complexContent> </xs:complexType>
<xs:complexType name="LetterType" mixed="true"> <xs:sequence> <xs:element name="custName" type="xs:string"/> <xs:element name="prodName" type="xs:string"/> <xs:element name="prodSize" type="xs:integer"/> </xs:sequence> </xs:complexType> <xs:complexType name="ExtendedLetterType" mixed="true"> <xs:complexContent> <xs:extension base="LetterType"> <xs:sequence> <xs:element name="prodNum" type="xs:string"/> </xs:sequence> </xs:extension> </xs:complexContent> </xs:complexType>
<xs:complexType name="ItemType"> <xs:attribute name="routingNum" type="xs:integer"/> </xs:complexType> <xs:complexType name="ProductType"> <xs:complexContent> <xs:extension base="ItemType"> <xs:sequence> <xs:element name="number" type="xs:integer"/> <xs:element name="name" type="xs:string"/> </xs:sequence> </xs:extension> </xs:complexContent> </xs:complexType>
<xs:complexType name="ItemType"> <xs:attribute name="id" type="xs:ID" use="required"/> <xs:attribute ref="xml:lang"/> </xs:complexType> <xs:complexType name="ProductType"> <xs:complexContent> <xs:extension base="ItemType"> <xs:attribute name="effDate" type="xs:date"/> <xs:attribute name="lang" type="xs:language"/> </xs:extension> </xs:complexContent> </xs:complexType>
<product id="prod557" xml:lang="en" lang="en" effDate="2001-04-12"/>
<xs:complexType name="BaseType"> <xs:anyAttribute processContents="lax" namespace="##local http://datypic.com/prod"/> </xs:complexType> <xs:complexType name="DerivedType"> <xs:complexContent> <xs:extension base="BaseType"> <xs:anyAttribute processContents="strict" namespace="##targetNamespace http://www.w3.org/1999/xhtml"/> </xs:extension> </xs:complexContent> </xs:complexType>
<xs:complexType name="DerivedType"> <xs:anyAttribute processContents="strict" namespace="##local http://datypic.com/prod ##targetNamespace http://www.w3.org/1999/xhtml"/> </xs:complexType>
<xs:complexType name="SmallSizeType"> <xs:simpleContent> <xs:restriction base="SizeType"> <xs:minInclusive value="2"/> <xs:maxInclusive value="6"/> <xs:attribute name="system" type="xs:token" use="required"/> </xs:restriction> </xs:simpleContent> </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="xs:integer" minOccurs="0"/> <xs:element name="color" type="xs:string" minOccurs="0"/> </xs:sequence> </xs:complexType> <xs:complexType name="RestrictedProductType"> <xs:complexContent> <xs:restriction base="ProductType"> <xs:sequence> <xs:element name="number" type="xs:integer"/> <xs:element name="name" type="xs:string"/> </xs:sequence> </xs:restriction> </xs:complexContent> </xs:complexType>
<xs:sequence> <xs:sequence> <xs:element name="a"/> <xs:element name="b"/> </xs:sequence> </xs:sequence>
<xs:sequence> <xs:element name="a"/> <xs:element name="b"/> </xs:sequence>
<xs:sequence> <xs:element name="a" maxOccurs="3"/> <xs:element name="b" fixed="bValue"/> <xs:element name="c" type="xs:string"/> </xs:sequence>
<xs:sequence> <xs:element name="a" maxOccurs="2"/> <xs:element name="b" fixed="bValue"/> <xs:element name="c" type="xs:token"/> </xs:sequence>
<xs:sequence> <xs:element name="a" maxOccurs="4"/> <xs:element name="b" fixed="newValue"/> <xs:element name="c" type="xs:integer"/> </xs:sequence>
<xs:sequence> <xs:element name="a"/> <xs:any namespace="##other" maxOccurs="1"/> </xs:sequence>
<xs:sequence> <xs:element name="a"/> <xs:element ref="otherns:b"/> </xs:sequence>
<xs:sequence> <xs:element name="a"/> <xs:element ref="b"/> <xs:element name="c"/> </xs:sequence>
<xs:any namespace="urn:a:1 urn:a:2" maxOccurs="2"/>
<xs:any namespace="urn:a:1" maxOccurs="1"/>
<xs:any namespace="##other" maxOccurs="3"/>
<xs:sequence> <xs:element name="a"/> <xs:element name="b" minOccurs="0"/> </xs:sequence>
<xs:element name="a"/>
<xs:sequence minOccurs="2" maxOccurs="5"> <xs:element name="a"/> <xs:element name="b"/> </xs:sequence>
<xs:sequence minOccurs="3" maxOccurs="4"> <xs:element name="a"/> <xs:element name="b"/> </xs:sequence>
<xs:sequence minOccurs="0" maxOccurs="6"> <xs:element name="a"/> <xs:element name="b"/> </xs:sequence>
<xs:all> <xs:element name="a"/> <xs:element name="b" minOccurs="0"/> <xs:element name="c"/> </xs:all>
<xs:all> <xs:element name="a"/> <xs:element name="c"/> </xs:all>
<xs:all> <xs:element name="c"/> <xs:element name="a"/> </xs:all>
<xs:all> <xs:element name="a"/> <xs:element name="b" minOccurs="0"/> <xs:element name="c"/> </xs:all>
<xs:all> <xs:element name="a"/> <xs:element name="c"/> </xs:all>
<xs:all> <xs:element name="a"/> <xs:element name="b"/> </xs:all>
<xs:choice> <xs:element name="a"/> <xs:element name="b"/> <xs:element name="c"/> </xs:choice>
<xs:choice> <xs:element name="a"/> <xs:element name="c"/> </xs:choice>
<xs:choice> <xs:element name="a"/> <xs:element name="d"/> </xs:choice>
<xs:all> <xs:element name="a"/> <xs:element name="b" minOccurs="0"/> <xs:element name="c"/> </xs:all>
<xs:sequence> <xs:element name="a"/> <xs:element name="c"/> </xs:sequence>
<xs:sequence> <xs:element name="a"/> <xs:element name="b"/> <xs:element name="c" minOccurs="2"/> </xs:sequence>
<xs:choice maxOccurs="2"> <xs:element name="a"/> <xs:element name="b"/> <xs:element name="c"/> </xs:choice>
<xs:sequence> <xs:element name="a"/> <xs:element name="c"/> </xs:sequence>
<xs:sequence> <xs:element name="a"/> <xs:element name="b"/> <xs:element name="c"/> </xs:sequence>
<xs:complexType name="BaseType"> <xs:openContent mode="suffix"> <xs:any namespace="http://datypic.com/prod http://datypic.com/ord"/> </xs:openContent> <xs:sequence> <xs:element name="a" type="xs:string" minOccurs="0"/> </xs:sequence> </xs:complexType>
<xs:complexType name="LegalDerivedType"> <xs:complexContent> <xs:restriction base="BaseType"> <xs:openContent mode="suffix"> <xs:any namespace="http://datypic.com/prod"/> </xs:openContent> <xs:sequence> <xs:element name="a" type="xs:string" minOccurs="0"/> </xs:sequence> </xs:restriction> </xs:complexContent> </xs:complexType>
<xs:complexType name="IllegalDerivedType"> <xs:complexContent> <xs:restriction base="BaseType"> <xs:openContent mode="interleave"> <xs:any namespace="##any"/> </xs:openContent> <xs:sequence> <xs:element name="a" type="xs:string" minOccurs="0"/> </xs:sequence> </xs:restriction> </xs:complexContent> </xs:complexType>
<xs:complexType name="LetterType" mixed="true"> <xs:sequence> <xs:element name="custName" type="xs:string"/> <xs:element name="prodName" type="xs:string"/> <xs:element name="prodSize" type="xs:integer" minOccurs="0"/> </xs:sequence> </xs:complexType> <xs:complexType name="RestrictedLetterType" mixed="true"> <xs:complexContent> <xs:restriction base="LetterType"> <xs:sequence> <xs:element name="custName" type="xs:string"/> <xs:element name="prodName" type="xs:string"/> </xs:sequence> </xs:restriction> </xs:complexContent> </xs:complexType>
<xs:complexType name="LetterType" mixed="true"> <xs:sequence minOccurs="0"> <xs:element name="custName" type="xs:string"/> <xs:element name="prodName" type="xs:string"/> <xs:element name="prodSize" type="xs:integer"/> </xs:sequence> </xs:complexType> <xs:complexType name="RestrictedLetterType"> <xs:simpleContent> <xs:restriction base="LetterType"> <xs:simpleType> <xs:restriction base="xs:string"/> </xs:simpleType> </xs:restriction> </xs:simpleContent> </xs:complexType>
<xs:complexType name="ItemType"> <xs:attribute name="routingNum" type="xs:integer"/> </xs:complexType> <xs:complexType name="RestrictedItemType"> <xs:complexContent> <xs:restriction base="ItemType"> <xs:attribute name="routingNum" type="xs:short"/> </xs:restriction> </xs:complexContent> </xs:complexType>
<xs:complexType name="BaseType"> <xs:attribute name="a" type="xs:integer"/> <xs:attribute name="b" type="xs:string"/> <xs:attribute name="c" type="xs:string" default="c"/> <xs:attribute name="d" type="xs:string"/> <xs:attribute name="e" type="xs:string" fixed="e"/> <xs:attribute name="f" type="xs:string"/> <xs:attribute name="g" type="xs:string"/> <xs:attribute name="x" type="xs:string"/> </xs:complexType> <xs:complexType name="DerivedType"> <xs:complexContent> <xs:restriction base="BaseType"> <xs:attribute name="a" type="xs:positiveInteger"/> <xs:attribute name="b" type="xs:string" default="b"/> <xs:attribute name="c" type="xs:string" default="c2"/> <xs:attribute name="d" type="xs:string" fixed="d"/> <xs:attribute name="e" type="xs:string" fixed="e"/> <xs:attribute name="f" type="xs:string" use="required"/> <xs:attribute name="g" type="xs:string" use="prohibited"/> </xs:restriction> </xs:complexContent> </xs:complexType>
<xs:complexType name="BaseType2"> <xs:attribute name="h" type="xs:integer"/> <xs:attribute name="i" type="xs:string" fixed="i"/> <xs:attribute name="j" type="xs:string" fixed="j"/> <xs:attribute name="k" type="xs:string" use="required"/> <xs:attribute name="l" type="xs:string" use="required"/> </xs:complexType> <xs:complexType name="IllegalDerivedType"> <xs:complexContent> <xs:restriction base="BaseType2"> <xs:attribute name="h" type="xs:decimal"/> <xs:attribute name="i" type="xs:string" fixed="i2"/> <xs:attribute name="j" type="xs:string" default="j"/> <xs:attribute name="k" type="xs:string"/> <xs:attribute name="l" type="xs:string" use="prohibited"/> <xs:attribute ref="pref:l"/> <xs:attribute name="m" type="xs:string"/> </xs:restriction> </xs:complexContent> </xs:complexType>
<xs:complexType name="BaseType"> <xs:anyAttribute processContents="lax" namespace="##any"/> </xs:complexType> <xs:complexType name="DerivedType"> <xs:complexContent> <xs:restriction base="BaseType"> <xs:anyAttribute processContents="strict" namespace="##targetNamespace http://www.w3.org/1999/xhtml"/> </xs:restriction> </xs:complexContent> </xs:complexType>
<xs:complexType name="BaseType"> <xs:anyAttribute processContents="lax" namespace="##any"/> </xs:complexType> <xs:complexType name="DerivedType"> <xs:complexContent> <xs:restriction base="BaseType"> <xs:attribute name="id" type="xs:ID" use="required"/> <xs:attribute name="name" type="xs:string"/> </xs:restriction> </xs:complexContent> </xs:complexType>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://datypic.com/prod" xmlns:prod="http://datypic.com/prod" elementFormDefault="qualified" attributeFormDefault="qualified"> <xs:complexType name="ProductType"> <xs:sequence> <xs:element ref="prod:number"/> <xs:element ref="prod:name"/> <xs:element ref="prod:size" minOccurs="0"/> </xs:sequence> <xs:attribute ref="prod:dept"/> </xs:complexType> <xs:element name="number" type="xs:integer"/> <xs:element name="name" type="xs:string"/> <xs:element name="size" type="xs:integer"/> <xs:attribute name="dept" type="xs:string"/> </xs:schema>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://datypic.com/ord" xmlns:prod="http://datypic.com/prod"> <xs:import namespace="http://datypic.com/prod" schemaLocation="prod.xsd"/> <xs:complexType name="RestrictedProductType"> <xs:complexContent> <xs:restriction base="prod:ProductType"> <xs:sequence> <xs:element ref="prod:number"/> <xs:element ref="prod:name"/> </xs:sequence> <xs:attribute ref="prod:dept" use="required"/> </xs:restriction> </xs:complexContent> </xs:complexType> </xs:schema>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://datypic.com/prod" elementFormDefault="qualified" attributeFormDefault="qualified"> <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="xs:integer" minOccurs="0"/> </xs:sequence> <xs:attribute name="dept" type="xs:string"/> </xs:complexType> </xs:schema>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://datypic.com/ord" xmlns:prod="http://datypic.com/prod" elementFormDefault="qualified" attributeFormDefault="qualified"> <xs:import namespace="http://datypic.com/prod" schemaLocation="prod.xsd"/> <xs:complexType name="RestrictedProductType"> <xs:complexContent> <xs:restriction base="prod:ProductType"> <xs:sequence> <xs:element name="number" type="xs:integer" targetNamespace="http://datypic.com/prod"/> <xs:element name="name" type="xs:string" targetNamespace="http://datypic.com/prod"/> </xs:sequence> <xs:attribute name="dept" type="xs:string" use="required" targetNamespace="http://datypic.com/prod"/> </xs:restriction> </xs:complexContent> </xs:complexType> </xs:schema>
<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="product" type="ProductType"/> <xs:complexType name="ShirtType"> <xs:complexContent> <xs:extension base="ProductType"> <xs:choice maxOccurs="unbounded"> <xs:element name="size" type="xs:integer"/> <xs:element name="color" type="xs:string"/> </xs:choice> </xs:extension> </xs:complexContent> </xs:complexType>
<items xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <product xsi:type="ShirtType"> <number>557</number> <name>Short-Sleeved Linen Blouse</name> <color>blue</color> </product> <!--...--> </items>
<xs:complexType name="ProductType" final="#all"> <xs:sequence> <xs:element name="number" type="xs:integer"/> <xs:element name="name" type="xs:string"/> </xs:sequence> </xs:complexType>
<xs:complexType name="ProductType" block="extension"> <xs:sequence> <xs:element name="number" type="xs:integer"/> <xs:element name="name" type="xs:string"/> </xs:sequence> </xs:complexType> <xs:element name="product" type="ProductType"/> <xs:complexType name="ShirtType"> <xs:complexContent> <xs:extension base="ProductType"> <xs:choice maxOccurs="unbounded"> <xs:element name="size" type="xs:integer"/> <xs:element name="color" type="xs:string"/> </xs:choice> </xs:extension> </xs:complexContent> </xs:complexType> <xs:element name="shirt" type="ShirtType"/>
<product xsi:type="ShirtType"> <number>557</number> <name>Short-Sleeved Linen Blouse</name> <color>blue</color> </product>
<xs:complexType name="ProductType" abstract="true"> <xs:sequence> <xs:element name="number" type="xs:integer"/> <xs:element name="name" type="xs:string"/> </xs:sequence> </xs:complexType> <xs:element name="product" type="ProductType"/> <xs:complexType name="ShirtType"> <xs:complexContent> <xs:extension base="ProductType"> <xs:choice maxOccurs="unbounded"> <xs:element name="size" type="xs:integer"/> <xs:element name="color" type="xs:string"/> </xs:choice> </xs:extension> </xs:complexContent> </xs:complexType> <xs:element name="shirt" type="ShirtType"/>
<product xsi:type="ShirtType"> <number>557</number> <name>Short-Sleeved Linen Blouse</name> <color>blue</color> </product> <shirt> <number>557</number> <name>Short-Sleeved Linen Blouse</name> <color>blue</color> </shirt>
<product> <number>557</number> <name>Short-Sleeved Linen Blouse</name> </product> <product xsi:type="ProductType"> <number>557</number> <name>Short-Sleeved Linen Blouse</name> </product>