Definitive XML Schema
Priscilla Walmsley (pwalmsley@datypic.com)
ISBN: 0130655678
1st edition, , Prentice Hall PTR.
Chapter 14: 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
<xsd:complexType name="SizeType"> <xsd:simpleContent> <xsd:extension base="xsd:integer"> <xsd:attribute name="system" type="xsd:token"/> </xsd:extension> </xsd:simpleContent> </xsd:complexType>
<size system="US-DRESS">10</size>
<xsd:complexType name="ProductType"> <xsd:sequence> <xsd:element name="number" type="ProdNumType"/> <xsd:element name="name" type="xsd:string"/> </xsd:sequence> </xsd:complexType> <xsd:complexType name="ShirtType"> <xsd:complexContent> <xsd:extension base="ProductType"> <xsd:choice maxOccurs="unbounded"> <xsd:element name="size" type="SizeType"/> <xsd:element name="color" type="ColorType"/> </xsd:choice> </xsd:extension> </xsd:complexContent> </xsd:complexType>
<xsd:complexType name="ShirtType"> <xsd:sequence> <xsd:sequence> <xsd:element name="number" type="ProdNumType"/> <xsd:element name="name" type="xsd:string"/> </xsd:sequence> <xsd:choice maxOccurs="unbounded"> <xsd:element name="size" type="SizeType"/> <xsd:element name="color" type="ColorType"/> </xsd:choice> </xsd:sequence> </xsd:complexType>
<xsd:complexType name="ItemsType"> <xsd:choice maxOccurs="unbounded"> <xsd:element ref="shirt"/> <xsd:element ref="hat"/> <xsd:element ref="umbrella"/> </xsd:choice> </xsd:complexType> <xsd:complexType name="ExpandedItemsType"> <xsd:complexContent> <xsd:extension base="ItemsType"> <xsd:choice maxOccurs="unbounded"> <xsd:element ref="sweater"/> <xsd:element ref="suit"/> </xsd:choice> </xsd:extension> </xsd:complexContent> </xsd:complexType>
<xsd:complexType name="LetterType" mixed="true"> <xsd:sequence> <xsd:element name="custName" type="PersonNameType"/> <xsd:element name="prodName" type="xsd:string"/> <xsd:element name="prodSize" type="SizeType"/> </xsd:sequence> </xsd:complexType> <xsd:complexType name="ExtendedLetterType" mixed="true"> <xsd:complexContent> <xsd:extension base="LetterType"> <xsd:sequence> <xsd:element name="prodNum" type="ProdNumType"/> </xsd:sequence> </xsd:extension> </xsd:complexContent> </xsd:complexType>
<xsd:complexType name="ItemType"> <xsd:attribute name="routingNum" type="xsd:integer"/> </xsd:complexType> <xsd:complexType name="ProductType"> <xsd:complexContent> <xsd:extension base="ItemType"> <xsd:sequence> <xsd:element name="number" type="ProdNumType"/> <xsd:element name="name" type="xsd:string"/> </xsd:sequence> </xsd:extension> </xsd:complexContent> </xsd:complexType>
<xsd:complexType name="ItemType"> <xsd:attribute name="id" type="xsd:ID" use="required"/> <xsd:attribute ref="xml:lang"/> </xsd:complexType> <xsd:complexType name="ProductType"> <xsd:complexContent> <xsd:extension base="ItemType"> <xsd:attribute name="effDate" type="xsd:date"/> <xsd:attribute name="lang" type="xsd:language"/> </xsd:extension> </xsd:complexContent> </xsd:complexType>
<product id="prod557" xml:lang="en" lang="en" effDate="2001-04-02"/>
<xsd:complexType name="BaseType"> <xsd:anyAttribute processContents="lax" namespace="##local http://example.org/prod"/> </xsd:complexType> <xsd:complexType name="DerivedType"> <xsd:complexContent> <xsd:extension base="BaseType"> <xsd:anyAttribute processContents="strict" namespace="##targetNamespace http://www.w3.org/1999/xhtml"/> </xsd:extension> </xsd:complexContent> </xsd:complexType>
<xsd:complexType name="DerivedType"> <xsd:anyAttribute processContents="strict" namespace="##local http://example.org/prod ##targetNamespace http://www.w3.org/1999/xhtml"/> </xsd:complexType>
<xsd:complexType name="SmallSizeType"> <xsd:simpleContent> <xsd:restriction base="SizeType"> <xsd:minInclusive value="2"/> <xsd:maxInclusive value="6"/> <xsd:attribute name="system" type="xsd:token" use="required"/> </xsd:restriction> </xsd:simpleContent> </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> <xsd:complexType name="RestrictedProductType"> <xsd:complexContent> <xsd:restriction base="ProductType"> <xsd:sequence> <xsd:element name="number" type="ProdNumType"/> <xsd:element name="name" type="xsd:string"/> </xsd:sequence> </xsd:restriction> </xsd:complexContent> </xsd:complexType>
<xsd:sequence> <xsd:sequence> <xsd:element name="a"/> <xsd:element name="b"/> </xsd:sequence> </xsd:sequence>
<xsd:sequence> <xsd:element name="a"/> <xsd:element name="b"/> </xsd:sequence>
<xsd:sequence> <xsd:element name="a" maxOccurs="3"/> <xsd:element name="b" fixed="bValue"/> <xsd:element name="c" type="xsd:string"/> </xsd:sequence>
<xsd:sequence> <xsd:element name="a" maxOccurs="2"/> <xsd:element name="b" fixed="bValue"/> <xsd:element name="c" type="xsd:token"/> </xsd:sequence>
<xsd:sequence> <xsd:element name="a" maxOccurs="4"/> <xsd:element name="b" fixed="newValue"/> <xsd:element name="c" type="xsd:integer"/> </xsd:sequence>
<xsd:sequence> <xsd:element name="a"/> <xsd:any namespace="##other" maxOccurs="1"/> </xsd:sequence>
<xsd:sequence> <xsd:element name="a"/> <xsd:element ref="otherns:b"/> </xsd:sequence>
<xsd:sequence> <xsd:element name="a"/> <xsd:element ref="b"/> <xsd:element name="c"/> </xsd:sequence>
<xsd:any namespace="urn:a:1 urn:a:2" maxOccurs="2"/>
<xsd:any namespace="urn:a:1" maxOccurs="1"/>
<xsd:any namespace="##other" maxOccurs="3"/>
<xsd:sequence> <xsd:element name="a"/> <xsd:element name="b" minOccurs="0"/> </xsd:sequence>
<xsd:element name="a"/>
<xsd:sequence minOccurs="2" maxOccurs="5"> <xsd:element name="a"/> <xsd:element name="b"/> </xsd:sequence>
<xsd:sequence minOccurs="3" maxOccurs="4"> <xsd:element name="a"/> <xsd:element name="b"/> </xsd:sequence>
<xsd:sequence minOccurs="0" maxOccurs="6"> <xsd:element name="a"/> <xsd:element name="b"/> </xsd:sequence>
<xsd:all> <xsd:element name="a"/> <xsd:element name="b" minOccurs="0"/> <xsd:element name="c"/> </xsd:all>
<xsd:all> <xsd:element name="a"/> <xsd:element name="c"/> </xsd:all>
<xsd:all> <xsd:element name="c"/> <xsd:element name="a"/> </xsd:all>
<xsd:all> <xsd:element name="a"/> <xsd:element name="b" minOccurs="0"/> <xsd:element name="c"/> </xsd:all>
<xsd:all> <xsd:element name="a"/> <xsd:element name="c"/> </xsd:all>
<xsd:all> <xsd:element name="a"/> <xsd:element name="b"/> </xsd:all>
<xsd:choice> <xsd:element name="a"/> <xsd:element name="b"/> <xsd:element name="c"/> </xsd:choice>
<xsd:choice> <xsd:element name="a"/> <xsd:element name="c"/> </xsd:choice>
<xsd:choice> <xsd:element name="a"/> <xsd:element name="d"/> </xsd:choice>
<xsd:all> <xsd:element name="a"/> <xsd:element name="b" minOccurs="0"/> <xsd:element name="c"/> </xsd:all>
<xsd:sequence> <xsd:element name="a"/> <xsd:element name="c"/> </xsd:sequence>
<xsd:sequence> <xsd:element name="a"/> <xsd:element name="b"/> <xsd:element name="c" minOccurs="2"/> </xsd:sequence>
<xsd:choice maxOccurs="2"> <xsd:element name="a"/> <xsd:element name="b"/> <xsd:element name="c"/> </xsd:choice>
<xsd:sequence> <xsd:element name="a"/> <xsd:element name="c"/> </xsd:sequence>
<xsd:sequence> <xsd:element name="a"/> <xsd:element name="b"/> <xsd:element name="c"/> </xsd:sequence>
<xsd:complexType name="LetterType" mixed="true"> <xsd:sequence> <xsd:element name="custName" type="PersonNameType"/> <xsd:element name="prodName" type="xsd:string"/> <xsd:element name="prodSize" type="SizeType" minOccurs="0"/> </xsd:sequence> </xsd:complexType> <xsd:complexType name="RestrictedLetterType" mixed="true"> <xsd:complexContent> <xsd:restriction base="LetterType"> <xsd:sequence> <xsd:element name="custName" type="PersonNameType"/> <xsd:element name="prodName" type="xsd:string"/> </xsd:sequence> </xsd:restriction> </xsd:complexContent> </xsd:complexType>
<xsd:complexType name="LetterType" mixed="true"> <xsd:sequence minOccurs="0"> <xsd:element name="custName" type="PersonNameType"/> <xsd:element name="prodName" type="xsd:string"/> <xsd:element name="prodSize" type="SizeType"/> </xsd:sequence> </xsd:complexType> <xsd:complexType name="RestrictedLetterType"> <xsd:simpleContent> <xsd:restriction base="LetterType"> <xsd:simpleType> <xsd:restriction base="xsd:string"/> </xsd:simpleType> </xsd:restriction> </xsd:simpleContent> </xsd:complexType>
<xsd:complexType name="ItemType"> <xsd:attribute name="routingNum" type="xsd:integer"/> </xsd:complexType> <xsd:complexType name="RestrictedItemType"> <xsd:complexContent> <xsd:restriction base="ItemType"> <xsd:attribute name="routingNum" type="xsd:short"/> </xsd:restriction> </xsd:complexContent> </xsd:complexType>
<xsd:complexType name="BaseType"> <xsd:attribute name="a" type="xsd:integer"/> <xsd:attribute name="b" type="xsd:string"/> <xsd:attribute name="c" type="xsd:string" default="c"/> <xsd:attribute name="d" type="xsd:string"/> <xsd:attribute name="e" type="xsd:string" fixed="e"/> <xsd:attribute name="f" type="xsd:string"/> <xsd:attribute name="g" type="xsd:string"/> <xsd:attribute name="x" type="xsd:string"/> </xsd:complexType> <xsd:complexType name="DerivedType"> <xsd:complexContent> <xsd:restriction base="BaseType"> <xsd:attribute name="a" type="xsd:positiveInteger"/> <xsd:attribute name="b" type="xsd:string" default="b"/> <xsd:attribute name="c" type="xsd:string" default="c2"/> <xsd:attribute name="d" type="xsd:string" fixed="d"/> <xsd:attribute name="e" type="xsd:string" fixed="e"/> <xsd:attribute name="f" type="xsd:string" use="required"/> <xsd:attribute name="g" type="xsd:string" use="prohibited"/> </xsd:restriction> </xsd:complexContent> </xsd:complexType>
<xsd:complexType name="BaseType2"> <xsd:attribute name="h" type="xsd:integer"/> <xsd:attribute name="i" type="xsd:string" fixed="i"/> <xsd:attribute name="j" type="xsd:string" fixed="j"/> <xsd:attribute name="k" type="xsd:string" use="required"/> <xsd:attribute name="l" type="xsd:string" use="required"/> </xsd:complexType> <xsd:complexType name="IllegalDerivedType"> <xsd:complexContent> <xsd:restriction base="BaseType2"> <xsd:attribute name="h" type="xsd:decimal"/> <xsd:attribute name="i" type="xsd:string" fixed="i2"/> <xsd:attribute name="j" type="xsd:string" default="j"/> <xsd:attribute name="k" type="xsd:string"/> <xsd:attribute name="l" type="xsd:string" use="prohibited"/> <xsd:attribute ref="pref:l"/> <xsd:attribute name="m" type="xsd:string"/> </xsd:restriction> </xsd:complexContent> </xsd:complexType>
<xsd:complexType name="BaseType"> <xsd:anyAttribute processContents="lax" namespace="##any"/> </xsd:complexType> <xsd:complexType name="DerivedType"> <xsd:complexContent> <xsd:restriction base="BaseType"> <xsd:anyAttribute processContents="strict" namespace="##targetNamespace http://www.w3.org/1999/xhtml"/> </xsd:restriction> </xsd:complexContent> </xsd:complexType>
<xsd:complexType name="BaseType"> <xsd:anyAttribute processContents="lax" namespace="##any"/> </xsd:complexType> <xsd:complexType name="DerivedType"> <xsd:complexContent> <xsd:restriction base="BaseType"> <xsd:attribute name="id" type="xsd:ID" use="required"/> <xsd:attribute name="name" type="xsd:string"/> </xsd:restriction> </xsd:complexContent> </xsd:complexType>
<xsd:complexType name="ProductType"> <xsd:sequence> <xsd:element name="number" type="ProdNumType"/> <xsd:element name="name" type="xsd:string"/> </xsd:sequence> </xsd:complexType> <xsd:element name="product" type="ProductType"/> <xsd:complexType name="ShirtType"> <xsd:complexContent> <xsd:extension base="ProductType"> <xsd:choice maxOccurs="unbounded"> <xsd:element name="size" type="SizeType"/> <xsd:element name="color" type="ColorType"/> </xsd:choice> </xsd:extension> </xsd:complexContent> </xsd: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 value="blue"/> </product> <!--...--> </items>
<xsd:complexType name="ProductType" final="#all"> <xsd:sequence> <xsd:element name="number" type="ProdNumType"/> <xsd:element name="name" type="xsd:string"/> </xsd:sequence> </xsd:complexType>
<xsd:complexType name="ProductType" block="extension"> <xsd:sequence> <xsd:element name="number" type="ProdNumType"/> <xsd:element name="name" type="xsd:string"/> </xsd:sequence> </xsd:complexType> <xsd:element name="product" type="ProductType"/> <xsd:complexType name="ShirtType"> <xsd:complexContent> <xsd:extension base="ProductType"> <xsd:choice maxOccurs="unbounded"> <xsd:element name="size" type="SizeType"/> <xsd:element name="color" type="ColorType"/> </xsd:choice> </xsd:extension> </xsd:complexContent> </xsd:complexType> <xsd:element name="shirt" type="ShirtType"/>
<product xsi:type="ShirtType"> <number>557</number> <name>Short-Sleeved Linen Blouse</name> <color value="blue"/> </product>
<xsd:complexType name="ProductType" abstract="true"> <xsd:sequence> <xsd:element name="number" type="ProdNumType"/> <xsd:element name="name" type="xsd:string"/> </xsd:sequence> </xsd:complexType> <xsd:element name="product" type="ProductType"/> <xsd:complexType name="ShirtType"> <xsd:complexContent> <xsd:extension base="ProductType"> <xsd:choice maxOccurs="unbounded"> <xsd:element name="size" type="SizeType"/> <xsd:element name="color" type="ColorType"/> </xsd:choice> </xsd:extension> </xsd:complexContent> </xsd:complexType> <xsd:element name="shirt" type="ShirtType"/>
<product xsi:type="ShirtType"> <number>557</number> <name>Short-Sleeved Linen Blouse</name> <color value="blue"/> </product> <shirt> <number>557</number> <name>Short-Sleeved Linen Blouse</name> <color value="blue"/> </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>