在 xsd 1.1 中的 xs:anyTipe 元素上定义属性断言

Defining attribute assertions on xs:anyTipe elements in xsd 1.1

我必须首先定义一个任意类型元素的序列。我的情况如下:

<staticAction name="Jump" >
    <"anyElementName" reqPoints="" gainedPoints="" />
    <"anyElementName" reqPoints="" gainedPoints="" />
    <"anyElementName" reqPoints="" gainedPoints="" />
    ...
</staticAction>

所以,我的问题是:如何定义具有 "dynamic" 名称但具有固定属性(reqPoints 和 gainedPoints)的无限元素序列?两个属性都是xs:integer。我考虑过通过断言添加属性,但我仍然不知道该怎么做。提前致谢。

通常 "dynamic" 名称可以替换,因此可以使用 XSD 对它们进行建模。示例:

来自

<places>
    <country value="x"/>
    <city value="y"/>
    <town value="z"/>
</places>

<places>
    <place type="country" value="x"/>
    <place type="city" value="y"/>
    <place type="town" value="z"/>
</places>

在这个新案例中,只有属性值是 "dynamic"。这适合使用 XSD 进行建模,这比 XPath 断言更容易且更具表现力。

但是,如果您确实需要验证具有已知属性和内容类型的 "dynamic" 名称,您可以查看带有断言的示例模式(在模式中进行了解释) :

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"
    vc:minVersion="1.1" xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning">
    <xs:element name="staticAction">
        <xs:complexType>
            <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:element>
</xs:schema>

请注意,通过扩展 xs:anyType 并使用该断言,您将获得具有任意名称和这两个属性的无限元素序列。