我有以下xml。
<Movies>
<Title>
<Platform>Hulu</Platform>
<PlatformID>50019855</PlatformID>
<UnixTimestamp>1431892827</UnixTimestamp>
</Title>
<Title>
<Platform>Hulu</Platform>
<PlatformID>50019855</PlatformID>
<UnixTimestamp>1431892127</UnixTimestamp>
</Title>
</Movies>
然后我有以下xsd来验证上述内容:
<?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"/>
<xs:element name="UnixTimestamp" type="xs:positiveInteger"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
如何添加一个唯一约束,使PlatformID
是唯一的valud,如果有重复值,验证会失败,例如在上面的xml中?
答案 0 :(得分:0)
You need to put the xs:unique
constaint within the Movies
level, not within Title
level. If it is within the Title
level, it will only check for duplicates within that node:
XML
<Movies>
<Title>
<Platform>Hulu</Platform>
<PlatformID>50019855</PlatformID>
<UnixTimestamp>1431892827</UnixTimestamp>
</Title>
<Title>
<Platform>Hulu</Platform>
<PlatformID>50019855</PlatformID>
<UnixTimestamp>1431892127</UnixTimestamp>
</Title>
</Movies>
XSD
<?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/PlatformID"/>
<xs:field xpath="."/>
</xs:unique>
</xs:element>
</xs:schema>