XSD:独特的元素

时间:2018-05-18 11:20:59

标签: xml xsd constraints unique unique-constraint

我希望在book元素中包含唯一引用(如下所示:author-ref),以便满足这两个条件:

1)每本书都可以有多个作者参考,但它们都是唯一的。

2)两本不同的书可以包含相同的作者-ref。

使用以下模式,条件1)似乎已满足,但条件2)没有。我收到以下错误消息:

[错误] 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 :(得分:0)

问题在于你已经将唯一性定义为库的级别,而你说你实际上想要它在书的级别。

一般规则是,如果你希望同一个Y中的每个X都有一个不同的Z值,那么你应该写

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