XML Schema 有一个 substitition groups 结构,它允许一些元素替换一些其他元素。这可能就是你想要的。
例如,您可以定义tag 类型的元素Tag:
<xs:element name="tag" type="Tag"/>
并在某处使用它:
<xs:complexType name="Tags">
<xs:sequence>
<xs:element ref="tag"/>
</xs:sequence>
</xs:complexType>
现在您可以定义元素quote、image 和format 来替换tag:
<xs:element name="quote" type="Quote" substitutionGroup="tag"/>
<xs:element name="image" type="Image" substitutionGroup="tag"/>
<xs:element name="format" type="Format" substitutionGroup="tag"/>
Quote、Image 和 Format 必须派生自 Tag 类型。
现在在所有使用tag 元素的地方(每个引用),它都可以替换为quote、image 或format 元素。
地理标记语言是使用此模式的架构示例,这里有一个链接到有些过时(但简洁)的版本2.1.2。
架构定义了一个抽象元素_Geometry:
<element name="_Geometry" type="gml:AbstractGeometryType" abstract="true"/>
<complexType name="AbstractGeometryType" abstract="true">
<annotation>
<documentation>
All geometry elements are derived from this abstract supertype;
a geometry element may have an identifying attribute (gid).
It may be associated with a spatial reference system.
</documentation>
</annotation>
<complexContent>
<restriction base="anyType">
<attribute name="gid" type="ID" use="optional"/>
<attribute name="srsName" type="anyURI" use="optional"/>
</restriction>
</complexContent>
</complexType>
然后定义一个元素Polygon可以替代_Geometry:
<element name="Polygon" type="gml:PolygonType" substitutionGroup="gml:_Geometry"/>
<complexType name="PolygonType">
<annotation>
<documentation>
A Polygon is defined by an outer boundary and zero or more inner
boundaries which are in turn defined by LinearRings.
</documentation>
</annotation>
<complexContent>
<extension base="gml:AbstractGeometryType">
<sequence>
<element ref="gml:outerBoundaryIs"/>
<element ref="gml:innerBoundaryIs" minOccurs="0" maxOccurs="unbounded"/>
</sequence>
</extension>
</complexContent>
</complexType>
所以现在您可以使用Polygon 和引用_Geometry 的其他元素:
<complexType name="GeometryAssociationType">
<annotation>
<documentation>
An instance of this type (e.g. a geometryMember) can either
enclose or point to a primitive geometry element. When serving
as a simple link that references a remote geometry instance,
the value of the gml:remoteSchema attribute can be used to
locate a schema fragment that constrains the target instance.
</documentation>
</annotation>
<sequence minOccurs="0">
<element ref="gml:_Geometry"/>
</sequence>
<attributeGroup ref="xlink:simpleAttrs"/>
<attribute ref="gml:remoteSchema" use="optional"/>
<!-- <attributeGroup ref="gml:AssociationAttributeGroup"/> -->
</complexType>