C++ XSD:如何制作多态;列表“;?
我正在尝试构建一个元素类型,它保存一个C++ XSD:如何制作多态;列表“;?,c++,xml,xsd,codesynthesis,C++,Xml,Xsd,Codesynthesis,我正在尝试构建一个元素类型,它保存一个change元素类型的列表,该元素类型是其他几个子类型的基本类型。我得到了这个密码: <xs:complexType name="change_list" > <xs:annotation> <xs:documentation>List of changes.</xs:documentation> </xs:annotation> <xs:sequence
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>
更改列表。
通用或特定类型的更改。
将激活元素的更改,或者如果已激活,则不执行任何操作。
将停用图元的更改,或者如果已停用,则不执行任何操作。
如果已停用,将激活该元件的更改;如果已激活,则将其停用。
将修改图元几何状态的更改
通过应用一个或多个几何变换。
IM使用代码合成生成C++代码。
现在,这似乎有些过分,因为这里我们清楚地定义了对不同类型的访问。我想我想要的是更简单的东西,比如:
更改列表。
通用或特定类型的更改。
现在,我不允许对不同的更改子类型使用不同的标记。
所以我想也许一个好的解决办法是使用
但是这样我就失去了使用特定子类型的属性和元素的能力
最初的解决方案可以做到这一点吗(有一个可以获得子类型的基类型对象列表)?您想要的在xml模式中是不可能的。您可以扩展(或限制)已定义的类型,但这是一个新类型。子类型多态性(包含多态性)在xml模式中不存在。听起来您想要的是一个更改列表,但您也想要列表中记录的更改类型(激活、切换等) 我要做的是创建一个名为ChangeType元素的简单元素,该元素将具有一个“type”属性,其数据类型将是另一个元素,ChangeType将是有效更改类型的枚举。例如:
<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>
不知道你是否还需要答案。。。但是下面的模式满足您的需要
首先是基类和两个具体的子类型(确保基类具有abstract=“true”集):
然后添加包含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>
Foo1
食物2
试验
替换组到底有什么问题?在XML模式中建模多形性是一种常见的做法。也许我错了,但使用替换组的AFAIK是一种别名,对吗?然后我可以用它作为“更改”标记的别名,对吗?然后我不能使用更改标记子元素类型特定的元素和属性。我说的对吗?那么,举例来说,没有办法说一个序列可以有一种类型的元素或这种类型的子类型?或者xsd中是否有一个等价物可以帮助我解决我的问题?我已经有了,但这并不能解决问题:如果这样做,就无法访问子元素类型特定的内容。例如,如果只有开关具有“target”元素(甚至属性),则不能在列表中设置它。
<xs:element name="TaskList">
<xs:complexType>
<xs:sequence>
<xs:element name="Tasks" minOccurs="0" maxOccurs="unbounded" type="BaseTask" />
</xs:sequence>
</xs:complexType>
</xs:element>
<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>