title: How to reference an attribute in xsd
body: <p>I tried to define an attribute that could be referenced in subsequent element definitions.</p>
  
  <pre><code>&lt;xs:attribute name="ridref" type="xs:string"/&gt;
  </code></pre>
  
  <p>Later I use it like this:</p>
  
  <pre><code>&lt;xs:element name="coordRegRef"&gt;
    &lt;xs:complexType&gt;
      &lt;!--xs:attribute name="ridref" type="xs:string"/ this works but I want to use ref --&gt;
      &lt;xs:attribute ref="ridref" use="required"/&gt;
    &lt;/xs:complexType&gt;
  &lt;/xs:element&gt;
  </code></pre>
  
  <p>XSD compiles fine with xmllint</p>
  
  <pre><code>xmllint --schema pc-ar.xsd pc-ar.xml
  </code></pre>
  
  <p>But xmllint says</p>
  
  <pre><code>pc-ar.xml:41: element coordRegRef: Schemas validity error : Element '{http://pc-ar.xsd}coordRegRef', attribute 'ridref': The attribute 'ridref' is not allowed.
  pc-ar.xml:41: element coordRegRef: Schemas validity error : Element '{http://pc-ar.xsd}coordRegRef': The attribute '{http://pc-ar.xsd}ridref' is required but missing.
  </code></pre>
  
  <p>which I interpret as meaning that my XML file must use a namespace for ridref</p>
  
  <pre><code>&lt;coordRegRef fix:ridref="111"/&gt;
  </code></pre>
  
  <p>(which does work, but undesired) instead of</p>
  
  <pre><code>&lt;coordRegRef ridref="111"/&gt;
  </code></pre>
  
  <p>Why?</p>
  
  <p>My XSD</p>
  
  <pre><code>&lt;?xml version="1.0"?&gt;
  &lt;!-- validate: xmllint - -schema pc-ar.xsd pc-ar.xml --&gt;
  &lt;xs:schema 
      xmlns:xs="http://www.w3.org/2001/XMLSchema"
      targetNamespace="http://pc-ar.xsd"
      elementFormDefault="qualified"
      xmlns="http://pc-ar.xsd"
      xmlns:fix="http://pc-ar.xsd"&gt;
  
      &lt;!--xs:attribute name="rid" type="xs:integer"/--&gt;
  
      &lt;!--xs:attributeGroup name="ridGroup"&gt;
      &lt;xs:attribute name="rid" type="xs:integer"&gt; 
          &lt;xs:annotation&gt;
              &lt;xs:documentation&gt;entry id&lt;/xs:documentation&gt;
          &lt;/xs:annotation&gt;
      &lt;/xs:attribute&gt;
  &lt;/xs:attributeGroup--&gt;
  
      &lt;xs:element name="name" type="xs:string"/&gt;
      &lt;xs:attribute name="id" type="xs:ID"/&gt;
      &lt;xs:attribute name="idref" type="xs:IDREF"/&gt;
  
      &lt;xs:attribute name="ridref" type="xs:string"/&gt;
  
      &lt;xs:element name="coordRegRef"&gt;
          &lt;xs:complexType&gt;
              &lt;!--xs:attribute name="ridref" type="xs:string"/works--&gt;
              &lt;xs:attribute ref="ridref" use="required"/&gt;
          &lt;/xs:complexType&gt;
      &lt;/xs:element&gt;
  
      &lt;xs:element name="assetRegRef"&gt;
          &lt;xs:complexType&gt;
              &lt;xs:sequence&gt;
                  &lt;xs:element name="ridref2" type="xs:string"/&gt;
              &lt;/xs:sequence&gt;
              &lt;!--xs:attribute name="ridref" type="xs:string"/works--&gt;
              &lt;xs:attribute name="ridref" type="xs:string"/&gt;
          &lt;/xs:complexType&gt;
      &lt;/xs:element&gt;
  
      &lt;xs:element name="lat"&gt;
          &lt;xs:simpleType&gt;
              &lt;xs:restriction base="xs:decimal"&gt;
                  &lt;xs:minInclusive value="-40"/&gt;
                  &lt;xs:maxInclusive value="-30"/&gt;
              &lt;/xs:restriction&gt;
          &lt;/xs:simpleType&gt;
      &lt;/xs:element&gt;
  
      &lt;xs:element name="lon" type="xs:decimal"/&gt;
  
      &lt;xs:complexType name="coordRegTableType"&gt;
          &lt;xs:sequence&gt;
              &lt;xs:element name="entry" minOccurs="0" maxOccurs="unbounded"&gt;
                  &lt;xs:complexType&gt;
                      &lt;xs:sequence&gt;
                          &lt;xs:element ref="name"/&gt;
                          &lt;xs:element ref="lat"/&gt;
                          &lt;xs:element ref="lon"/&gt;
                      &lt;/xs:sequence&gt;
                      &lt;xs:attribute name="rid" type="xs:string"/&gt;
                      &lt;!--xs:attribute ref="fix:id" use="required"/--&gt;
                      &lt;xs:attribute name="id" type="xs:string" use="required"/&gt;
                  &lt;/xs:complexType&gt;
              &lt;/xs:element&gt;
          &lt;/xs:sequence&gt;
      &lt;/xs:complexType&gt;
  
      &lt;xs:complexType name="assetRegTableType"&gt;
          &lt;xs:sequence&gt;
              &lt;xs:element name="entry" minOccurs="0" maxOccurs="unbounded"&gt;
                  &lt;xs:complexType&gt;
                      &lt;xs:sequence&gt;
                          &lt;xs:element ref="name"/&gt;
                          &lt;xs:element ref="coordRegRef"/&gt;
                          &lt;xs:element ref="assetRegRef"/&gt;
                      &lt;/xs:sequence&gt;
                      &lt;xs:attribute name="rid" type="xs:string"/&gt;
                      &lt;xs:attribute name="id" type="xs:string" use="required"/&gt;
                  &lt;/xs:complexType&gt;
              &lt;/xs:element&gt;
          &lt;/xs:sequence&gt;
      &lt;/xs:complexType&gt;
  
      &lt;xs:element name="coordReg" type="fix:coordRegTableType"&gt;
          &lt;xs:unique name="coordRegNameUniq"&gt;
              &lt;xs:selector xpath="*"/&gt;
              &lt;xs:field xpath="fix:name"/&gt;
          &lt;/xs:unique&gt;
      &lt;/xs:element&gt;
  
      &lt;xs:element name="assetReg" type="assetRegTableType"&gt;
          &lt;xs:unique name="assetRegNameUniq"&gt;
              &lt;xs:selector xpath="fix:entry"/&gt;
              &lt;xs:field xpath="fix:name"/&gt;
          &lt;/xs:unique&gt;
      &lt;/xs:element&gt;
  
      &lt;xs:element name="assetExchange"&gt;
          &lt;xs:complexType&gt;
              &lt;xs:sequence&gt;
                  &lt;xs:element ref="coordReg"/&gt;
                  &lt;xs:element ref="assetReg"/&gt;
              &lt;/xs:sequence&gt;
              &lt;xs:attribute name="rid" type="xs:integer"/&gt;
              &lt;!--xs:attribute ref="id" use="required"/--&gt;
          &lt;/xs:complexType&gt;
  
          &lt;xs:key name="coordRegKey"&gt;
              &lt;xs:selector xpath="fix:coordReg/fix:entry"/&gt;
              &lt;xs:field xpath="@rid"/&gt;
          &lt;/xs:key&gt;
  
          &lt;xs:key name="assetRegKey"&gt;
              &lt;xs:selector xpath="fix:assetReg/fix:entry"/&gt;
              &lt;xs:field xpath="@rid"/&gt;
          &lt;/xs:key&gt;
  
          &lt;xs:keyref name="assetRegKeyRef" refer="assetRegKey"&gt;
              &lt;xs:selector xpath=".//fix:assetRegRef"/&gt;
              &lt;xs:field xpath="@ridref"/&gt;
          &lt;/xs:keyref&gt;
  
          &lt;xs:keyref name="assetRegKeyRef2" refer="assetRegKey"&gt;
              &lt;!--xs:selector xpath=".//fix:assetRegRef"/works--&gt;
              &lt;!--xs:selector xpath="./*/*/fix:assetRegRef"/works--&gt;
              &lt;!--xs:selector xpath="./*/fix:entry/fix:assetRegRef"/works--&gt;
              &lt;!--xs:selector xpath="./*/*/fix:assetRegRef"/works--&gt;
              &lt;xs:selector xpath=".//fix:assetRegRef"/&gt; &lt;!-- any assetRefRef --&gt;
              &lt;xs:field xpath="fix:ridref2"/&gt;
          &lt;/xs:keyref&gt;
  
          &lt;xs:keyref name="coordRegKeyRef" refer="coordRegKey"&gt;
              &lt;xs:selector xpath=".//fix:coordRegRef"/&gt;
              &lt;xs:field xpath="@ridref"/&gt;
          &lt;/xs:keyref&gt;
  
      &lt;/xs:element&gt;
  
  &lt;/xs:schema&gt; 
  </code></pre>
  
  <p>My XML test file</p>
  
  <pre><code>&lt;?xml version="1.0"?&gt;
  
  &lt;assetExchange 
    xmlns="http://pc-ar.xsd"
    xmlns:fix="http://pc-ar.xsd"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://pc-ar.xsd pc-ar.xsd"
    rid="1"&gt;
  
  &lt;coordReg&gt;
    &lt;entry id="1" rid="111"&gt;
      &lt;name&gt;sitea&lt;/name&gt;
      &lt;lat&gt;-31.23&lt;/lat&gt;
      &lt;lon&gt;151&lt;/lon&gt;
    &lt;/entry&gt;
  
    &lt;entry id="2" rid="222"&gt;
      &lt;name&gt;siteb&lt;/name&gt;
      &lt;lat&gt;-31.23&lt;/lat&gt;
      &lt;lon&gt;151&lt;/lon&gt;
    &lt;/entry&gt;
  
    &lt;entry id="3" rid="333"&gt;
      &lt;name&gt;sitec&lt;/name&gt;
      &lt;lat&gt;-31.23&lt;/lat&gt;
      &lt;lon&gt;151&lt;/lon&gt;
    &lt;/entry&gt;
  &lt;/coordReg&gt;
  
  &lt;assetReg&gt;
    &lt;entry id="11" rid="11"&gt;
      &lt;name&gt;deva&lt;/name&gt;
      &lt;coordRegRef fix:ridref="111"/&gt;
      &lt;assetRegRef ridref="aa"&gt;&lt;ridref2&gt;aa&lt;/ridref2&gt;&lt;/assetRegRef&gt;
    &lt;/entry&gt;
  
    &lt;entry id="22" rid="22"&gt;
      &lt;name&gt;devb&lt;/name&gt;
      &lt;coordRegRef ridref="2212"/&gt;
      &lt;assetRegRef ridref="11"&gt;&lt;ridref2&gt;11&lt;/ridref2&gt;&lt;/assetRegRef&gt;
    &lt;/entry&gt;
  
    &lt;entry id="33" rid="33"&gt;
      &lt;name&gt;devc&lt;/name&gt;
      &lt;coordRegRef ridref="333"/&gt;
      &lt;assetRegRef ridref="44"&gt;&lt;ridref2&gt;44&lt;/ridref2&gt;&lt;/assetRegRef&gt;
    &lt;/entry&gt;
  
  &lt;/assetReg&gt;
  
  &lt;/assetExchange&gt;
  </code></pre>