使用扩展机制进行复杂类型派生
由扩展派生的复杂类型是其基本数据类型的功能超集。 顾名思义,其基本数据类型是要定义的类型的基础,与基本数据类型的差异体现在附加的内容中。 本主题提供一个示例,其中 ShippingAddress 和 BillingAddress 的 两个元素基于复杂的全局类型 GlobalAddrType。 ShippingAddress 仅定义为 GlobalAddrType 类型,而 BillingAddress 则定义为扩展 GlobalAddrType 类型。 在示例结束时,向 BillingAddress 添加了一个名为 Department 的附加元素,该元素的类型为字符串,默认值为“应付账款”。
有关使用扩展机制派生新的复杂类型的全面信息,请参阅 W3C 网站。 有关指向此网站和其他网站的各种链接,请参阅 Web 上的 XSD 资源。
若要通过扩展从复杂全局类型派生,请在架构树中的另一个位置,首先在所需位置插入新的 Record 节点。 然后将其 “基数据类型” 属性设置为复杂全局类型的名称。
在下面的示例中, BillingAddress 是新插入的 Record 节点的名称, GlobalAddrType 是它从中派生并打算扩展的复杂全局类型的名称。 在架构树视图中, 将“基本数据类型” 设置为 GlobalAddrType 后,名为 BillingAddress 的节点下方会显示重复的节点结构,与名为 ShippingAddress 的节点下的相邻节点结构相同。 它们之间的差别在于 ,BillingAddress 节点结构可扩展到基础数据类型 GlobalAddrType 之外, 而 ShippingAddress 结构将保持与基本数据类型 GlobalAddrType 相同。
之前,使用名为 BillingAddress 的新插入节点。
<xs:schema> <xs:element name="Root"> <xs:complexType> <xs:sequence> <xs:element name="ShippingAddress" type="GlobalAddrType" /> <xs:element name="BillingAddress"> <xs:sequence /> </xs:element> </xs:sequence> </xs:complexType> </xs:element> <xs:complexType name="GlobalAddrType"> [Address structure defined globally here.] </xs:complexType> </xs:schema>
从复杂基类型 GlobalAddrType 派生后,按扩展。
<xs:schema> <xs:element name="Root"> <xs:complexType> <xs:sequence> <xs:element name="ShippingAddress" type="GlobalAddrType" /> <xs:element name="BillingAddress"> <xs:complexType> <xs:complexContent mixed="false"> <xs:extension base="GlobalAddrType" /> </xs:complexContent> </xs:complexType> </xs:element> </xs:sequence> </xs:complexType> </xs:element> <xs:complexType name="GlobalAddrType"> [Address structure defined globally here.] </xs:complexType> </xs:schema>
通过将节点插入架构树中的 BillingAddress 节点来指定基数据类型的扩展。 以下 XSD 片段演示将序列组节点作为 BillingAddress 节点的新子节点插入,然后将名为 PaymentType 的 Field 元素节点作为序列组节点的子节点插入时,空扩展元素如何展开。
<xs:extension base="GlobalAddrType">
<xs:sequence>
<xs:element default="Accounts Payable"
name="Department" type="xs:string" />
</xs:sequence>
</xs:extension>