title: How to use unique and keyref properly in XML schema?
body: <p>I have this XML schema but I don't know how to complete it in order to achieve what I need. I searched a lot online about unique and keyref usage, but all I can find are basic examples.</p>
  
  <p>This is my schema:  </p>
  
  <pre><code>    &lt;xs:element name="access" type="myaccess" /&gt;
  
      &lt;xs:complexType name="myaccess"&gt;
          &lt;xs:sequence&gt;
              &lt;xs:element name="user" type="myuser" minOccurs="0" maxOccurs="unbounded"&gt;
                  &lt;xs:unique name="u_idunique"&gt;
                      &lt;xs:selector xpath="user" /&gt;
                      &lt;xs:field xpath="@id" /&gt;
                  &lt;/xs:unique&gt;
              &lt;/xs:element&gt;
              &lt;xs:element name="authorization" type="myauthorization" minOccurs="0" maxOccurs="unbounded"&gt;
  
              &lt;!-- HERE I WANT A KEYREF TO id attribute of user element --&gt;
              &lt;!-- HERE I WANT A KEYREF TO id attribute of building element OR door element --&gt;
  
              &lt;/xs:element&gt;
              &lt;xs:element name="building" type="mybuilding" minOccurs="0" maxOccurs="unbounded" &gt;
                  &lt;!-- I DON'T KNOW HOW TO SPECIFY THAT id of building, id of door and id of gate are in the same scope --&gt;
                  &lt;xs:unique name="b_idunique"&gt;
                      &lt;xs:selector xpath="building" /&gt;
                      &lt;xs:field xpath="@id" /&gt;
                  &lt;/xs:unique&gt;
              &lt;/xs:element&gt;
          &lt;/xs:sequence&gt;
      &lt;/xs:complexType&gt;
  
      &lt;xs:complexType name="myuser"&gt;
          &lt;xs:attribute name="id" type="my_id" use="required" /&gt;
          &lt;xs:attribute name="name" type="xs:string" use="required" /&gt;
          &lt;xs:attribute name="phone" type="my_string_numeric" use="required" /&gt;
      &lt;/xs:complexType&gt;
  
      &lt;xs:complexType name="mybuilding"&gt;
          &lt;xs:sequence&gt;
              &lt;xs:element name="door" type="mydoor" minOccurs="0" maxOccurs="unbounded"&gt;
                  &lt;!-- I DON'T KNOW HOW TO SPECIFY THAT id of building, id of door and id of gate are in the same scope --&gt;
                  &lt;xs:unique name="d_idunique"&gt;
                      &lt;xs:selector xpath="door" /&gt;
                      &lt;xs:field xpath="@id" /&gt;
                  &lt;/xs:unique&gt;
              &lt;/xs:element&gt;
          &lt;/xs:sequence&gt;
          &lt;xs:attribute name="id" type="my_id" use="required" /&gt;
          &lt;xs:attribute name="name" type="xs:string" use="required" /&gt;
          &lt;xs:attribute name="country" type="xs:string" use="required" /&gt;
      &lt;/xs:complexType&gt;
  
      &lt;xs:complexType name="mydoor"&gt;
          &lt;xs:sequence&gt;
              &lt;xs:element name="gate" type="mygate" maxOccurs="unbounded"&gt;
                  &lt;!-- I DON'T KNOW HOW TO SPECIFY THAT id of building, id of door and id of gate are in the same scope --&gt;
                  &lt;xs:unique name="g_idunique"&gt;
                      &lt;xs:selector xpath="gate" /&gt;
                      &lt;xs:field xpath="@id" /&gt;
                  &lt;/xs:unique&gt;
              &lt;/xs:element&gt;
          &lt;/xs:sequence&gt;
          &lt;xs:attribute name="id" type="my_id" use="required" /&gt;
          &lt;xs:attribute name="address" type="xs:string" use="required" /&gt;
          &lt;xs:attribute name="status" type="mystatus" default="DISABLED" /&gt;
      &lt;/xs:complexType&gt;
  
      &lt;xs:complexType name="mygate"&gt;
          &lt;xs:attribute name="id" type="my_id" use="required" /&gt;
          &lt;xs:attribute name="type" type="mytype" use="required" /&gt;
          &lt;xs:attribute name="status" type="mystatus" default="DISABLED" /&gt;
      &lt;/xs:complexType&gt;
  
      &lt;xs:complexType name="myauthorization"&gt;
          &lt;xs:sequence&gt;
              &lt;xs:element name="validityperiod" type="myvalidityperiod" /&gt;
          &lt;/xs:sequence&gt;
          &lt;xs:attribute name="idu" type="my_id" use="required" /&gt;
          &lt;xs:attribute name="idao" type="my_id" use="required" /&gt;
      &lt;/xs:complexType&gt;
  
      &lt;!-- OMITTED USELESS PART OF THE SCHEMA --&gt;
  
  &lt;/xs:schema&gt;
  </code></pre>
  
  <p>I have two problems:</p>
  
  <ul>
  <li>I don't know how to specify that the id field of building, the id field of door and the id field of gate are in the same scope and I can't have 2 id equals (two building can't have the same id, but also a door and a building can't share the same id)</li>
  <li>I don't know how to use correctly the keyref element.
  <ol>
  <li>I'd like that idu filed of authorization element is an id that is present in one of the user elements (see [*] below).</li>
  <li>I'd like that the idao field of authorization element is an id that is present in one of the building elements OR one of the door elements.</li>
  </ol></li>
  </ul>
  
  <p>[*] I tried to write this, but it's not working:</p>
  
  <pre><code>&lt;xs:keyref name="useridkeyref" refer="u_idunique"&gt;
      &lt;xs:selector xpath="authorization" /&gt;   
      &lt;xs:field xpath="@idu" /&gt;
  &lt;/xs:keyref&gt;
  </code></pre>
  
  <p>I know this is not a short question and I thanks everyone in advance for reading it. I hope I can get some help. Thank you!</p>