[英]Specify unique element in xml
有人可以找出我的架构有什么问题吗,因为我可以在架构中添加重复的PersonID:
<xs:element name="Persons" minOccurs="1" maxOccurs="1">
<xs:complexType>
<xs:sequence>
<xs:element name="Person" minOccurs="1" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="PersonID" minOccurs="1" maxOccurs="1">
<xs:simpleType>
<xs:restriction base="xs:int">
<xs:minExclusive value="0"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="JoinedDate" type="xs:date" minOccurs="1" maxOccurs="1"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
<xs:unique name="UniquePeronID">
<xs:selector xpath="Person" />
<xs:field xpath="@PersonID" />
</xs:unique>
</xs:element>
和我的XML类似:
<Persons>
<Person>
<PersonID>69674</PersonID>
<JoinedDate>2006-08-25</JoinedDate>
</Person>
<Person>
<PersonID>69674</PersonID>
<JoinedDate>2006-08-25</JoinedDate>
</Person>
</Persons>
我阅读了以下参考文献,但没有帮助我: 如何确保XML模式中的唯一元素值?
https://msdn.microsoft.com/zh-CN/library/ms256146(v=vs.110).aspx
http://support.liquid-technologies.com/KB/a79/creating-a-unique-constrant-with-an-xsd.aspx
我注意到他们在xpath中有“ mstns:”,但在我的情况下,我的架构没有它,例如
<xs:field xpath="@PersonID" />
这是指定一个PersonID
属性( @
含义),因此您的架构正在寻找重复的属性。 您需要重新措辞以引用子元素。
尝试以下方法:
<xs:field xpath="PersonID" />
然后,您的xs:unique
约束将按预期工作。
小注释:如果Persons
成为XSD中的顶级元素,请删除minOccurs
和maxOccurs
因为它们不允许出现在顶级元素定义中。
另请参见XML模式中的唯一约束
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.