<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">

  <xsd:simpleType name="codeType">
    <xsd:restriction base="xsd:string">
      <xsd:pattern value="[A-Z]+"/>
    </xsd:restriction>
  </xsd:simpleType>

  <xsd:element name="Test">
    <xsd:complexType>
      <xsd:sequence>
        <xsd:element ref="Country"
                     maxOccurs="unbounded"/>
      </xsd:sequence>
    </xsd:complexType>
  </xsd:element>

  <xsd:element name="Country">
    <xsd:complexType>
      <xsd:sequence>
        <xsd:element ref="City"
                     maxOccurs="unbounded"/>
      </xsd:sequence>
      <xsd:attribute name="code"
                     type="codeType"
                     use="required"/>
    </xsd:complexType>
    <xsd:key name="CountryDummyKey">
      <xsd:selector xpath="."/>
      <xsd:field xpath="@code"/>
    </xsd:key>
    <xsd:keyref name="PartRefCountryDummy"
      refer="CountryDummyKey">
      <xsd:selector xpath=".//Part"/>
      <xsd:field xpath="@country"/>
    </xsd:keyref>
  </xsd:element>

  <xsd:element name="City">
    <xsd:complexType>
      <xsd:sequence>
        <xsd:element ref="Part"
                     minOccurs="2"
                     maxOccurs="2"/>
      </xsd:sequence>
      <xsd:attribute name="name"
                     type="xsd:string"
                     use="required"/>
      <xsd:attribute name="code"
                     type="xsd:string"
                     use="required"/>
    </xsd:complexType>
  </xsd:element>

  <xsd:element name="Part">
    <xsd:complexType>
      <xsd:attribute name="country"
                    type="codeType"
                    use="required"/>
    </xsd:complexType>
  </xsd:element>
</xsd:schema>