Thudani Hettimulla
Thudani Hettimulla

Reputation: 774

XSD validate uniquness of child elments

I need to validate incoming XMLs to my system using an XSD. Below is a sample XML and the 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>

Here I am checking whether there is a single 'childlist' element with at least one 'child' element. For 'child' elements 'childref' attribute is compulsory and it should be of the type 'childreftype'. Now I need to make sure that there are no two 'child' elements with the same 'childref'. Any idea on how to achieve this with XSD.

**UPDATE: This works after putting <xs:unique> element under 'childlist' scope.

Upvotes: 0

Views: 43

Answers (1)

Michael Kay
Michael Kay

Reputation: 163322

For the constraint "Within an X, there must be no two Y elements having the same value of Z", you need an xs:unique constraint in the element declaration for X:

<xs:unique name="x-y-z">
  <xs:selector xpath="Y"/>
  <xs:field xpath="Z"/>
</xs:unique>

In your case Y="child" and Z="chilldref" (sic), but X could be either root, records, record, or childlist - you haven't specified the problem carefully enough for me to know.

Upvotes: 2

Related Questions