【问题标题】:XSD 1.1: Limit number of overall attributes for an elementXSD 1.1:限制元素的整体属性数量
【发布时间】:2021-07-29 07:27:11
【问题描述】:

我正在开发一款纸牌游戏,它应该显示多种状态并根据条件更改它们。为了实现这一点,我有一个元素 <properties> 作为强制性元素赋予每张卡片。每个属性都可以有一系列属性,如 xsd 示例中所示:

                        <xs:element name="properties" maxOccurs="1">
                            <xs:complexType>
                                <xs:attribute name="type" type="MyAttributeType"/>
                                <xs:attribute name="color" type="color"/>
                                <xs:attribute name="number" type="xs:integer"/>
                            </xs:complexType>
                        </xs:element>

这些properties 不仅显示了每张卡片的属性,还设置了整个游戏场的属性。 现在我的任务是让一张牌可以根据自己的属性改变游戏场的这些属性。为此,我应该实现一个类型connected_condition,它只在某个条件为真时触发。例如:

<connected_condition><properties color='red'/><properties color='green'/></connected_condition/>

例如,当显示具有“红色”属性的卡片时,该条件应该将游戏区域的颜色更改为绿色而不是红色。

但是 - 因为我们在这里处理属性,所以像这样的 connected_condition 可能是有效的,但会违反我必须完成的任务:

<connected_condition><properties color='red' type='heart'/><properties color='green'/></connected_condition/>

无论properties 内部是否有一个或两个元素,在connected_condition 的上下文中,最多两个属性的总数应该是有效的。但同样的属性也应该能够改变(例如红色到绿色),我没有找到一种方法在connected_condition 的一个属性元素中使用 2 倍相同的属性。为了解决这个问题,我正在寻找一种方法来限制可以在元素connected_condition 的上下文中使用的属性总数。最好是一个断言帽说类似:“计算connected_condition内所有子元素的所有属性,如果超过2个则为假”。 此外,这应该不会限制单个元素 properties 中使用的属性数量,该元素提供给每张卡本身。最好的结果是这样的卡片:

XSD:

                    <xs:element name="card">
                        <xs:complexType>
                            <xs:sequence>
                                <xs:element name='name' type='xs:string'/>
                                <xs:element name="properties" type='properties'/>          <!--should have all 3 of its attributes-->
                                <xs:element name='connected_condition' minOccurs='0' maxOccurs='1'>
                                    <xs:complexType>
                                        <xs:sequence>
                                            <xs:element name="properties" type='properties' maxOccurs='2'/>   <!--attributes limited to 2-->
                                        </xs:sequence>
                                    </xs:complexType>
                                </xs:element>
                            </xs:sequence>
                        </xs:complexType>
                    </xs:element>
                    <xs:simpleType name='properties'>
                        <xs:restriction base="xs:string">
                            <xs:attribute name="type" type="MyAttributeType"/>
                            <xs:attribute name="color" type="color"/>
                            <xs:attribute name="number" type="xs:integer"/>
                        </xs:restriction>
                    </xs:simpleType>

XML:

                        <card>
                            <name>Card 1</name>
                            <properties type='heart' color='red' number='7'/>
                            <connected_condition><properties color='red'/><properties color='green'/></connected_condition>
                            <text>Here goes the text</text>
                        </card>

当他们的元素被用作使用断言的子元素时,有没有办法限制属性的总数?

【问题讨论】:

    标签: xml xsd annotations attributes assert


    【解决方案1】:

    我不确定是否限制类型而不是断言形式的断言不是更好

       <xs:element name="card">
            <xs:complexType>
                <xs:sequence>
                    <xs:element name='name' type='xs:string'/>
                    <xs:element name="properties" type='properties'/>          <!--should have all 3 of its attributes-->
                    <xs:element name='connected_condition' minOccurs='0' maxOccurs='1'>
                        <xs:complexType>
                            <xs:sequence>
                                <xs:element name="properties" type='properties' maxOccurs='2'/>   <!--attributes limited to 2-->
                            </xs:sequence>
                        </xs:complexType>
                    </xs:element>
                </xs:sequence>
                <xs:assert test="every $prop in connected_condition/properties satisfies count($prop/@*) le 2"/>
            </xs:complexType>
        </xs:element>
    

    可能会表达您对connected_condition 内每个properties 元素的属性数量的限制。

    您的评论表明您更喜欢&lt;xs:assert test="count(connected_condition//@*) le 2"/&gt;

    【讨论】:

    • 很遗憾,我无法确认。它似乎没有改变任何东西。将其应用于代码然后为每个属性添加超过 1 个属性时,它仍然有效。
    • @DanielJanz,您的描述不清楚,例如,示例说“属性限制为 2”,并且以 count($prop/@*) le 2 的方式发布的代码最多允许两个属性(le 2 是“小于或等于 2")。如果这是您想要的,请将号码更改为 1。我什至不确定您是要检查每个properties 元素还是全部,发布的代码假定您要检查connected_connection 元素的每个properties 子元素的属性数。
    • 啊,不,很抱歉给您带来不便。它是关于在connected_condition 中的所有properties 元素上添加的所有属性的数量。所以无论properties-element只有一个还是两个,connected_condition内使用的属性总数都不能超过两个。
    • 不知道如何处理这个任务?我还是迷路了。
    • &lt;xs:assert test="count(connected_condition//@*) le 2"/&gt; 编辑没有帮助吗?
    猜你喜欢
    • 1970-01-01
    • 2018-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-25
    • 1970-01-01
    • 2011-11-21
    • 1970-01-01
    相关资源
    最近更新 更多