XSD:如何使用'独特'和&带键元素值的'key'/'keyref'?

时间:2011-02-10 15:24:27

标签: xml schema xsd

我尝试将<xs:unique><xs:key>/<xs:keyref>用于元素值,但我无法让它工作。如果我用attrubute值来做它就像魅力一样。

的test.xml

<test:config xmlns:test="http://www.example.org/Test"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.example.org/Test Test.xsd ">

    <test:location id="id1" path="/path2">
        <test:roles>
            <test:role>role1</test:role>
            <test:role>role2</test:role>
            <test:role>role2</test:role> <!-- DUPLICATE: FAIL VALIDATION  -->
        </test:roles>
        <test:action name="action1">
            <test:roles>
                <test:role>role1</test:role>
                <test:role>role1</test:role> <!-- DUPLICATE: FAIL VALIDATION -->
                <test:role>role3</test:role> <!-- NOT DEFINED: FAIL VALIDATION -->
            </test:roles>
        </test:action>
    </test:location>

</test:config>

我希望确保角色只定义一次,而 action 元素下定义的角色只是在上层定义的角色

Test.xsd

<xs:element name="config">
    <xs:complexType>
        <xs:sequence>
            <xs:element ref="test:location" maxOccurs="unbounded" />
        </xs:sequence>
    </xs:complexType>       
</xs:element>

<xs:element name="location" type="test:LocationType">
    <xs:key name="keyRole">
        <xs:selector xpath="test:roles" />
        <xs:field xpath="test:role" />
    </xs:key>
    <xs:keyref name="keyrefRole" refer="test:keyRole">
        <xs:selector xpath="test:action/test:roles" />
        <xs:field xpath="test:role" />
    </xs:keyref>
</xs:element>

<xs:complexType name="LocationType">
    <xs:sequence>
        <xs:element ref="test:roles" minOccurs="0" />
        <xs:element name="action" type="test:ActionType" minOccurs="0" maxOccurs="unbounded"/>
    </xs:sequence>
    <xs:attribute name="id" type="xs:string" use="required"/>
    <xs:attribute name="path" type="xs:string" use="required"/>
</xs:complexType>

<xs:element name="roles" type="test:RolesType">
    <xs:unique name="uniqueRole">
        <xs:selector xpath="." />
        <xs:field xpath="test:role" />
    </xs:unique>
</xs:element>

<xs:complexType name="RolesType">
    <xs:sequence>
        <xs:element name="role" type="xs:string" maxOccurs="unbounded"/>
    </xs:sequence>      
</xs:complexType>

<xs:complexType name="ActionType">
    <xs:sequence>
        <xs:element ref="test:roles" />
    </xs:sequence>
    <xs:attribute name="name" type="xs:string" use="required" />
</xs:complexType>

验证失败并显示以下消息:

Description Resource    Path    Location    Type
cvc-identity-constraint.3: Field "./test:role" of identity constraint "keyrefRole" matches more than one value within the scope of its selector; fields must match unique values.   Test.xml    /filebrowser-ejb/src/test/resources line 15 XML Problem
cvc-identity-constraint.3: Field "./test:role" of identity constraint "keyrefRole" matches more than one value within the scope of its selector; fields must match unique values.   Test.xml    /filebrowser-ejb/src/test/resources line 16 XML Problem
cvc-identity-constraint.3: Field "./test:role" of identity constraint "keyRole" matches more than one value within the scope of its selector; fields must match unique values.  Test.xml    /filebrowser-ejb/src/test/resources line 9  XML Problem
cvc-identity-constraint.3: Field "./test:role" of identity constraint "keyRole" matches more than one value within the scope of its selector; fields must match unique values.  Test.xml    /filebrowser-ejb/src/test/resources line 10 XML Problem
cvc-identity-constraint.3: Field "./test:role" of identity constraint "uniqueRole" matches more than one value within the scope of its selector; fields must match unique values.   Test.xml    /filebrowser-ejb/src/test/resources line 9  XML Problem
cvc-identity-constraint.3: Field "./test:role" of identity constraint "uniqueRole" matches more than one value within the scope of its selector; fields must match unique values.   Test.xml    /filebrowser-ejb/src/test/resources line 10 XML Problem
cvc-identity-constraint.3: Field "./test:role" of identity constraint "uniqueRole" matches more than one value within the scope of its selector; fields must match unique values.   Test.xml    /filebrowser-ejb/src/test/resources line 15 XML Problem
cvc-identity-constraint.3: Field "./test:role" of identity constraint "uniqueRole" matches more than one value within the scope of its selector; fields must match unique values.   Test.xml    /filebrowser-ejb/src/test/resources line 16 XML Problem
cvc-identity-constraint.4.1: Duplicate unique value [role1] declared for identity constraint "uniqueRole" of element "roles".   Test.xml    /filebrowser-ejb/src/test/resources line 9  XML Problem
cvc-identity-constraint.4.1: Duplicate unique value [role1] declared for identity constraint "uniqueRole" of element "roles".   Test.xml    /filebrowser-ejb/src/test/resources line 15 XML Problem
cvc-identity-constraint.4.2.2: Duplicate key value [role1] declared for identity constraint "keyRole" of element "location".    Test.xml    /filebrowser-ejb/src/test/resources line 9  XML Problem
cvc-identity-constraint.4.3: Key 'keyrefRole' with value 'role3' not found for identity constraint of element 'location'.   Test.xml    /filebrowser-ejb/src/test/resources line 19 XML Problem

如果我注释掉应该失败的行,现在验证仍然会失败:

Description Resource    Path    Location    Type
cvc-identity-constraint.3: Field "./test:role" of identity constraint "keyRole" matches more than one value within the scope of its selector; fields must match unique values.  Test.xml    /filebrowser-ejb/src/test/resources line 10 XML Problem
cvc-identity-constraint.3: Field "./test:role" of identity constraint "uniqueRole" matches more than one value within the scope of its selector; fields must match unique values.   Test.xml    /filebrowser-ejb/src/test/resources line 10 XML Problem

我做错了什么?

1 个答案:

答案 0 :(得分:5)

该字段需要标识选择器中的单个项目。你需要更像的东西:

    <xs:selector xpath="test:roles/test:role" />
    <xs:field xpath="." />