在文档级别设置 XSD 重复约束 [英] Set XSD duplicate constraint at document-level
问题描述
我有以下 XML:
<Movies>
<Title>
<Platform>Hulu</Platform>
<PlatformID>50019855</PlatformID> <!-- Reject xml based on duplicate PlatformID -->
<UnixTimestamp>1431892827</UnixTimestamp>
</Title>
<Title>
<Platform>Hulu</Platform>
<PlatformID>50019855</PlatformID> <!-- Reject xml based on duplicate PlatformID -->
<UnixTimestamp>1431892127</UnixTimestamp>
</Title>
</Movies>
以下 XSD 验证,但我希望它拒绝它,因为它有重复的 PlatformID
.
The following XSD validates, however I want it to reject it because it has a duplicate PlatformID
.
<?xml version="1.0" encoding="UTF-8" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Movies">
<xs:complexType>
<xs:sequence>
<xs:element name="Title" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="Platform" type="xs:string"/>
<xs:element name="PlatformID" type="xs:string" maxOccurs="unbounded"/>
<xs:element name="UnixTimestamp" type="xs:positiveInteger"/>
</xs:sequence>
</xs:complexType>
<xs:unique name="uniquePlatformID">
<xs:selector xpath=".//Title/PlatformID"/>
<xs:field xpath="."/>
</xs:unique>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
这里正确的 XSD 是什么,根据上面重复的 PlatformID
拒绝它?我目前使用的 XSD 有什么问题?我认为问题在于我试图在 Title
节点中创建一个唯一约束,而我需要在 Movies/Title/PlatformID
处创建整个文档水平.
What would be the correct XSD here, to reject it based on the duplicate PlatformID
above? What is wrong with the XSD I'm currently using? I believe the issue is that I'm trying to create a unique constraint within the Title
node, whereas I need to create it document-wide at the Movies/Title/PlatformID
level.
推荐答案
你已经接近了.只需将 xs:unique
向上移动为根 Movies
元素定义的一部分,因为唯一性的范围旨在跨越整个文档(在根元素内,即):
You were close. Just move the xs:unique
up to be part of the definition of the root Movies
element since the scope of uniqueness is intended to be across the entire document (within the root element, that is):
<?xml version="1.0" encoding="UTF-8" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Movies">
<xs:complexType>
<xs:sequence>
<xs:element name="Title" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="Platform" type="xs:string"/>
<xs:element name="PlatformID" type="xs:string" maxOccurs="unbounded"/>
<xs:element name="UnixTimestamp" type="xs:positiveInteger"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
<xs:unique name="uniquePlatformID">
<xs:selector xpath="Title"/>
<xs:field xpath="PlatformID"/>
</xs:unique>
</xs:element>
</xs:schema>
然后你会得到一个关于PlatformID
重复值的错误:
Then you'll get an error such as the following about duplicate values for PlatformID
:
[错误] try.xml:11:42: cvc-identity-constraint.4.1: Duplicate unique为身份约束uniquePlatformID"声明的值 [50019855]元素电影".
[Error] try.xml:11:42: cvc-identity-constraint.4.1: Duplicate unique value [50019855] declared for identity constraint "uniquePlatformID" of element "Movies".
这篇关于在文档级别设置 XSD 重复约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!