XSD: уникальные элементы - PullRequest
0 голосов
/ 18 мая 2018

Я хочу, чтобы внутри элемента книги были уникальные ссылки (ниже: author-ref), чтобы выполнялись два условия:

1) Каждая книга может иметь несколько ссылок на автора, но все они уникальны.

2) Две разные книги могут содержать одного и того же автора.

При следующей схеме условие 1) кажется выполненным, а условие 2) - нет.Я получаю следующее сообщение об ошибке:

[Error] library.xml: 15: 41: cvc-identity-constraint.4.1: Дубликат уникального значения [T.Pratchett] объявлен для ограничения идентичности "authorIdUniqueConstraint" элемента "библиотека ".

Почему?

Экземпляр 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>T.Pratchett</author-ref>
    <title>The Colour of Magic</title>
    <year>1983</year>
  </book>

  <book>
    <author-ref>T.Pratchett</author-ref><!--two different refs but same ref as in the first book -> gives error -->
    <author-ref>J.K.Rowling</author-ref>
    <title>Good Omens: The Nice and Accurate Prophecies...</title>
    <language>en</language>
  </book>

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

  <author id="T.Pratchett">
    <last-name>Pratchett</last-name>
    <first-name>Terry</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="yearType">
        <xs:restriction base="xs:int">
            <xs:pattern value="[0-9][0-9][0-9][0-9]"/>
        </xs:restriction>
    </xs:simpleType>

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

    <xs:complexType name="bookType">
        <xs:sequence>
            <xs:element name="author-ref" minOccurs="1" maxOccurs="10" type="xs:string"/>
            <xs:element name="title" type="xs:string"/>
            <xs:element name="language" type="xs:language" 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">
            <xs:selector xpath="./author" />
            <xs:field xpath="@id" />
        </xs:key>
        <xs:keyref name="authorIdRef" refer="authorId">
            <xs:selector xpath="./book/author-ref" />
            <xs:field xpath="." />
        </xs:keyref>
        <xs:unique name="authorIdUniqueConstraint"><!-- problem here i guess -->
            <xs:selector xpath="./book/author-ref" />
            <xs:field xpath="." />
        </xs:unique>
    </xs:element>
</xs:schema>

1 Ответ

0 голосов
/ 19 мая 2018

Проблема в том, что вы определили уникальность на уровне библиотеки, тогда как вы говорите, что на самом деле хотите, чтобы она была на уровне книги.

Общее правило: если вы хотите, чтобы каждый Xв том же Y, чтобы иметь отличное значение для Z, тогда вы должны написать

<xs:element name="Y">
  <xs:unique>
    <xs:selector xpath="X"/>
    <xs:field xpath="Z"/>
...