title: XSD: unique elements
body: <p>I want to have unique references (below: author-ref) inside a book element, so that the two conditions are fulfilled:</p>
  
  <p>1) Each book can have multiple author-refs but they are all unique.</p>
  
  <p>2) Two different books can contain the same author-ref. </p>
  
  <p>With the following schema, condition 1) seems to be fulfilled but condition 2) not. I get the follwing error message:</p>
  
  <p>[Error] library.xml:15:41: cvc-identity-constraint.4.1: Duplicate unique value [T.Pratchett] declared for identity constraint "authorIdUniqueConstraint" of element "library".</p>
  
  <p>Why?</p>
  
  <p>The xml instance:</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;?xml version="1.1" encoding="UTF-8"?&gt;
  
  &lt;library xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
             xsi:noNamespaceSchemaLocation='library.xsd'&gt;
  
  
    &lt;book&gt;
      &lt;author-ref&gt;T.Pratchett&lt;/author-ref&gt;
      &lt;title&gt;The Colour of Magic&lt;/title&gt;
      &lt;year&gt;1983&lt;/year&gt;
    &lt;/book&gt;
  
    &lt;book&gt;
      &lt;author-ref&gt;T.Pratchett&lt;/author-ref&gt;&lt;!--two different refs but same ref as in the first book -&gt; gives error --&gt;
      &lt;author-ref&gt;J.K.Rowling&lt;/author-ref&gt;
      &lt;title&gt;Good Omens: The Nice and Accurate Prophecies...&lt;/title&gt;
      &lt;language&gt;en&lt;/language&gt;
    &lt;/book&gt;
  
    &lt;author id="J.K.Rowling"&gt;
      &lt;last-name&gt;Rowling&lt;/last-name&gt;
      &lt;first-name&gt;Joanne K.&lt;/first-name&gt;
    &lt;/author&gt;
  
    &lt;author id="T.Pratchett"&gt;
      &lt;last-name&gt;Pratchett&lt;/last-name&gt;
      &lt;first-name&gt;Terry&lt;/first-name&gt;
    &lt;/author&gt;
  
  &lt;/library&gt;
  </code></pre>
  
  <p>The xsd schema:</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;?xml version="1.0" encoding="ISO-8859-1"?&gt;
  &lt;xs:schema  xmlns:xs="http://www.w3.org/2001/XMLSchema"&gt;
      &lt;!-- definition of simple types --&gt;
      &lt;xs:simpleType name="yearType"&gt;
          &lt;xs:restriction base="xs:int"&gt;
              &lt;xs:pattern value="[0-9][0-9][0-9][0-9]"/&gt;
          &lt;/xs:restriction&gt;
      &lt;/xs:simpleType&gt;
  
      &lt;!-- definition of complex types --&gt;
      &lt;xs:complexType name="authorType"&gt;
          &lt;xs:sequence&gt;
              &lt;xs:element name="last-name" type="xs:string"/&gt;
              &lt;xs:element name="first-name" type="xs:string"/&gt;
          &lt;/xs:sequence&gt;
          &lt;xs:attribute name="id" use="required" type="xs:string"/&gt;
      &lt;/xs:complexType&gt;
  
      &lt;xs:complexType name="bookType"&gt;
          &lt;xs:sequence&gt;
              &lt;xs:element name="author-ref" minOccurs="1" maxOccurs="10" type="xs:string"/&gt;
              &lt;xs:element name="title" type="xs:string"/&gt;
              &lt;xs:element name="language" type="xs:language" minOccurs="0"/&gt;
              &lt;xs:element name="year" type="yearType" minOccurs="0"/&gt;
          &lt;/xs:sequence&gt;
      &lt;/xs:complexType&gt;
  
      &lt;!-- definition of root type library --&gt;
      &lt;xs:element name="library"&gt;
          &lt;xs:complexType&gt;
              &lt;xs:sequence&gt;
                  &lt;xs:element name="book" type="bookType" maxOccurs="unbounded"/&gt;
                  &lt;xs:element name="author" type="authorType" maxOccurs="unbounded"/&gt;
              &lt;/xs:sequence&gt;
          &lt;/xs:complexType&gt;
          &lt;xs:key name="authorId"&gt;
              &lt;xs:selector xpath="./author" /&gt;
              &lt;xs:field xpath="@id" /&gt;
          &lt;/xs:key&gt;
          &lt;xs:keyref name="authorIdRef" refer="authorId"&gt;
              &lt;xs:selector xpath="./book/author-ref" /&gt;
              &lt;xs:field xpath="." /&gt;
          &lt;/xs:keyref&gt;
          &lt;xs:unique name="authorIdUniqueConstraint"&gt;&lt;!-- problem here i guess --&gt;
              &lt;xs:selector xpath="./book/author-ref" /&gt;
              &lt;xs:field xpath="." /&gt;
          &lt;/xs:unique&gt;
      &lt;/xs:element&gt;
  &lt;/xs:schema&gt;
  </code></pre>