XSD 验证子元素的唯一性
XSD validate uniquness of child elments
我需要使用 XSD 验证传入的 XML 到我的系统。下面是示例 XML 和 XSD.
<?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>
我在这里检查是否有一个 'childlist' 元素至少有一个 'child' 元素。对于 'child' 个元素,'childref' 属性是强制性的,它应该是 'childreftype' 类型。
现在我需要确保没有两个 'child' 元素具有相同的 'childref'。关于如何使用 XSD.
实现此目的的任何想法
**更新:
这在将 <xs:unique>
元素置于 'childlist' 范围后起作用。
对于约束 "Within an X, there must be no two Y elements having the same value of 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 可能是根、记录、记录或子列表 - 您没有足够仔细地指定问题让我知道。
我需要使用 XSD 验证传入的 XML 到我的系统。下面是示例 XML 和 XSD.
<?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>
我在这里检查是否有一个 'childlist' 元素至少有一个 'child' 元素。对于 'child' 个元素,'childref' 属性是强制性的,它应该是 'childreftype' 类型。 现在我需要确保没有两个 'child' 元素具有相同的 'childref'。关于如何使用 XSD.
实现此目的的任何想法**更新:
这在将 <xs:unique>
元素置于 'childlist' 范围后起作用。
对于约束 "Within an X, there must be no two Y elements having the same value of 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 可能是根、记录、记录或子列表 - 您没有足够仔细地指定问题让我知道。