【发布时间】:2021-04-14 15:37:33
【问题描述】:
对于我应该设计的纸牌游戏,我的任务是实现一个属性,该属性只能具有其他两个属性可以具有的值。我已经问过一个接近这个问题的问题,在那里我能够将该属性限制为与另一个属性具有相同的值。但是,当它的值不同时,编译器会给出错误消息。
背景: 我有元素“card”,它分为元素“type”(也将“color”定义为属性)和“annotation”,它可以有多个属性,例如“function”和“until”(参见示例):
<card>
<type color="black">One</type>
<annotation function="drawcards">1</annotation>
</card>
有问题的属性是属性“until”,它应该只能具有来自属性“color”或“function”的值,这些值由枚举确定并且只能具有某些值:
<xs:simpleType name="color">
<xs:restriction base="xs:string">
<xs:enumeration value="black"/>
<xs:enumeration value="red"/>
<xs:enumeration value="blue"/>
<xs:enumeration value="green"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="function">
<xs:restriction base="xs:string">
<xs:enumeration value="drawcards"/>
<xs:enumeration value="cancel_turn"/>
<xs:enumeration value="reverse"/>
<xs:enumeration value="throwcards"/>
</xs:restriction>
</xs:simpleType>
到目前为止,我能够使用<xs:assert test="if (@until) then (@until = type/@color) or (@until = annotation/@function) else not (@until)"/> 确定属性“直到”
但是 - 这不允许我为属性“直到”赋予另一个值,而不是“颜色”或“功能”的值。所以下面的例子是无效的,即使它应该是:
<card>
<type color="black">Five</type>
<annotation function="reverse" until="red">1</annotation>
</card>
我需要如何在断言中写入才能使“颜色”或“功能”可能具有“直到”的有效值的任何值?
【问题讨论】:
标签: xml xsd attributes restriction