我有一个xsd,它必须防止元素的重复值。我尝试了各种方法,并以某种方式缺少实现元素的唯一约束。
在下面的xml中,我有 xyz:interval 元素具有重复值。 如何使用唯一标记避免重复?
我尝试在下面的XSD中使用unique,但是无法实现相同的目标。
XSD:
<?xml version="1.0" encoding="utf-8" ?>
<xs:schema xmlns:xyz="http://www.example.com/schema/public/" attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="http://www.example.com/schema/public/" version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:complexType name="Intervals">
<xs:sequence>
<xs:element name="interval" type="xs:int" maxOccurs="unbounded">
<xs:unique name="alias_unique">
<xs:selector xpath="supportedMeasurementIntervals" />
<xs:field xpath="." />
</xs:unique>
</xs:element>
<xs:element name="defaultInterval" type="xs:int" minOccurs="1" maxOccurs="1" />
</xs:sequence>
</xs:complexType>
<xs:element name="NoOfPeriod" type="xs:int" />
<xs:element name="isPeriodSupported" type="xs:boolean" />
<xs:element name="MType">
<xs:complexType mixed="true">
<xs:sequence minOccurs="0">
<xs:element name="SIntervals" type="xyz:Intervals" minOccurs="0" maxOccurs="1" />
</xs:sequence>
<xs:attribute name="id" type="xs:string" use="required" />
</xs:complexType>
</xs:element>
<xs:group name="Child">
<xs:sequence>
<xs:element name="SIntervals" type="xyz:Intervals" minOccurs="1" maxOccurs="1" />
<xs:element ref="xyz:NoOfPeriod" minOccurs="1" maxOccurs="1" />
<xs:element ref="xyz:isPeriodSupported" minOccurs="1" maxOccurs="1" />
</xs:sequence>
</xs:group>
<xs:element name="Parent">
<xs:complexType>
<xs:sequence>
<xs:element name="Child" minOccurs="1" maxOccurs="1">
<xs:complexType>
<xs:group ref="xyz:Child" />
</xs:complexType>
</xs:element>
<xs:element name="MTypes" minOccurs="1" maxOccurs="1">
<xs:complexType>
<xs:sequence>
<xs:element ref="xyz:MType" maxOccurs="unbounded" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
XML :
<?xml version="1.0" encoding="utf-8"?>
<xyz:Parent xmlns:xyz="http://www.example.com/schema/public/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.example.com/schema/public/">
<xyz:Child>
<xyz:SIntervals>
<xyz:interval>1111</xyz:interval>
<xyz:interval>1111</xyz:interval>
<xyz:interval>1111</xyz:interval>
<xyz:interval>9727</xyz:interval>
<xyz:defaultInterval>504</xyz:defaultInterval>
</xyz:SIntervals>
<xyz:NoOfPeriod>4804</xyz:NoOfPeriod>
<xyz:isPeriodSupported>1</xyz:isPeriodSupported>
</xyz:Child>
<xyz:MTypes>
<xyz:MType id="string" />
<xyz:MType id="string" />
<xyz:MType id="string">
<xyz:SIntervals>
<xyz:interval>2222</xyz:interval>
<xyz:interval>2222</xyz:interval>
<xyz:defaultInterval>6631</xyz:defaultInterval>
</xyz:SIntervals>
</xyz:MType>
<xyz:MType id="string" />
</xyz:MTypes>
</xyz:Parent>
答案 0 :(得分:0)
实际上,您可以更改maxOccurs =&#34; 1&#34;而不是&#34;无界&#34;在您对Intervals
。
[.....]
<xs:complexType name="Intervals">
<xs:sequence>
<xs:element name="interval" type="xs:int" maxOccurs="1">
[.....]
编辑(评论后)
unique
适用于element
的范围。你把它定义得太低了。Child
元素的范围内定义唯一性,如下所示:
<xs:group name="Child">
<xs:sequence>
<xs:element name="SIntervals" type="xyz:Intervals" minOccurs="1" maxOccurs="1" >
<xs:unique name="uniqueInterval">
<xs:selector xpath="xyz:interval" />
<xs:field xpath="." />
</xs:unique>
</xs:element>
<xs:element ref="xyz:NoOfPeriod" minOccurs="1" maxOccurs="1" />
<xs:element ref="xyz:isPeriodSupported" minOccurs="1" maxOccurs="1" />
</xs:sequence>
然后你必须以同样的方式为你的其他元素(或你重新设计定义)这样做。