Xml 在文档级别设置XSD重复约束
我有以下XML:Xml 在文档级别设置XSD重复约束,xml,xsd,xml-validation,Xml,Xsd,Xml Validation,我有以下XML: <Movies> <Title> <Platform>Hulu</Platform> <PlatformID>50019855</PlatformID> <!-- Reject xml based on duplicate PlatformID --> <UnixTimestamp>1431892827</UnixTime
<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>
基于上面重复的
PlatformID
来拒绝它,这里正确的XSD是什么?我目前使用的XSD有什么问题?我认为问题在于我试图在Title
节点中创建一个唯一的约束,而我需要在Movies/Title/PlatformID
级别创建一个文档范围的约束。您已经接近了。只需将xs:unique
向上移动,使其成为根Movies
元素定义的一部分,因为唯一性的范围将覆盖整个文档(即根元素内):
然后,您将得到一个错误,如以下关于PlatformID
的重复值的错误:
[错误]try.xml:11:42:cvc标识约束。4.1:Duplicate unique
为标识约束“uniquePlatformID”声明的值[50019855]
电影元素
<?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>
<?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>