title: How to implement key elements must appear in referenced elements XML Schema?
body: <p>I am not sure I have written the title properly in relation what I want to ask (question titles too long are not admitted).</p>
  
  <h2>The problem by words</h2>
  
  <p>I have an element 'A' that contains a set of key elements (called 'a') and an element 'B' that contains a set of elements 'b(a1,a2)' (where 'a1' and 'a2' could be equals, there's not constraint in this). The goal is to implement an  XML rule that forces all 'a' elements must appears/be referenced by elements 'b'.</p>
  
  <p><strong>Case not desired</strong>:</p>
  
  <p>A:{a1, a2, a3, a4, a5}</p>
  
  <p>B:{b1(a1, a5), b2(a2, a4)}</p>
  
  <p>Element a3 is left alone and doesn't appears in some 'b' element.</p>
  
  <p><strong>Case OK</strong>:</p>
  
  <p>A:{a1, a2, a3, a4}</p>
  
  <p>B:{b1(a1, a4), b2(a2, a3)}</p>
  
  <p>All 'a' elements are referenced by some 'b'.</p>
  
  <p><strong>Case OK</strong>:</p>
  
  <p>A:{a1, a2, a3, a4}</p>
  
  <p>B:{b1(a1, a2), b2(a2, a1), b3(a1, a3), b4(a4, a4}</p>
  
  <p>All 'a' elements are referenced by some 'b'.</p>
  
  <h2>The problem by code</h2>
  
  <p><strong>XML OK</strong></p>
  
  <pre><code>...
  &lt;nodes&gt;
      &lt;node nodeID="n1" functionalType="WEB_CLIENT"/&gt;
      &lt;node nodeID="n2" functionalType="FW"/&gt;
      &lt;node nodeID="n3" functionalType="NAT"/&gt;
      &lt;node nodeID="n6" functionalType="NAT"/&gt;
      &lt;node nodeID="n4" functionalType="WEB_SERVER"/&gt;
      &lt;node nodeID="n5" functionalType="WEB_SERVER"/&gt;
      &lt;node nodeID="n10" functionalType="WEB_SERVER"/&gt;
  &lt;/nodes&gt;
  &lt;links&gt;
      &lt;link linkID="l1"&gt;
          &lt;SRC nodeID="n1"/&gt;
          &lt;DST nodeID="n2"/&gt;
      &lt;/link&gt;
      &lt;link linkID="l2"&gt;
          &lt;SRC nodeID="n2"/&gt;
          &lt;DST nodeID="n3"/&gt;
      &lt;/link&gt;
      &lt;link linkID="l4"&gt;
          &lt;SRC nodeID="n3"/&gt;
          &lt;DST nodeID="n4"/&gt;
      &lt;/link&gt;
      &lt;link linkID="l5"&gt;
          &lt;SRC nodeID="n3"/&gt;
          &lt;DST nodeID="n6"/&gt;
      &lt;/link&gt;
      &lt;link linkID="l6"&gt;
          &lt;SRC nodeID="n6"/&gt;
          &lt;DST nodeID="n5"/&gt;
      &lt;/link&gt;
  &lt;/links&gt;
  ...
  </code></pre>
  
  <p>Element <code>node nodeID="n10"</code> is left alone.</p>
  
  <p>Part of XML Schema:</p>
  
  <pre><code>...
  &lt;xsd:key name="linkKey"&gt;
      &lt;xsd:selector xpath="links/link"/&gt;
      &lt;xsd:field xpath="@linkID"/&gt;
  &lt;/xsd:key&gt;
  
  &lt;xsd:key name="nodeKey"&gt;
      &lt;xsd:selector xpath="nodes/node"/&gt;
      &lt;xsd:field xpath="@nodeID"/&gt;
  &lt;/xsd:key&gt;
  
  &lt;xsd:keyref name="dummy1" refer="nodeKey"&gt;
      &lt;xsd:selector xpath="links/link/SRC"/&gt;
      &lt;xsd:field xpath="@nodeID"/&gt;
  &lt;/xsd:keyref&gt;
  
  &lt;xsd:keyref name="dummy2" refer="nodeKey"&gt;
      &lt;xsd:selector xpath="links/link/DST"/&gt;
      &lt;xsd:field xpath="@nodeID"/&gt;
  &lt;/xsd:keyref&gt;
  ...
  </code></pre>
  
  <p>and the definitions of the elements types:</p>
  
  <pre><code>...
  &lt;!-- Nodes --&gt;
  
  &lt;xsd:complexType name="nodes_t"&gt;
      &lt;xsd:sequence&gt;
          &lt;xsd:element name="node" type="node_t" minOccurs="1" maxOccurs="unbounded"/&gt;
      &lt;/xsd:sequence&gt;
  &lt;/xsd:complexType&gt;
  
  &lt;xsd:complexType name="node_t"&gt;
      &lt;xsd:attribute name="nodeID" type="xsd:string" use="required"/&gt;
      &lt;xsd:attribute ref="functionalType"/&gt;
  &lt;/xsd:complexType&gt;
  
  
  &lt;!-- Links --&gt;
  
  &lt;xsd:complexType name="links_t"&gt;
      &lt;xsd:sequence&gt;
          &lt;xsd:element name="link" type="link_t" minOccurs="1" maxOccurs="unbounded"/&gt;
      &lt;/xsd:sequence&gt;
  &lt;/xsd:complexType&gt;
  
  &lt;xsd:complexType name="link_t"&gt;
      &lt;xsd:sequence&gt;
          &lt;xsd:element name="SRC" type="node_t" minOccurs="1" maxOccurs="1"/&gt;
          &lt;xsd:element name="DST" type="node_t" minOccurs="1" maxOccurs="1"/&gt;
      &lt;/xsd:sequence&gt;
      &lt;xsd:attribute name="linkID" type="xsd:string" use="required"/&gt;
  &lt;/xsd:complexType&gt;
  ...
  </code></pre>