XSD:如何制作多态“ldquo; list”rdquo;?

我正在尝试构建一个元素类型,它保留一个
change
元素类型的列表,它是几个其他子类型的基本类型。我得到了这段代码:
<xs:complexType name="change_list" >
    <xs:annotation>
      <xs:documentation>List of changes.</xs:documentation>
    </xs:annotation>

    <xs:sequence>
      <xs:choice minOccurs="1" maxOccurs="unbounded" >

        <xs:element name="change" type="aos:change" >
          <xs:annotation>
            <xs:documentation>Generic or specific type of change.</xs:documentation>
          </xs:annotation>
        </xs:element>

        <xs:element name="activate" type="aos:activate" >
          <xs:annotation>
            <xs:documentation>Change that will activate an element or do nothing if already activated.</xs:documentation>
          </xs:annotation>
        </xs:element>

        <xs:element name="deactivate" type="aos:deactivate" >
          <xs:annotation>
            <xs:documentation>Change that will deactivate an element or do nothing if already deactivated.</xs:documentation>
          </xs:annotation>
        </xs:element>

        <xs:element name="switch" type="aos:switch" >
          <xs:annotation>
            <xs:documentation>Change that will activate the element if deactivated or deactivate it if activated.</xs:documentation>
          </xs:annotation>
        </xs:element>

        <xs:element name="transform" type="aos:transform" >
          <xs:annotation>
            <xs:documentation>
              Change that will modify the geometric state of the element
              by applying one or several geometric transformations.
            </xs:documentation>
          </xs:annotation>
        </xs:element>


      </xs:choice>
    </xs:sequence>
我正在使用CodeSynthesis生成C ++代码。 现在,这似乎有点过头了,因为我们在这里明确定义了对不同类型的访问。我认为我想要的更简单:                    变更清单。         
    <xs:sequence>
      <xs:choice minOccurs="1" maxOccurs="unbounded" >

        <xs:element name="change" type="aos:change" >
          <xs:annotation>
            <xs:documentation>Generic or specific type of change.</xs:documentation>
          </xs:annotation>
        </xs:element>

      </xs:choice>
    </xs:sequence>
现在不允许我为不同的变化子类型使用不同的标签。 所以我想也许一个好的解决方案可能是使用替换组。 但是我会放弃使用特定子类型的属性和元素的能力。 原始解决方案是否很好(具有可以获取子类型的基类型对象列表)?     
已邀请:
不知道你是否还需要答案......但是以下架构可以满足您的需求。 首先是基类型和两个具体的子类型(确保你的基类有abstract =“true”set):
<xs:complexType abstract="true" name="BaseTask">
  <xs:sequence>
    <xs:element name="Name" type="xs:string" />
  </xs:sequence>
</xs:complexType>

<xs:complexType name="ConcreteTask1">
  <xs:complexContent>
    <xs:extension base="BaseTask">

    </xs:extension>
  </xs:complexContent>
</xs:complexType>

<xs:complexType name="ConcreteTask2">
  <xs:complexContent>
    <xs:extension base="BaseTask">
      <xs:sequence>
        <xs:element name="Description" type="xs:string" />
      </xs:sequence>
    </xs:extension>
  </xs:complexContent>
</xs:complexType>
然后添加一个包含BaseTask子类型元素的列表:
<xs:element name="TaskList">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="Tasks" minOccurs="0" maxOccurs="unbounded" type="BaseTask" />
    </xs:sequence>
  </xs:complexType>
</xs:element>
然后xml看起来像这样:
<TaskList>
  <Tasks xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ConcreteTask1">
    <Name>Foo1</Name>
  </Tasks>
  <Tasks xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ConcreteTask2">
    <Name>Foo2</Name>
    <Description>Test</Description>
  </Tasks>
</TaskList>
    
你想要的是xml-schema中不可能的。您可以扩展(或限制)已定义的类型,但这是一种新类型。 xml-schema中不存在子类型多态(包含多态)。     
听起来你想要的是一个变化列表,但你也想要列表中记录的变化类型(激活,切换等)。 我要做的是创建一个名为ChangeType元素的简单元素,它具有“type”属性,其数据类型将是另一个元素,ChangeTypeType将是有效更改类型的枚举。例如:
<element name="ChangeList" type="ChangeListType"/>    
<complexType name="ChangeListType">
    <annotation>
        <documentation>
            A list of changes
        </documentation>
    </annotation>
    <sequence>
        <element name="change" type="ChangeType" minOccurs="1" maxOccurs="unbounded">
            <annotation>
                <documentation>
                    A change event
                </documentation>
            </annotation>
        </element>
    </sequence>
</complexType>

<complexType name="ChangeType">
    <annotation>
        <documentation>
            A change unit
        </documentation>
    </annotation>
    <attribute ref="typeOfChange" use="required"/>
</complexType>

<attribute name="typeOfChange" type="ChangeTypeType">
    <annotation>
        <documentation>
            The kind of change
        </documentation>
    </annotation>
</attribute>

<simpleType name="ChangeTypeType">
    <annotation>
        <documentation>
            Describes the types of allowed changes
        </documentation>
    </annotation>
    <restriction base="token">
        <enumeration value="activate"/>
        <enumeration value="activate"/>
                    <enumeration value="switch"/>
                    <enumeration value="transform"/>
    </restriction>
</simpleType>
这将为您提供一个XML文档,如:
<ChangeList>
    <change typeOfChange="activate/>
    <change typeOfChange="switch/>
    <change typeOfChange="transform/>
</ChangeList>
    

要回复问题请先登录注册