XML 模式规范支持按扩展名派生复杂类型的功能。
在 XML 实例文档中,您通过均派生自同一基本类型的一组类型来声明 XML 元素的类型,而非在 XML 模式中显式声明 XML 元素的类型。元素的类型使用 type 属性来定义,如以下示例 XML 实例文档中所示:
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns="http://shapesrus.com/example" xmlns:xsd="http://www.w3.org/2001/XMLSchema-instance" xsd:type="circle">
<color xmlns="">blue</color>
<diameter xmlns="">10</diameter>
</shape>
在前面的 XML 实例文档中,<shape> 元素声明为 circle 类型。 还可以将 XML 实例文档中的 <shape> 元素定义为 rectangle 类型,如以下示例 XML 实例文档中所定义:
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns="http://shapesrus.com/example" xmlns:xsd="http://www.w3.org/2001/XMLSchema-instance" xsd:type="rectangle">
<color xmlns="">blue</color>
<length xmlns="">5</length>
<width xmlns="">8</width>
</shape>
在此示例中,<shape> 元素基于 shapeType 基础类型的派生类型之一。shapeType 的派生类型被命名为 circle 和 rectangle,如以下示例 XML 模式中所示:在上述示例 XML 模式中,名为 <shapeType> 的基本类型被定义为抽象基本类型。不能在 XML 实例文档中使用抽象基本类型来定义元素类型 - 必须指定某一派生类型。在此示例中,无法将 XML 实例文档中的 <shape> 元素声明为 shapeType,必须将其声明为派生类型之一:circle 或 rectangle。
<xsd:complexType name="shapeType">
<xsd:sequence>
<xsd:element name="color" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
WSDL 是用于定义 Web service 的特定 XML 模式。还支持将类型替换用于 WSDL。您可以为重现目标节点的扩展出现指定替换类型。仅对重现目标节点支持扩展出现的此类型替换。目标节点位于映射窗格右侧。
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://shapesrus.com/example"
xmlns:sru="http://shapesrus.com/example">
<xsd:element name="shape" type="sru:shapeType"/>
<xsd:complexType name="shapeType" abstract="true">
<xsd:sequence>
<xsd:element name="color" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="circle">
<xsd:complexContent>
<xsd:extension base="sru:shapeType">
<xsd:sequence>
<xsd:element name="diameter" type="xsd:integer"/>
</xsd:sequence>
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
<xsd:complexType name="rectangle">
<xsd:complexContent>
<xsd:extension base="sru:shapeType">
<xsd:sequence>
<xsd:element name="length" type="xsd:integer"/>
<xsd:element name="width" type="xsd:integer"/>
</xsd:sequence>
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
</xsd:schema>