title: XML, XSD xs:keyref within recursive complex type defintion
body: <p>I have some xml data with a list of allowed types at the top and a recursive list type of objects and sub-objects that refer to the allowed types:</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;ROOT xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="KEYREF_TEST.xsd"&gt;
    &lt;ALLOWED_SHAPE_TYPES&gt;
      &lt;SHAPE_TYPE&gt;Circle&lt;/SHAPE_TYPE&gt;
      &lt;SHAPE_TYPE&gt;Triangle&lt;/SHAPE_TYPE&gt;
      &lt;SHAPE_TYPE&gt;Square&lt;/SHAPE_TYPE&gt;
    &lt;/ALLOWED_SHAPE_TYPES&gt;
    &lt;SHAPES&gt;
      &lt;SHAPE&gt;
        &lt;TYPE&gt;Triangle&lt;/TYPE&gt;
        &lt;SUB_SHAPES&gt;
          &lt;SHAPE&gt;
            &lt;TYPE&gt;Circle&lt;/TYPE&gt;
            &lt;SUB_SHAPES/&gt;
          &lt;/SHAPE&gt;
          &lt;SHAPE&gt;
            &lt;TYPE&gt;Square&lt;/TYPE&gt;
            &lt;SUB_SHAPES&gt;
              &lt;SHAPE&gt;
                &lt;TYPE&gt;Triangle&lt;/TYPE&gt;
                &lt;SUB_SHAPES/&gt;
              &lt;/SHAPE&gt;
            &lt;/SUB_SHAPES&gt;
          &lt;/SHAPE&gt;
        &lt;/SUB_SHAPES&gt;
      &lt;/SHAPE&gt;
      &lt;SHAPE&gt;
        &lt;TYPE&gt;Square&lt;/TYPE&gt;
        &lt;SUB_SHAPES/&gt;
      &lt;/SHAPE&gt;
    &lt;/SHAPES&gt;
  &lt;/ROOT&gt;
  </code></pre>
  
  <p>I have defined the schema that validates this document as follows:</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"&gt;
    &lt;xs:element name="ROOT"&gt;
      &lt;xs:complexType&gt;
        &lt;xs:sequence&gt;
          &lt;xs:element name="ALLOWED_SHAPE_TYPES"&gt;
            &lt;xs:complexType&gt;
              &lt;xs:sequence minOccurs="0" maxOccurs="unbounded"&gt;
                &lt;xs:element name="SHAPE_TYPE" type="xs:string"/&gt;
              &lt;/xs:sequence&gt;
            &lt;/xs:complexType&gt;
          &lt;/xs:element&gt;
          &lt;xs:element name="SHAPES" type="shape-list-type"/&gt;
        &lt;/xs:sequence&gt;
      &lt;/xs:complexType&gt;
      &lt;xs:unique name="SHAPE_TYPE_UK"&gt;
        &lt;xs:selector xpath="ALLOWED_SHAPE_TYPES/SHAPE_TYPE"/&gt;
        &lt;xs:field xpath="."/&gt;
      &lt;/xs:unique&gt;
    &lt;/xs:element&gt;
    &lt;xs:complexType name="shape-list-type"&gt;
      &lt;xs:sequence&gt;
        &lt;xs:element name="SHAPE" minOccurs="0" maxOccurs="unbounded"&gt;
          &lt;xs:complexType&gt;
           &lt;xs:sequence&gt;
              &lt;xs:element name="TYPE" type="xs:string"&gt;
                &lt;xs:keyref name="SHAPE_TYPE_FK" refer="SHAPE_TYPE_UK"&gt;
                  &lt;xs:selector xpath="."&gt;&lt;/xs:selector&gt;
                  &lt;xs:field xpath="."&gt;&lt;/xs:field&gt;
                &lt;/xs:keyref&gt;
              &lt;/xs:element&gt;
              &lt;xs:element name="SUB_SHAPES" type="shape-list-type"/&gt;
            &lt;/xs:sequence&gt;
          &lt;/xs:complexType&gt;
        &lt;/xs:element&gt;
      &lt;/xs:sequence&gt;
    &lt;/xs:complexType&gt;
  &lt;/xs:schema&gt;
  </code></pre>
  
  <p>When validating in oXygen 13.2 I get the following error:</p>
  
  <pre><code>System ID: C:\pvcswork\CodeSource\XMLSchema\KEYREF_DATA.xml
  Main validation file: C:\pvcswork\CodeSource\XMLSchema\KEYREF_DATA.xml
  Schema: C:\pvcswork\CodeSource\XMLSchema\KEYREF_TEST.xsd
  Engine name: Xerces
  Severity: error
  Description: Identity Constraint error:  identity constraint "KeyRef@1df2ead" has a keyref which refers to a key or unique that is out of scope.
  Start location: 10:28
  </code></pre>
  
  <p>I believe this is due to the xs:keyref being a child of the xs:complexType definition which is not a sub-element of the xs:element where the xs:unique is defined.</p>
  
  <p>I could move the xs:keyref into the ROOT definition and target the TYPE elements with an XPath, but the // syntax is not permitted so I would lose the recursion and have to define a new xs:keyref for each level of nesting. ** UPDATE - // is permitted when at the start of the XPath, preceeded by a dot! **</p>
  
  <p>Is there a solution I have missed (still using XSDs)? Thanks in advance.. </p>