【问题标题】:XSD: unique elementsXSD:独特的元素
【发布时间】:2018-10-28 19:53:53
【问题描述】:

我想在 book 元素中拥有唯一的引用(如下:author-ref),以便满足两个条件:

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

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

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

[Error] library.xml:15:41: cvc-identity-constraint.4.1: Duplicate unique value [T.Pratchett] 为元素“library”的身份约束“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>

【问题讨论】:

    标签: xml xsd constraints unique unique-constraint


    【解决方案1】:

    问题是您已将唯一性定义为在图书馆级别,而您说您实际上希望在图书级别。

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多