选择替换类型

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 的派生类型被命名为 circlerectangle,如以下示例 XML 模式中所示:
当 XML 模式根据派生类型定义 XML 元素时,您必须指定在运行时 XML 元素的期望派生类型(或替换类型)。对于以上显示的示例 XML 模式,您必须指定 <shape> 元素的类型:circlerectangle。 在 Studio 中,您可使用将节点替换为... 菜单选项来指定 XML 元素的期望替换类型:
  • 对于基于替换类型的变量 - 通过以下方式来选择 XML 元素的期望类型:在变量选项卡的模式窗格中右键单击节点,然后从菜单中选择将节点替换为... 选项。有关更多信息,请参阅选择变量的替换类型
  • 对于映射窗格中基于替换类型的节点 - 通过以下方式来选择 XML 元素的期望类型:在变量选项卡的模式窗格中右键单击节点,然后从菜单中选择将节点替换为...选项,如以下过程所述。 

在以上显示的示例 XML 模式中,名为 <shapeType> 的基本类型被定义为抽象。不能在 XML 实例文档中使用抽象基本类型来定义元素类型 - 必须指定某一派生类型。在此示例中,无法将 XML 实例文档中的 <shape> 元素声明为 shapeType,必须将其声明为派生类型之一:circlerectangle

在以上示例中,基本类型定义为抽象,但是也支持基于非抽象基本类型的派生类型的类型替换。要使名为 shapeType 的 complexType 在以上所示的示例 XML 模式中不处于抽象状态,请除去 abstract=true 属性,如以下 XML 模式片段所示:
<xsd:complexType name="shapeType">
  <xsd:sequence>
    <xsd:element name="color" type="xsd:string"/>
  </xsd:sequence>
 </xsd:complexType>
WSDL 是用于定义 Web service 的特定 XML 模式。还支持将类型替换用于 WSDL。

您可以为重现目标节点的扩展出现指定替换类型。仅对重现目标节点支持扩展出现的此类型替换。目标节点位于映射窗格右侧。

注意: 复杂类型的类型替换在活动的映射窗格中受支持,对于变量也受支持。但是,使用替换组来按扩展名派生元素在活动的映射窗格中不受支持,对于变量也不受支持。有关更多信息,请参阅不受支持的 XML 模式特征

示例 XML 模式

此主题中使用的示例 XML 模式仅供参考:
<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>