flyingaxel
flyingaxel

Reputation: 351

XSD: declaring key/keyref constraint correctly

What is wrong with the key/keyref definition below? I tried to validate the xml against the xsd and got the following error messages which i do not understand in this context:

[Error] library.xml:7:41: cvc-id.3: A field of identity constraint 'authorIdRef' matched element 'library', but this element does not have a simple type. [Error] library.xml:19:11: cvc-identity-constraint.4.3: Key 'authorIdRef' with value 'null' not found for identity constraint of element 'library'. library.xml: 127 ms (9 elems, 2 attrs, 0 spaces, 117 chars)

The xml instance:

<?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>

The xsd schema:

<?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>

Upvotes: 1

Views: 871

Answers (1)

Michael Kay
Michael Kay

Reputation: 163468

Saxon gives a similar error message, though you might find it slightly more informative:

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)

The problem is that you defined author-ref with:

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

So its type defaults to xs:anyType, which is a complex type. Add type="xs:string" and the problem goes away.

Upvotes: 1

Related Questions