title: XSD: how to use 'unique' & 'key'/'keyref' with element values?
body: <p>I trying to use <code>&lt;xs:unique&gt;</code> and <code>&lt;xs:key&gt;/&lt;xs:keyref&gt;</code> with element values but I just can't get it to work. If I do it with attrubute values it works like a charm.</p>
  
  <p><strong>Test.xml</strong></p>
  
  <pre><code>&lt;test:config xmlns:test="http://www.example.org/Test"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://www.example.org/Test Test.xsd "&gt;
  
      &lt;test:location id="id1" path="/path2"&gt;
          &lt;test:roles&gt;
              &lt;test:role&gt;role1&lt;/test:role&gt;
              &lt;test:role&gt;role2&lt;/test:role&gt;
              &lt;test:role&gt;role2&lt;/test:role&gt; &lt;!-- DUPLICATE: FAIL VALIDATION  --&gt;
          &lt;/test:roles&gt;
          &lt;test:action name="action1"&gt;
              &lt;test:roles&gt;
                  &lt;test:role&gt;role1&lt;/test:role&gt;
                  &lt;test:role&gt;role1&lt;/test:role&gt; &lt;!-- DUPLICATE: FAIL VALIDATION --&gt;
                  &lt;test:role&gt;role3&lt;/test:role&gt; &lt;!-- NOT DEFINED: FAIL VALIDATION --&gt;
              &lt;/test:roles&gt;
          &lt;/test:action&gt;
      &lt;/test:location&gt;
  
  &lt;/test:config&gt;
  </code></pre>
  
  <p>I want ensure that <em>roles</em> are only defined once and that the <em>roles</em> defined under the <em>action</em> element are only those defined at the upper level.</p>
  
  <p><strong>Test.xsd</strong>
  </p>
  
  <pre><code>&lt;xs:element name="config"&gt;
      &lt;xs:complexType&gt;
          &lt;xs:sequence&gt;
              &lt;xs:element ref="test:location" maxOccurs="unbounded" /&gt;
          &lt;/xs:sequence&gt;
      &lt;/xs:complexType&gt;       
  &lt;/xs:element&gt;
  
  &lt;xs:element name="location" type="test:LocationType"&gt;
      &lt;xs:key name="keyRole"&gt;
          &lt;xs:selector xpath="test:roles" /&gt;
          &lt;xs:field xpath="test:role" /&gt;
      &lt;/xs:key&gt;
      &lt;xs:keyref name="keyrefRole" refer="test:keyRole"&gt;
          &lt;xs:selector xpath="test:action/test:roles" /&gt;
          &lt;xs:field xpath="test:role" /&gt;
      &lt;/xs:keyref&gt;
  &lt;/xs:element&gt;
  
  &lt;xs:complexType name="LocationType"&gt;
      &lt;xs:sequence&gt;
          &lt;xs:element ref="test:roles" minOccurs="0" /&gt;
          &lt;xs:element name="action" type="test:ActionType" minOccurs="0" maxOccurs="unbounded"/&gt;
      &lt;/xs:sequence&gt;
      &lt;xs:attribute name="id" type="xs:string" use="required"/&gt;
      &lt;xs:attribute name="path" type="xs:string" use="required"/&gt;
  &lt;/xs:complexType&gt;
  
  &lt;xs:element name="roles" type="test:RolesType"&gt;
      &lt;xs:unique name="uniqueRole"&gt;
          &lt;xs:selector xpath="." /&gt;
          &lt;xs:field xpath="test:role" /&gt;
      &lt;/xs:unique&gt;
  &lt;/xs:element&gt;
  
  &lt;xs:complexType name="RolesType"&gt;
      &lt;xs:sequence&gt;
          &lt;xs:element name="role" type="xs:string" maxOccurs="unbounded"/&gt;
      &lt;/xs:sequence&gt;      
  &lt;/xs:complexType&gt;
  
  &lt;xs:complexType name="ActionType"&gt;
      &lt;xs:sequence&gt;
          &lt;xs:element ref="test:roles" /&gt;
      &lt;/xs:sequence&gt;
      &lt;xs:attribute name="name" type="xs:string" use="required" /&gt;
  &lt;/xs:complexType&gt;
  </code></pre>
  
  <p></p>
  
  <p>The validation fails with these messages:</p>
  
  <pre><code>Description Resource    Path    Location    Type
  cvc-identity-constraint.3: Field "./test:role" of identity constraint "keyrefRole" matches more than one value within the scope of its selector; fields must match unique values.   Test.xml    /filebrowser-ejb/src/test/resources line 15 XML Problem
  cvc-identity-constraint.3: Field "./test:role" of identity constraint "keyrefRole" matches more than one value within the scope of its selector; fields must match unique values.   Test.xml    /filebrowser-ejb/src/test/resources line 16 XML Problem
  cvc-identity-constraint.3: Field "./test:role" of identity constraint "keyRole" matches more than one value within the scope of its selector; fields must match unique values.  Test.xml    /filebrowser-ejb/src/test/resources line 9  XML Problem
  cvc-identity-constraint.3: Field "./test:role" of identity constraint "keyRole" matches more than one value within the scope of its selector; fields must match unique values.  Test.xml    /filebrowser-ejb/src/test/resources line 10 XML Problem
  cvc-identity-constraint.3: Field "./test:role" of identity constraint "uniqueRole" matches more than one value within the scope of its selector; fields must match unique values.   Test.xml    /filebrowser-ejb/src/test/resources line 9  XML Problem
  cvc-identity-constraint.3: Field "./test:role" of identity constraint "uniqueRole" matches more than one value within the scope of its selector; fields must match unique values.   Test.xml    /filebrowser-ejb/src/test/resources line 10 XML Problem
  cvc-identity-constraint.3: Field "./test:role" of identity constraint "uniqueRole" matches more than one value within the scope of its selector; fields must match unique values.   Test.xml    /filebrowser-ejb/src/test/resources line 15 XML Problem
  cvc-identity-constraint.3: Field "./test:role" of identity constraint "uniqueRole" matches more than one value within the scope of its selector; fields must match unique values.   Test.xml    /filebrowser-ejb/src/test/resources line 16 XML Problem
  cvc-identity-constraint.4.1: Duplicate unique value [role1] declared for identity constraint "uniqueRole" of element "roles".   Test.xml    /filebrowser-ejb/src/test/resources line 9  XML Problem
  cvc-identity-constraint.4.1: Duplicate unique value [role1] declared for identity constraint "uniqueRole" of element "roles".   Test.xml    /filebrowser-ejb/src/test/resources line 15 XML Problem
  cvc-identity-constraint.4.2.2: Duplicate key value [role1] declared for identity constraint "keyRole" of element "location".    Test.xml    /filebrowser-ejb/src/test/resources line 9  XML Problem
  cvc-identity-constraint.4.3: Key 'keyrefRole' with value 'role3' not found for identity constraint of element 'location'.   Test.xml    /filebrowser-ejb/src/test/resources line 19 XML Problem
  </code></pre>
  
  <p>If I comment out the lines that should fail, validation still fails now with these messages:</p>
  
  <pre><code>Description Resource    Path    Location    Type
  cvc-identity-constraint.3: Field "./test:role" of identity constraint "keyRole" matches more than one value within the scope of its selector; fields must match unique values.  Test.xml    /filebrowser-ejb/src/test/resources line 10 XML Problem
  cvc-identity-constraint.3: Field "./test:role" of identity constraint "uniqueRole" matches more than one value within the scope of its selector; fields must match unique values.   Test.xml    /filebrowser-ejb/src/test/resources line 10 XML Problem
  </code></pre>
  
  <p>What am I doing wrong?</p>