XSD 1.1:属性应该仅具有另一个属性的值 [英] XSD 1.1: Attribute is supposed to only have values of another attribute

查看:53
本文介绍了XSD 1.1:属性应该仅具有另一个属性的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于我应该设计的纸牌游戏,我的任务是实现一个属性,该属性只能具有其他两个属性可以具有的值.我已经问过一个与此问题很接近的问题,我可以在其中限制该属性与另一个属性具有相同的值.但是,当它的值不相同时,编译器将给出错误消息.

For a card-game that I am supposed to design, I have the task of implementing an attribute that can only have values that two other attributes can have. I already asked a question close to this one, where I was able to restrict that attribute to have the same value as another attribute. However, when it has not the same value, the compiler is giving an error message.

背景:我有元素卡"分为元素类型"(还定义了颜色"作为属性)和注释"可以具有几个属性,例如功能".和直到"(请参见示例):

Background: I have the element "card" which is divided into the element "type" (which also defines a "color" as an attribute) and "annotation" which can have several attributes, for example "function" and "until" (see the example):

<card>
   <type color="black">One</type>
   <annotation function="drawcards">1</annotation>
</card> 

所讨论的属性是"until"属性.它应该只能具有来自属性颜色"的值.或函数",由枚举确定,并且只能具有某些值:

The attribute in question is the attribute "until" that should only be able to have values from the attributes "color" or "function", which are determined by enumerations and can only have certain values:

    <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 =如果(@until)则(@until =类型/@ color)或(@until =批注/@ function)否则(@until)"/>但是,这不允许我为属性直到"赋予另一个值.比颜色"的值或功能".因此,以下示例即使有效,也无效:

So far I was able to determine the attribute "until" by using <xs:assert test="if (@until) then (@until = type/@color) or (@until = annotation/@function) else not (@until)"/> However - this doesn't allow me to give another value to the attribute "until" than the value of "color" or "function". So the following example is not valid even though it should:

    <card>
        <type color="black">Five</type>
        <annotation function="reverse" until="red">1</annotation>
    </card> 

我需要如何在断言中进行写操作,以使任何颜色"值变为0.或功能"可能还具有直到"的有效值?

How do I need to write in the assert in order to make any value that "color" or "function" could have also a valid value for "until"?

推荐答案

好吧,一种方法是列出允许的值:

Well, one way to do this would be to list the allowed values:

 <xs:simpleType name="until">
    <xs:restriction base="xs:string">
        <xs:enumeration value="black"/>
        <xs:enumeration value="red"/>
        <xs:enumeration value="blue"/>
        <xs:enumeration value="green"/>
        <xs:enumeration value="drawcards"/>
        <xs:enumeration value="cancel_turn"/>
        <xs:enumeration value="reverse"/>
        <xs:enumeration value="throwcards"/>
    </xs:restriction>
</xs:simpleType>

但是,当然,您要避免重复列表,因此,您真正想要做的是定义一个允许其他两种类型允许的任何类型的类型.您可以通过联合来做到这一点:

But of course, you want to avoid duplicating the lists, so what you really want to do is to define a type that allows anything that the two other types allow. You can do this with a union:

 <xs:simpleType name="until">
    <xs:union memberTypes="color function"/>
 </xs:simpleType>

这不需要XSD 1.1,也不需要断言,因为允许 until 的值(如果我理解正确的话)不取决于在其他地方看到的内容实例,它们仅取决于架构中的内容.

This doesn't need XSD 1.1 and it doesn't need an assertion, because the allowed values for until (if I've understood you correctly) don't depend on what's seen elsewhere in the instance, they only depend on what's in the schema.

这篇关于XSD 1.1:属性应该仅具有另一个属性的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆