XSD:正确声明键/ keyref约束

时间:2018-05-18 09:01:30

标签: xml xpath xsd constraints

下面的密钥/密钥定义有什么问题?我尝试针对xsd验证xml并获得以下错误消息,我在此上下文中不理解:

[错误] library.xml:7:41:cvc-id.3:身份约束字段' authorIdRef'匹配元素'库',但此元素没有简单类型。 [错误] library.xml:19:11:cvc-identity-constraint.4.3:Key' authorIdRef'有价值的' null'没有找到元素'库的身份约束。 library.xml:127 ms(9个elems,2个attrs,0个空格,117个字符)

xml实例:

<?xml version="1.1" encoding="UTF-8"?>

<library xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
           xsi:noNamespaceSchemaLocation='library.xsd'>
  <book>
    <author-ref>J.K.Rowling</author-ref>
    <title>Harry Potter und der Stein der Weisen</title>
    <language>de</language>
    <year>1998</year>
  </book>

  <author id="J.K.Rowling">
    <last-name>Rowling</last-name>
    <first-name>Joanne K.</first-name>
  </author>

</library>

xsd架构:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xs:schema  xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <!-- definition of simple types -->
    <xs:simpleType name="languageType">
        <xs:restriction base="xs:language"/>
    </xs:simpleType>

    <xs:simpleType name="yearType">
        <xs:restriction base="xs:int">
            <xs:pattern value="[0-9][0-9][0-9][0-9]"/>
        </xs:restriction>
    </xs:simpleType>

    <xs:simpleType name="nameType">
        <xs:restriction base="xs:string"/>
    </xs:simpleType>

    <!-- definition of complex types -->
    <xs:complexType name="authorType">
        <xs:sequence>
            <xs:element name="last-name" type="nameType"/>
            <xs:element name="first-name" type="nameType"/>
        </xs:sequence>
        <xs:attribute name="id" use="required"/>
    </xs:complexType>

    <xs:complexType name="bookType">
        <xs:sequence>
        <xs:element name="author-ref" minOccurs="1" maxOccurs="10"/>
        <xs:element name="title" type="nameType"/>
        <xs:element name="language" type="languageType" minOccurs="0"/>
        <xs:element name="year" type="yearType" minOccurs="0"/>
        </xs:sequence>
    </xs:complexType>

    <!-- definition of root type library -->
    <xs:element name="library">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="book" type="bookType" maxOccurs="unbounded"/>
                <xs:element name="author" type="authorType" maxOccurs="unbounded"/>
            </xs:sequence>
        </xs:complexType>
        <xs:key name="authorId"><!-- something is wrong here-->
            <xs:selector xpath="./author" />
            <xs:field xpath="@id" />
        </xs:key>
        <xs:keyref name="authorIdRef" refer="authorId"><!-- something is wrong here-->
            <xs:selector xpath="./book/author-ref" />
            <xs:field xpath="." />
        </xs:keyref>
    </xs:element>
</xs:schema>

1 个答案:

答案 0 :(得分:0)

Saxon给出了类似的错误消息,但您可能会发现它提供了更多信息:

Validation error on line 6 column 45 of test.xml:
  FORG0001: The value of a field participating in an identity constraint must have a simple
  type, or a complex type with simple content (See
  http://www.w3.org/TR/xmlschema11-1/#cvc-identity-constraint clause 3)

问题是你用:

定义了author-ref
<xs:element name="author-ref" minOccurs="1" maxOccurs="10"/>

因此其类型默认为xs:anyType,这是一种复杂类型。添加type="xs:string",问题就会消失。