4.7 Abstract Elements and TypesAbstract Elements and Types
ref26XML Schema provides a mechanism to
force substitution for a particular element or type. When
an element or type is declared to be "abstract", it cannot
be used in an instance document. When an element is
declared to be abstract, a member of that element's
substitution group must appear in the instance document.
When an element's corresponding type definition is declared
as abstract, all instances of that element must use
xsi:type to
indicate a derived type that is not abstract.
In the substitution group example we described in [Substitution Groups], it would be useful to
specifically disallow use of the comment
element so that instances must make use of the
customerComment and shipComment
elements. To declare the comment element
abstract, we modify its original declaration in the
international purchase order schema, ipo.xsd, as follows:
NOTE:
<element name="comment" type="string" abstract="true"/>
With comment declared as abstract, instances
of international purchase orders are now only valid if they
contain customerComment and
shipComment elements.
Declaring an element as abstract requires the use of a
substitution group. Declaring a type as abstract simply
requires the use of a type derived from it (and identified
by the
xsi:type attribute) in the instance document.
Consider the following schema definition:
NOTE:
Schema for Vehicles
<schema xmlns="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://cars.example.com/schema"
xmlns:target="http://cars.example.com/schema">
<complexType name="Vehicle" abstract="true"/>
<complexType name="Car">
<complexContent>
<extension base="target:Vehicle"/>
</complexContent>
</complexType>
<complexType name="Plane">
<complexContent>
<extension base="target:Vehicle"/>
</complexContent>
</complexType>
<element name="transport" type="target:Vehicle"/>
</schema>
The transport element is not abstract,
therefore it can appear in instance documents. However,
because its type definition is abstract, it may never
appear in an instance document without an xsi:type attribute that
refers to a derived type. That means the following is not
schema-valid:
NOTE:
<transport xmlns="http://cars.example.com/schema"/>
because the transport element's type is
abstract. However, the following is schema-valid:
NOTE:
<transport xmlns="http://cars.example.com/schema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:type="Car"/>
because it uses a non-abstract type that is substitutable
for Vehicle.
|