XSD:如何制作多态“列表”?

发布于 2024-10-03 22:05:44 字数 2710 浏览 7 评论 0原文

我正在尝试构建一个元素类型,该元素类型保留 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>

现在,这不允许我为不同的更改子类型使用不同的标签。 所以我想也许一个好的解决方案可能是使用 替换组

但这样我就会失去使用特定子类型的属性和元素的能力。

原始解决方案是否适合这样做(拥有也可以获取子类型的基类型对象列表)?

I'm trying to build an element type that keep a list of change element type that is the base type of several other child type. I got this code :

<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>

Im' using CodeSynthesis to generate C++ code.

Now, that seems overkill because here we clearly define access to different types. I think what I want is something simpler like :

List of changes.

    <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>

Now that don't allow me to have different tags for different subtypes of changes.
So I thought maybe a good solution might be to use substitution group.

But then I would loose the ability to use the specific sub-type's attributes and elements.

Is the original solution good to do that (having a list of base type object that can get child types too)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

我也只是我 2024-10-10 22:05:44

不知道您是否仍然需要答案...但是以下架构可以满足您的需要。

首先是基本类型和两个具体子类型(确保您的基类设置有abstract=“true”):

<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>

Dont know if you still need an answer... But the following schema does what you need.

First of all the base type and two concrete subtypes (make sure, that your base class has 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>

Then adding a list that holds elements that are subtypes of 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>

The xml then looks like this:

<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>
凉城 2024-10-10 22:05:44

你想要的在 xml 模式中是不可能的。您可以扩展(或限制)已定义的类型,但这是一个新类型。 xml 模式中不存在子类型多态性(包含多态性)。

What you want is not possible in xml-schema. You can extend (or restrict) a defined type but then this is a new type. Subtyping polymorphism (inclusion polymorphism) doesn't exist in xml-schema.

森罗 2024-10-10 22:05:44

听起来您想要的是更改列表,但您还希望列表中记录更改的类型(激活、切换等)。

我要做的是创建一个名为 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>

It sounds like what you want is a list of changes, but you also want the type of change recorded in the list (activate, switch, etc).

What I would do is make a simple element called ChangeType element that would have a "type" attribute whose data type would be another element, ChangeTypeType which would be an enum of your valid changed types. For example:

<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>

This would give you an XML document like:

<ChangeList>
    <change typeOfChange="activate/>
    <change typeOfChange="switch/>
    <change typeOfChange="transform/>
</ChangeList>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文