Xml XSD验证子元素的唯一性
我需要使用XSD验证传入的XML到我的系统。下面是一个示例XML和XSDXml XSD验证子元素的唯一性,xml,xsd,Xml,Xsd,我需要使用XSD验证传入的XML到我的系统。下面是一个示例XML和XSD <?xml version="1.0" encoding="utf-8" standalone="no"?> <root> <records> <record> <content>record text</content> <childlist>
<?xml version="1.0" encoding="utf-8" standalone="no"?>
<root>
<records>
<record>
<content>record text</content>
<childlist>
<child>
<chilldref>left_child</chilldref>
<content>child 1 text</content>
</child>
<child>
<chilldref>middle_child</chilldref>
<content>child 2 text</content>
</child>
<child>
<chilldref>right_child</chilldref>
<content>child 3 text</content>
</child>
</childlist>
</record>
</records>
</root>
<?xml version="1.0" encoding="Windows-1252"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="root">
<xs:complexType>
<xs:all>
<xs:element name="records">
<xs:complexType>
<xs:sequence>
<xs:element name="record">
<xs:complexType>
<xs:all minOccurs="0">
<xs:element name="content" type="xs:string" />
<xs:element name="childlist" minOccurs="1" maxOccurs="1">
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs="3" name="child" minOccurs="1">
<xs:complexType>
<xs:all minOccurs="0">
<xs:element name="chilldref" type="childreftype" minOccurs="1" />
<xs:element name="content" type="xs:string" />
</xs:all>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
<xs:unique name="uniqueref">
<xs:selector xpath="child" />
<xs:field xpath="childref" />
</xs:unique>
</xs:element>
</xs:all>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:all>
</xs:complexType>
</xs:element>
<xs:simpleType name="childreftype">
<xs:restriction base="xs:string">
<xs:enumeration value="left_child" />
<xs:enumeration value="right_child" />
<xs:enumeration value="middle_child" />
</xs:restriction>
</xs:simpleType>
</xs:schema>
记录文本
左撇子
儿童1文本
中年儿童
儿童2文本
对孩子
儿童3文本
这里我检查是否有一个“childlist”元素,其中至少有一个“child”元素。对于“child”元素,“childref”属性是必需的,并且应为“childreftype”类型。
现在我需要确保没有两个“child”元素具有相同的“childref”。关于如何使用XSD实现这一点的任何想法
**更新:
将
元素置于“childlist”作用域下后,此项工作正常 对于约束“在X内,不得有两个Y元素具有相同的Z值”,您需要在X的元素声明中使用xs:unique约束:
<xs:unique name="x-y-z">
<xs:selector xpath="Y"/>
<xs:field xpath="Z"/>
</xs:unique>
在您的情况下,Y=“child”和Z=“chilldref”(原文如此),但是X可以是root、records、record或childlist-您没有仔细指定问题,我不知道