Derived Complex Types
We have been using the xs:extension and xs:restriction
elements without going too deeply into how or why they work. The
schema language provides functionality for extending existing types,
which is conceptually similar to that of inheritance in
object-oriented programming. The extension and restriction elements
allow new types to be defined either by expanding or limiting the
potential values of existing types.
Deriving by extension
When deriving a new type from an existing type, the
resulting type is equivalent to appending the contents of the new
declaration to the contents of the base declaration. For instance,
the following example declares a new type called mailingAddressType that extends the
physicalAddressType type to
include a Zip Code:
<xs:complexType name="mailingAddressType">
<xs:complexContent>
<xs:extension base="addr:physicalAddressType">
<xs:sequence>
<xs:element name="zipCode" type="xs:string"/>
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>This declaration appends a required element, zipCode, to the existing physicalAddressType type. The biggest
benefit of this approach is that as new declarations are added to
the underlying type, the derived type will automatically inherit
them.
Deriving by restriction
When a new type is a logical subset of an existing type,
the xs:restriction element allows this relationship to be expressed
directly. Like the xs:extension type, it allows a new type to be created based on an existing ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access