XSD: правильное объявление ограничения ключ / ключ - PullRequest
0 голосов
/ 18 мая 2018

Что не так с определением ключа / keyref ниже?Я попытался проверить xml на xsd и получил следующие сообщения об ошибках, которые я не понимаю в этом контексте:

[Ошибка] library.xml: 7: 41: cvc-id.3: полеограничение идентичности «authorIdRef» соответствует элементу «библиотека», но этот элемент не имеет простого типа.[Ошибка] library.xml: 19: 11: cvc-identity-constraint.4.3: Ключ 'authorIdRef' со значением 'null' не найден для ограничения идентичности элемента 'library'.library.xml: 127 мс (9 элементов, 2 атрибута, 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 голосов
/ 18 мая 2018

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" и проблема исчезнет.

...