XSD:如何制作多态“列表”?
我正在尝试构建一个元素类型,该元素类型保留 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不知道您是否仍然需要答案...但是以下架构可以满足您的需要。
首先是基本类型和两个具体子类型(确保您的基类设置有abstract=“true”):
然后添加一个包含 BaseTask 子类型元素的列表:
xml 如下所示:
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):
Then adding a list that holds elements that are subtypes of BaseTask:
The xml then looks like this:
你想要的在 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.
听起来您想要的是更改列表,但您还希望列表中记录更改的类型(激活、切换等)。
我要做的是创建一个名为 ChangeType 元素的简单元素,该元素将具有一个“type”属性,其数据类型将是另一个元素 ChangeTypeType,它将是有效更改类型的枚举。例如:
这将为您提供一个 XML 文档,如下所示:
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:
This would give you an XML document like: