根据 xsd 1.1 中的属性和元素定义断言 [英] Defining assertions depending on both attributes and elements in xsd 1.1

查看:21
本文介绍了根据 xsd 1.1 中的属性和元素定义断言的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在我的 XSD 架构中定义以下情况.这是我的 XML 示例:

I need to define the following situation in my XSD Schema. This is an example of my XML:

<initialization>

  <stat name="SelfActualization" range="" init="" tickValue="" colorR="" colorG="" colorB=""/>
  <stat name="Social" range="" init="" tickValue="" colorR="" colorG="" colorB=""/>

  <staticAction name="Study" >
    <SelfActualization reqPoints="0" gainedPoints="0" />
    <Social reqPoints="0" gainedPoints="0" />
  </staticAction>

  <staticAction name="Greetings" >
    <SelfActualization reqPoints="0" gainedPoints="0" />
    <Social reqPoints="0" gainedPoints="0" />
  </staticAction>

  <staticAction name="Eat" >
    <SelfActualization reqPoints="0" gainedPoints="0" />
    <Social reqPoints="0" gainedPoints="0" />
  </staticAction>

</initialization>

我需要的第一个断言:我可以根据需要定义任意数量的stat"元素(在本例中只有 2 个),并且我已经设法获得了这种行为.我不知道该怎么做:在我的任何StaticActions"中,我需要再次命名之前定义的所有stats"(作为元素,这是我已经通过 xs:anyType 元素完成的事情,但我需要一个控制名称对应的断言),并且按照它们在开始时定义的相同顺序.正如我们在示例中所看到的,SelfActualization"和Social"确实在我的所有静态操作中都在那里并且以正确的顺序存在.如果之前未定义的另一个stat",或者定义的stats"之一缺失,或者顺序错误,则必须拒绝 XML.到目前为止,我的 XSD 看起来像这样:

1st assertation that I need: I can define as many "stat" elements as I want (in this case only 2) and I already managed to obtain this behavior. What I don't know how to do is: in any of my "StaticActions" I need that all the "stats" previously defined above are named again (as elements, this is something I already do through the xs:anyType element but I need an assertation which controls that the names correspond), AND in the same order they have been defined at the beginning. As we can see in the example indeed both "SelfActualization" and "Social" are there and in the right order in all my StaticActions. If another "stat" not defined before, or if one of the "stats" defined is missing, or if the order is wrong, the XML has to be refused. My XSD looks like this so far:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

<xs:element name="initialization" type="initializationType" />

<xs:complexType name="initializationType">
  <xs:sequence>
    <xs:element  minOccurs="0" maxOccurs="unbounded" ref="stat"/>
    <xs:element  minOccurs="0" maxOccurs="unbounded" ref="staticAction"/>
  </xs:sequence>
</xs:complexType>

<xs:element name="staticAction" type="StaticActionType"/>

<xs:complexType name="StaticActionType">
    <xs:complexContent>
        <!-- Base is anyType -->
        <xs:extension base="xs:anyType">
            <xs:attribute name="name" type="xs:string"/>
            <!-- Check that every "dynamic" child has the two integer attributes and no other attributes -->
            <xs:assert test="every $element in ./* satisfies 
                ($element[
                    matches(@reqPoints, '^[+-]?\d+$') and
                    matches(@gainedPoints, '^[+-]?\d+$') and
                    count(@*=2)])"/>
            <!-- Test that there is no content in staticAction nor in the "dynamic" nodes -->
            <xs:assert test="matches(string(.), '^\s*$')"/>
        </xs:extension>
    </xs:complexContent>
</xs:complexType>

我需要的第二个断言:我使用简单的 xs:sequence 在我的 xsd 模式的开头声明了 StaticActions,因为我正好需要一定数量的静态操作并按特定顺序.在那里,我想确保这些 staticActions 具有正确的名称(通过 staticAction 定义中指定的name"属性):例如,我想要 "Study" "Greetings" 和 "Eat" .所以我需要通过一个简单的断言来检查我的 XML 中的所有学习"、问候"和吃"都在那里.

2nd assertation that I need: I declare the StaticActions at the beginning of my xsd schema by using a simple xs:sequence because I need exactly a certain number of those and in a certain order. There, I want to make sure that these staticActions have proper names (through the "name" attribute specified in the staticAction definition): for example I want "Study" "Greetings" and "Eat" . So I need to check that in my XML all the 3 of them "Study", "Greetings" and "Eat" are there, through a simple assertion.

最后一点是:除了动态元素外,我的 staticAction 还需要包含 2 个静态元素,如下例所示:

Last point is: My staticAction needs to contain 2 static elements as well besides the dynamic ones like shown in the following example:

<initialization>

  <stat name="SelfActualization" range="" init="" tickValue="" colorR="" colorG="" colorB=""/>
  <stat name="Social" range="" init="" tickValue="" colorR="" colorG="" colorB=""/>

  <staticAction name="Study" >
    <SelfActualization reqPoints="0" gainedPoints="0" />
    <Social reqPoints="0" gainedPoints="0" />

    <RequiredAction action="string" cardinality="int" />
    <SuccessLikelihood likelihood="int" />
  </staticAction>

因此,我需要修改StaticAction"的定义并修改一些检查 staticAction 的所有子项的断言.

Therefore I need to modify my definition of the "StaticAction" and modify some of the assertions that checks all of the childs of staticAction.

推荐答案

对于第一个要求,您可以在 initializationType 中使用以下断言:

For the first requeriment you can use the following assertion inside initializationType:

<xs:assert test="every $staticAction in ./staticAction satisfies deep-equal(data($staticAction/*/name()), data(./stat/@name)) "/>

这确保每个 staticAction 包含节点名称与 stat 元素的属性名称相同的子节点.

This ensures that every staticAction contains childs with node names equal to the attribute name of the stat elements in the same order.

对于第二个要求,您可以使用这个简单的断言,假设顺序不重要并且值可以重复:

For the second requirement you can use this simple assertion supposing that order is not important and that values can be repeated:

<xs:assert test="every $staticAction in ./staticAction satisfies $staticAction/@name = ('Study', 'Greetings', 'Eat') "/>

这也可以在 XSD 中建模:

This can also be modelated in XSD:

<xs:attribute name="name">
    <xs:simpleType>
        <xs:restriction base="xs:string">
            <xs:enumeration value="Study"/>
            <xs:enumeration value="Greetings"/>
            <xs:enumeration value="Eat"/>
        </xs:restriction>
    </xs:simpleType>
</xs:attribute>

否则,如果值不能重复但必须出现,您可以使用三个不同的断言来确保每个值都存在一次(例如:count(./staticAction[@name='Eat']) = 1).

Otherwise, if values cannot be repeated but must appear you can use three diferent assertions to ensure that every value exsists one time (e.g: count(./staticAction[@name='Eat']) = 1).

这篇关于根据 xsd 1.1 中的属性和元素定义断言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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