XSD: 唯一元素
XSD: unique elements
我想在 book 元素中有唯一的引用(下面:author-ref),以便满足两个条件:
1) 每本书可以有多个作者参考,但它们都是独一无二的。
2) 两本不同的书可以包含相同的作者参考。
对于以下架构,条件 1) 似乎已满足,但条件 2) 未满足。我收到以下错误消息:
[错误] library.xml:15:41: cvc-identity-constraint.4.1: 为元素 [=30] 的身份约束 "authorIdUniqueConstraint" 声明了重复的唯一值 [T.Pratchett] =].
为什么?
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>
问题是您将唯一性定义为图书馆级别,而您说您实际上希望在图书级别。
一般规则是,如果您希望同一 Y 中的每个 X 都具有不同的 Z 值,那么您应该写
<xs:element name="Y">
<xs:unique>
<xs:selector xpath="X"/>
<xs:field xpath="Z"/>
我想在 book 元素中有唯一的引用(下面:author-ref),以便满足两个条件:
1) 每本书可以有多个作者参考,但它们都是独一无二的。
2) 两本不同的书可以包含相同的作者参考。
对于以下架构,条件 1) 似乎已满足,但条件 2) 未满足。我收到以下错误消息:
[错误] library.xml:15:41: cvc-identity-constraint.4.1: 为元素 [=30] 的身份约束 "authorIdUniqueConstraint" 声明了重复的唯一值 [T.Pratchett] =].
为什么?
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>
问题是您将唯一性定义为图书馆级别,而您说您实际上希望在图书级别。
一般规则是,如果您希望同一 Y 中的每个 X 都具有不同的 Z 值,那么您应该写
<xs:element name="Y">
<xs:unique>
<xs:selector xpath="X"/>
<xs:field xpath="Z"/>