XML Schema Inheritance: Extension and Restriction
XML Schema Definition (XSD) supports object-oriented inheritance principles by allowing new data types to be derived from existing base types. This derivation occurs primarily through two mechanisms: extension, which expands a base type by adding new elements or attributes, and restriction, which constrains an existing type to a more specific subset of values or structural rules. Together, these mechanisms allow developers to create modular, reusable, and hierarchical schema designs.
Type Derivation in XML Schema
Inheritance in XSD is declared within
<xs:complexType> or
<xs:simpleType> definitions using either
<xs:complexContent> or
<xs:simpleContent>. The derived type references the
base type using the base attribute, establishing an “is-a”
relationship similar to class inheritance in object-oriented
programming.
Inheritance via Extension
Extension allows a derived type to inherit all elements and attributes of a base type while adding new child elements, attributes, or both.
Extending Complex Types
When extending a complex type, the newly declared elements are appended to the sequence or choice model defined in the base type.
<!-- Base Type -->
<xs:complexType name="PersonType">
<xs:sequence>
<xs:element name="firstName" type="xs:string"/>
<xs:element name="lastName" type="xs:string"/>
</xs:sequence>
</xs:complexType>
<!-- Derived Type via Extension -->
<xs:complexType name="EmployeeType">
<xs:complexContent>
<xs:extension base="PersonType">
<xs:sequence>
<xs:element name="employeeId" type="xs:string"/>
<xs:element name="department" type="xs:string"/>
</xs:sequence>
<xs:attribute name="hireDate" type="xs:date"/>
</xs:extension>
</xs:complexContent>
</xs:complexType>In this example, EmployeeType inherits
firstName and lastName, while adding
employeeId, department, and the
hireDate attribute.
Extending Simple Types
Extension can also transform a simple type into a complex type with simple content by adding attributes to a base text value:
<xs:complexType name="PriceType">
<xs:simpleContent>
<xs:extension base="xs:decimal">
<xs:attribute name="currency" type="xs:string" use="required"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>Inheritance via Restriction
Restriction narrows down the permissible values, occurrences, or structure of a base type. Unlike extension, restriction cannot add new fields; every valid instance of a restricted derived type must also be a valid instance of the base type.
Restricting Simple Types
Restriction on simple types applies facets (such as
xs:minInclusive, xs:pattern, or
xs:enumeration) to limit allowed data:
<!-- Base Type is xs:integer -->
<xs:simpleType name="AgeType">
<xs:restriction base="xs:integer">
<xs:minInclusive value="0"/>
<xs:maxInclusive value="120"/>
</xs:restriction>
</xs:simpleType>Restricting Complex Types
When restricting a complex type, the full structure must be
redeclared with tighter constraints, such as narrowing the occurrence
limits (minOccurs/maxOccurs) or fixing
specific values:
<!-- Base Type -->
<xs:complexType name="AddressType">
<xs:sequence>
<xs:element name="street" type="xs:string"/>
<xs:element name="apt" type="xs:string" minOccurs="0"/>
<xs:element name="postalCode" type="xs:string"/>
</xs:sequence>
</xs:complexType>
<!-- Derived Type: Requires 'apt' instead of leaving it optional -->
<xs:complexType name="ApartmentAddressType">
<xs:complexContent>
<xs:restriction base="AddressType">
<xs:sequence>
<xs:element name="street" type="xs:string"/>
<xs:element name="apt" type="xs:string" minOccurs="1"/>
<xs:element name="postalCode" type="xs:string"/>
</xs:sequence>
</xs:restriction>
</xs:complexContent>
</xs:complexType>Polymorphism via
xsi:type
XSD supports polymorphic instance documents. An XML element declared
with a base type can instantiate a derived type at runtime using the
xsi:type attribute:
<person xsi:type="EmployeeType" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<firstName>Jane</firstName>
<lastName>Doe</lastName>
<employeeId>E-9041</employeeId>
<department>Engineering</department>
</person>Summary of Differences
- Extension: Broadens the base definition by appending new elements or attributes.
- Restriction: Limits the base definition by constraining facets, tightening occurrence ranges, or eliminating optional choices.
- Compatibility: Restricted types always conform to
the base contract, whereas extended types require polymorphic validation
via
xsi:typewhen substituted for base types.