关于xml:XSD:正确声明键/键引用约束

XSD: declaring key/keyref constraint correctly

下面的key / keyref定义有什么问题?我尝试针对xsd验证xml,并收到以下在这种情况下我不理解的错误消息:

[错误] library.xml:7:41:cvc-id.3:标识约束\\'authorIdRef \\'的字段与元素\\'library \\'相匹配,但是此元素没有简单的类型。
[错误] library.xml:19:11:cvc-identity-constraint.4.3:找不到元素\\'library \\'的身份约束键\\'authorIdRef \\',其值为\\'null \\'。
library.xml:127毫秒(9个元素,2个属性,0个空格,117个字符)

xml实例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?xml version="1.1" encoding="UTF-8"?>

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

 
    <last-name>Rowling</last-name>
    <first-name>Joanne K.</first-name>
  </author>

</library>

xsd模式:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<?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>

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

1
2
3
4
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:

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

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