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 ...
Get XML in a Nutshell, 3rd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.