title: How to set up a key/keyref constraint where the key and keyref elements are named the same
body: <p>I'm looking to find a way in XPath 1.0 to create a constraint between keys and keyrefs.</p>
  
  <p>Say I have an XML document as such -</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8" ?&gt;
  
  &lt;root xmlns="schema.xsd"&gt;
  
    &lt;items&gt;
      &lt;item id="1"/&gt;
      &lt;item id="2"/&gt;
      &lt;item id="3"/&gt;
    &lt;/items&gt;
  
    &lt;group&gt;
      &lt;items&gt;
        &lt;item ref="2"/&gt;
      &lt;/items&gt;
    &lt;/group&gt;
  
  &lt;/root&gt;
  </code></pre>
  
  <p>I need to ensure that all items listed underneath 'group' refer to an item in the root/items list.</p>
  
  <p>For my XSD, I've tried a number of different ways, but none of them work.</p>
  
  <p>For example,</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;xs:schema xmlns="schema.xsd"
             xmlns:xs="http://www.w3.org/2001/XMLSchema"
             targetNamespace="schema.xsd" elementFormDefault="qualified"
             id="schema"&gt;
  
    &lt;xs:element name="root"&gt;
  
      &lt;xs:complexType&gt;
        &lt;xs:sequence&gt; 
  
          &lt;xs:element name="items"&gt;
            &lt;xs:complexType&gt;
              &lt;xs:sequence maxOccurs="unbounded"&gt;
  
                &lt;xs:element name="item"&gt;
                  &lt;xs:complexType&gt;
                    &lt;xs:attribute name="id"/&gt;
                  &lt;/xs:complexType&gt;
                &lt;/xs:element&gt;
  
              &lt;/xs:sequence&gt;
            &lt;/xs:complexType&gt;
          &lt;/xs:element&gt; 
  
          &lt;xs:element name="group" maxOccurs="unbounded"&gt;
            &lt;xs:complexType&gt;
              &lt;xs:sequence&gt;
  
                &lt;xs:element name="items"&gt;
                  &lt;xs:complexType&gt;
                    &lt;xs:sequence maxOccurs="unbounded"&gt;
  
                      &lt;xs:element name="item"&gt;
                        &lt;xs:complexType&gt;
                          &lt;xs:attribute name="ref"/&gt;
                        &lt;/xs:complexType&gt;                          
                      &lt;/xs:element&gt;
  
                    &lt;/xs:sequence&gt;
                  &lt;/xs:complexType&gt;
                  &lt;xs:keyref name="itemIdRef" refer="itemId"&gt;
                    &lt;xs:selector xpath="item"/&gt;
                    &lt;xs:field xpath="@ref"/&gt;
                  &lt;/xs:keyref&gt;
                &lt;/xs:element&gt;
  
              &lt;/xs:sequence&gt;
            &lt;/xs:complexType&gt;
          &lt;/xs:element&gt;   
  
        &lt;/xs:sequence&gt;    
      &lt;/xs:complexType&gt;
  
      &lt;xs:key name="itemId"&gt;
        &lt;xs:selector xpath="./root/items/item"/&gt;
        &lt;xs:field xpath="@id"/&gt;
      &lt;/xs:key&gt;
    &lt;/xs:element&gt;
  
  &lt;/xs:schema&gt;
  </code></pre>
  
  <p>Depending on the validator, either '...Has A Keyref Which Refers To A Key Or Unique That Is Out Of Scope' is thrown when using an id that exists, or no errors are thrown regardless of the ids used.  Either way, I've not managed to get this XSD to successfully validate in any validator, so the selectors I'm using for the key and keyref must be wrong.</p>
  
  <p>Can anyone spot where I've gone wrong with this?</p>