Extending Complex Types

Contact Us or call 1-877-932-8228
Extending Complex Types

Extending Complex Types

New complex types can be derived by extending existing complex types. Both elements and attributes can be added in the new type, but nothing in the existing type can be overridden. New elements are appended to the content model, such that the original elements and new elements act as two groups that must appear in sequence.

The example below shows how the Person element can be extended.

Code Sample:

ReusingComponents/Demos/Book4.xsd
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
	<xs:complexType name="Person">
		<xs:sequence>
			<xs:element name="FirstName" type="xs:string"/>
			<xs:element name="MiddleName" type="xs:string" minOccurs="0"/>
			<xs:element name="LastName" type="xs:string"/>
		</xs:sequence>
		<xs:attributeGroup ref="AttGroupPerson"/>
	</xs:complexType>
	<xs:complexType name="PersonExtended">
		<xs:complexContent>
			<xs:extension base="Person">
				<xs:sequence>
					<xs:element name="Specialty">
						<xs:simpleType>
							<xs:restriction base="xs:string">
								<xs:enumeration value="Mystery"/>
								<xs:enumeration value="Humor"/>
								<xs:enumeration value="Horror"/>
								<xs:enumeration value="Childrens"/>
							</xs:restriction>
						</xs:simpleType>
					</xs:element>
				</xs:sequence>
			</xs:extension>
		</xs:complexContent>
	</xs:complexType>
---- C O D E   O M I T T E D ----

	<xs:element name="Book">
		<xs:complexType>
			<xs:sequence>
				<xs:element name="Title" type="xs:string"/>
				<xs:element name="Author" type="PersonExtended"/>
				<xs:element name="Illustrator" type="Person" minOccurs="0"/>
			</xs:sequence>
		</xs:complexType>
	</xs:element>
</xs:schema>

No material changes have been made from Book.xsd to Book4.xsd. The WinnieThePooh.xml file would be valid according to all of these schemas; however the last one would be easier to maintain and build upon than the first.

Abstract Types

When a type is made abstract, it cannot be used directly in an XML instance. One of its derived types must be used instead. The derived type is identified in the instance document using the xsi:type attribute. The schema below includes an abstract type with two derivations.

Code Sample:

ReusingComponents/Demos/Animals.xsd
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
	<xs:complexType name="Measurement">
		<xs:simpleContent>
			<xs:extension base="xs:integer">
				<xs:attribute name="units" type="xs:string"/>
			</xs:extension>
		</xs:simpleContent>
	</xs:complexType>
	<xs:element name="Weight" type="Measurement"/>
	<xs:element name="Name" type="xs:string"/>
	<!--Abstract Type-->
	<xs:complexType name="Animal" abstract="true">
		<xs:sequence>
			<xs:element ref="Name"/>
			<xs:element ref="Weight"/>
		</xs:sequence>
	</xs:complexType>
	<xs:complexType name="Dog">
		<xs:complexContent>
			<xs:extension base="Animal"/>
		</xs:complexContent>
	</xs:complexType>
	<xs:complexType name="Bird">
		<xs:complexContent>
			<xs:extension base="Animal">
				<xs:sequence>
					<xs:element name="WingSpan" type="Measurement"/>
				</xs:sequence>
			</xs:extension>
		</xs:complexContent>
	</xs:complexType>
	<xs:element name="Animals">
		<xs:complexType>
			<xs:sequence>
				<xs:element name="Animal" type="Animal" maxOccurs="unbounded"/>
			</xs:sequence>
		</xs:complexType>
	</xs:element>
</xs:schema>

The Animal type is declared as abstract by setting the abstract attribute to true. It is extended by the Dog and Bird types. The Dog type doesn't actually modify the original type at all, but the Bird type addes a WingSpan element.

Note that the Animal element declared within the Animals element is of the abstract type Animal.

Let's now look at an instance document of this schema:

Code Sample:

ReusingComponents/Demos/Animals.xml
<?xml version="1.0" encoding="UTF-8"?>
<Animals xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="Animals.xsd">
	<Animal xsi:type="Dog">
		<Name>Rover</Name>
		<Weight units="pounds">80</Weight>
	</Animal>
	<Animal xsi:type="Bird">
		<Name>Tweetie</Name>
		<Weight units="grams">15</Weight>
		<WingSpan units="cm">20</WingSpan>
	</Animal>
</Animals>

Notice that each of the Animal elements includes an xsi:type attribute. If we were to remove that attribute, the instance would be become invalid because the Animal element is of an abstract type.

Next