title: XSD 1.1 Check if an attribute exists in another element
body: <p>I have 2 main sections in my XML file Sequence and StateRelations. The Sequence section is defined as. The Transition element should be unqiue based on its 4 properties as defined below.</p>
  
  <pre><code>&lt;xs:element name="Transition"&gt;
      &lt;xs:complexType&gt;
          &lt;xs:sequence&gt;
              &lt;xs:element ref="Element1" minOccurs="0" maxOccurs="1"/&gt;
              &lt;xs:element ref="Element2" minOccurs="0" maxOccurs="1"/&gt;
              &lt;xs:element ref="Element3" minOccurs="0" maxOccurs="unbounded"/&gt;
          &lt;/xs:sequence&gt;
          &lt;xs:attribute name="stateName" type="xs:string" use="required"/&gt;
          &lt;xs:attribute name="A" type="xs:string" use="required"/&gt;
          &lt;xs:attribute name="B" type="xs:string" use="required"/&gt;
          &lt;xs:attribute name="C" type="xs:string" use="optional"/&gt;
      &lt;/xs:complexType&gt;
  &lt;/xs:element&gt;
  &lt;xs:element name="Sequence"&gt;
      &lt;xs:complexType&gt;
          &lt;xs:sequence&gt;
              &lt;xs:element ref="Transition" minOccurs="0" maxOccurs="unbounded"/&gt;
          &lt;/xs:sequence&gt;
          &lt;xs:attribute name="name" type="xs:string" use="optional"/&gt;
          &lt;xs:attribute ref="xml:base"/&gt;
      &lt;/xs:complexType&gt;
      &lt;xs:unique name="uniqueTransition"&gt;
          &lt;xs:selector xpath=".//Transition"/&gt;
          &lt;xs:field xpath="@stateName"/&gt;
          &lt;xs:field xpath="@A"/&gt;
          &lt;xs:field xpath="@B"/&gt;
          &lt;xs:field xpath="@C"/&gt;
      &lt;/xs:unique&gt;
  &lt;/xs:element&gt;
  </code></pre>
  
  <p>and StateRelations section is defined as follows. "<em>stateName1</em>" is actually a foreign key (keyref) which related to the "<em>stateName</em>" (key) of Transition. <em>Note: Relation element is actually recursive.</em></p>
  
  <pre><code>&lt;xs:element name="Relation"&gt;
      &lt;xs:complexType&gt;
          &lt;xs:sequence&gt;
              &lt;xs:element ref="Relation" minOccurs="0" maxOccurs="unbounded"/&gt;
          &lt;/xs:sequence&gt;
          &lt;xs:attribute name="stateName1" type="xs:string" use="required"/&gt;
      &lt;/xs:complexType&gt;
  &lt;/xs:element&gt;
  &lt;xs:element name="StateRelations"&gt;
      &lt;xs:complexType&gt;
          &lt;xs:sequence&gt;
              &lt;xs:element ref="Relation" minOccurs="0" maxOccurs="unbounded"/&gt;
          &lt;/xs:sequence&gt;
          &lt;xs:attribute name="name" type="xs:string" use="optional"/&gt;
          &lt;xs:attribute ref="xml:base"/&gt;
      &lt;/xs:complexType&gt;
  &lt;/xs:element&gt;
  </code></pre>
  
  <p>The problem comes in the following scenario. Since Transition has a unique constraint based on 4 attributes so the following XML is valid. </p>
  
  <pre><code>&lt;Transition stateName="S3" A="a1" B="b1" C="c"/&gt;
  &lt;Transition stateName="S3" A="a" B="b" C="c"/&gt;
  </code></pre>
  
  <p>As you can see that <em>stateName=S3</em> can repeat multiple times. But this repetition breaks the primary-foreign key relationship between Transition and Relations. Reason: stateName can repeat in Transitions. Now, we have a conflict here. My end goal would be to have </p>
  
  <ol>
  <li><p>Unique Sequences based on 4 attributes</p></li>
  <li><p>and each <em>stateName1</em> in StateRelations should be a valid <em>stateName</em>
  defined in Transitions.</p></li>
  </ol>
  
  <p>Till now, I know that key-keyref won't work in my scenario so I started looking into <em>assert</em> but I cannot get it to work. I have tried the following but nothing seems to be working.</p>
  
  <pre><code>    &lt;xs:element name="Relation"&gt;
      &lt;xs:complexType&gt;
          &lt;xs:sequence&gt;
              &lt;xs:element ref="Relation" minOccurs="0" maxOccurs="unbounded"/&gt;
          &lt;/xs:sequence&gt;
          &lt;xs:attribute name="stateName1" type="xs:string" use="required"/&gt;
  
          &lt;xs:assert test="matches( .//Transition/@stateName , @stateName1 )"/&gt;
          &lt;xs:assert test="/Replay/Sequence/Transition[contains(@stateName, @stateName1)]" /&gt;
          &lt;xs:assert test="/Replay/Sequence/Transition[contains(@stateName, string( @stateName1 ))]/@stateName = string(@stateName1) "/&gt;
  
      &lt;/xs:complexType&gt;
  &lt;/xs:element&gt;
  </code></pre>
  
  <p><strong>EDIT</strong>: Here is the XSD (I have omitted some details).
  Now, I want to somehow verify that <code>/Replay/StateRelations/Relation/@stateName1</code> exists in <code>/Replay/Sequence/Transition/@stateName</code>. I cannot use key/keyref because <code>/Replay/Sequence/Transition/@stateName</code> will not be unique.</p>
  
  <pre><code>&lt;Replay&gt;
     &lt;xs:element name="Relation"&gt;
      &lt;xs:complexType&gt;
          &lt;xs:sequence&gt;
              &lt;xs:element ref="Relation" minOccurs="0" maxOccurs="unbounded"/&gt;
          &lt;/xs:sequence&gt;
          &lt;xs:attribute name="stateName1" type="xs:string" use="required"/&gt;
          &lt;xs:assert test="matches( .//Transition/@stateName , @stateName1 )"/&gt;
          &lt;xs:assert test="/Replay/Sequence/Transition[contains(@stateName, @stateName1)]" /&gt;
          &lt;xs:assert test="/Replay/Sequence/Transition[contains(@stateName, string( @stateName1 ))]/@stateName = string(@stateName1) "/&gt;
      &lt;/xs:complexType&gt;
  &lt;/xs:element&gt;
  &lt;xs:element name="StateRelations"&gt;
      &lt;xs:complexType&gt;
          &lt;xs:sequence&gt;
              &lt;xs:element ref="Relation" minOccurs="0" maxOccurs="unbounded"/&gt;
          &lt;/xs:sequence&gt;
          &lt;xs:attribute name="name" type="xs:string" use="optional"/&gt;
          &lt;xs:attribute ref="xml:base"/&gt;
      &lt;/xs:complexType&gt;
  &lt;/xs:element&gt;
  
  &lt;xs:element name="Transition"&gt;
      &lt;xs:complexType&gt;
          &lt;xs:sequence&gt;
              &lt;xs:element ref="Element1" minOccurs="0" maxOccurs="1"/&gt;
              &lt;xs:element ref="Element2" minOccurs="0" maxOccurs="1"/&gt;
              &lt;xs:element ref="Element3" minOccurs="0" maxOccurs="unbounded"/&gt;
          &lt;/xs:sequence&gt;
          &lt;xs:attribute name="stateName" type="xs:string" use="required"/&gt;
          &lt;xs:attribute name="A" type="xs:string" use="required"/&gt;
          &lt;xs:attribute name="B" type="xs:string" use="required"/&gt;
          &lt;xs:attribute name="C" type="xs:string" use="optional"/&gt;
      &lt;/xs:complexType&gt;
  &lt;/xs:element&gt;
  &lt;xs:element name="Sequence"&gt;
      &lt;xs:complexType&gt;
          &lt;xs:sequence&gt;
              &lt;xs:element ref="Transition" minOccurs="0" maxOccurs="unbounded"/&gt;
          &lt;/xs:sequence&gt;
          &lt;xs:attribute name="name" type="xs:string" use="optional"/&gt;
          &lt;xs:attribute ref="xml:base"/&gt;
      &lt;/xs:complexType&gt;
      &lt;xs:unique name="uniqueTransition"&gt;
          &lt;xs:selector xpath=".//Transition"/&gt;
          &lt;xs:field xpath="@stateName"/&gt;
          &lt;xs:field xpath="@A"/&gt;
          &lt;xs:field xpath="@B"/&gt;
          &lt;xs:field xpath="@C"/&gt;
      &lt;/xs:unique&gt;
  &lt;/xs:element&gt;
  &lt;/Replay&gt;
  </code></pre>