Q:

How to add a unique id to a child element in an XSD file?

I cannot figure out how to create my XSD file to have a unique id to the child element. It works fine for the root element, but copying this code and changing the values does not work, so I assume I am doing something wrong. Here is my XML:
<?xml version="1.0" encoding="UTF-8"?>
<flights
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="test.xsd">

    <flight flightid="1">
        <plane planeid="1">Airbus A380-861</plane>
    </flight>

</flights>

Here is the XSD file:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" 
    elementFormDefault="qualified" 
    attributeFormDefault="unqualified">

    <xs:element name="flights">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="flight" 
                            type="flighttype" 
                            minOccurs='1' 
                            maxOccurs='unbounded' />
            </xs:sequence>
        </xs:complexType>
        <xs:unique name="flightidisunique">
            <xs:selector xpath="flight"/>
            <xs:field xpath="@flightid"/>
        </xs:unique>
    </xs:element>

    <xs:complexType name="flighttype">
        <xs:sequence>
            <xs:element name="plane" 
                        type="planetype" 
                        minOccurs='1' 
                        maxOccurs='unbounded' />
        </xs:sequence>
        <xs:unique name="planeidisunique">
            <xs:selector xpath="plane"/>
            <xs:field xpath="@planeid"/>
        </xs:unique>
        <xs:attribute name="flightid" type="xs:string"/>
    </xs:complexType>

    <xs:complexType name="planetype">
        <xs:sequence>
        </xs:sequence>
        <xs:attribute name="planeid" type="xs:string"/>
    </xs:complexType>

</xs:schema>

What is the correct way to create a valid Schema for the XML I have posted?

A:

The following schema makes sure that flightid is unique amongst all flight elements and that planeid is unique amongst all plane elements within the same flight element:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
    elementFormDefault="qualified"
    attributeFormDefault="unqualified">

  <xs:element name="flights">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="flight" type="flighttype" minOccurs='1' maxOccurs='unbounded'>
          <xs:unique name="planeidisunique">
            <xs:selector xpath="plane"/>
            <xs:field xpath="@planeid"/>
          </xs:unique>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
    <xs:unique name="flightidisunique">
      <xs:selector xpath="flight"/>
      <xs:field xpath="@flightid"/>
    </xs:unique>
  </xs:element>

  <xs:complexType name="flighttype">
    <xs:sequence>
      <xs:element name="plane" type="planetype" minOccurs='1' maxOccurs='unbounded' />
    </xs:sequence>
    <xs:attribute name="flightid" type="xs:string"/>
  </xs:complexType>

  <xs:complexType name="planetype" mixed="true">
    <xs:sequence>
    </xs:sequence>
    <xs:attribute name="planeid" type="xs:string"/>
  </xs:complexType>

</xs:schema>

This variation of the schema makes planeid unique amongst ALL plane elements:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
    elementFormDefault="qualified"
    attributeFormDefault="unqualified">

  <xs:element name="flights">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="flight" type="flighttype" minOccurs='1' maxOccurs='unbounded'>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
    <xs:unique name="flightidisunique">
      <xs:selector xpath="flight"/>
      <xs:field xpath="@flightid"/>
    </xs:unique>
    <xs:unique name="planeidisunique">
      <xs:selector xpath="flight/plane"/>
      <xs:field xpath="@planeid"/>
    </xs:unique>
  </xs:element>

  <xs:complexType name="flighttype">
    <xs:sequence>
      <xs:element name="plane" type="planetype" minOccurs='1' maxOccurs='unbounded' />
    </xs:sequence>
    <xs:attribute name="flightid" type="xs:string"/>
  </xs:complexType>

  <xs:complexType name="planetype" mixed="true">
    <xs:sequence>
    </xs:sequence>
    <xs:attribute name="planeid" type="xs:string"/>
  </xs:complexType>

</xs:schema>

