title: XSD assuring that each enumeration value can only occur once as attribute
body: <p>Let's say I have a schema to define a list of articles. Each of these articles has a list of long names, one for each known language. The language is an attribute at the level of an individual long name.</p>
  <p>I want to assure 2 things:</p>
  <ol>
  <li>The language attribute can only have one of several pre-determined values (solved using an enumeration).</li>
  <li>Within each <strong>list</strong> of long names, a given value for the language attribute can only occur once. For each article I can only have one English long name, one German long name, one French long name etc.</li>
  </ol>
  <p>Following another post (<a href="https://stackoverflow.com/questions/42793170/xml-schema-can-i-use-unique-with-enumeration-for-validation">link</a>) I implemented a unique constraint at the longName-level. It will satisfy #1, but not #2. If I have 'EN' twice as longName attribute within the same longNames element, it will consider the xml valid.</p>
  <h2>Original sample XML</h2>
  <pre><code>&lt;article&gt;
          &lt;articleNumber&gt;3019&lt;/articleNumber&gt;
          &lt;longNames&gt;
              &lt;longName language=&quot;EN&quot;&gt;
                  &lt;description&gt;A French Omelette with Cheese&lt;/description&gt;
              &lt;/longName&gt;
              &lt;longName language=&quot;FR&quot;&gt;
                  &lt;description&gt;Un omelette DU(???) fromage&lt;/description&gt;
              &lt;/longName&gt;
          &lt;/longNames&gt;
      &lt;/article&gt;
  </code></pre>
  <h2>Original XSD schema</h2>
  <pre><code>    &lt;xs:complexType name=&quot;ArticleType&quot;&gt;
      &lt;xs:sequence&gt;
          &lt;xs:element name=&quot;articleNumber&quot; type=&quot;xs:integer&quot; /&gt;
          &lt;xs:element type=&quot;bl:LongNamesType&quot; name=&quot;longNames&quot; /&gt;
          &lt;xs:element type=&quot;bl:ShortNameType&quot; name=&quot;shortName&quot; maxOccurs=&quot;3&quot; /&gt;
          &lt;xs:element type=&quot;bl:AllergensType&quot; name=&quot;allergens&quot; maxOccurs=&quot;3&quot; /&gt;
          &lt;xs:element type=&quot;bl:IngredientsType&quot; name=&quot;ingredients&quot; maxOccurs=&quot;3&quot; /&gt;
          &lt;xs:element type=&quot;bl:PictureType&quot; name=&quot;picture&quot; maxOccurs=&quot;3&quot; /&gt;
          &lt;xs:element name=&quot;barcode&quot; type=&quot;xs:integer&quot;/&gt;
      &lt;/xs:sequence&gt;
  &lt;/xs:complexType&gt;
  
  &lt;xs:complexType name=&quot;LongNamesType&quot;&gt;
      &lt;xs:sequence&gt;
          &lt;xs:element name=&quot;longName&quot; maxOccurs=&quot;unbounded&quot;&gt;
              &lt;xs:complexType&gt;
                  &lt;xs:sequence&gt;
                      &lt;!-- &lt;xs:element type=&quot;bl:LanguageType&quot; name=&quot;language&quot; /&gt; --&gt;
                      &lt;xs:element name=&quot;description&quot; type=&quot;xs:string&quot; /&gt;
                  &lt;/xs:sequence&gt;
                  &lt;xs:attribute name=&quot;language&quot; use=&quot;required&quot;&gt;
                      &lt;xs:simpleType&gt;
                          &lt;xs:restriction base=&quot;xs:string&quot;&gt;
                              &lt;xs:enumeration value=&quot;EN&quot; /&gt;
                              &lt;xs:enumeration value=&quot;FR&quot; /&gt;
                              &lt;xs:enumeration value=&quot;DE&quot; /&gt;
                          &lt;/xs:restriction&gt;
                      &lt;/xs:simpleType&gt;
                  &lt;/xs:attribute&gt;
              &lt;/xs:complexType&gt;
              &lt;xs:unique name=&quot;uniqueLongNameLanguage&quot;&gt;
                  &lt;xs:selector xpath=&quot;longNames&quot; /&gt;
                  &lt;xs:field xpath=&quot;@language&quot; /&gt;
              &lt;/xs:unique&gt;              
          &lt;/xs:element&gt;
      &lt;/xs:sequence&gt;
  &lt;/xs:complexType&gt;
  </code></pre>
  <p>Could anyone help me figure out what I'm doing wrong?</p>
  <hr />
  <p>Edit: thanks to Yitzak Khabinsky and Michael Kay I managed to find below working solution. Admittedly it's a bit of a mixture of styles. Thanks to all who replied!</p>
  <h2>Improved XSD</h2>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;no&quot;?&gt;
  &lt;xs:schema
      xmlns:xs=&quot;http://www.w3.org/2001/XMLSchema&quot; elementFormDefault=&quot;qualified&quot; attributeFormDefault=&quot;unqualified&quot;
          xmlns:bl=&quot;urn:inventoryinput&quot;
          targetNamespace=&quot;urn:inventoryinput&quot;&gt;
  
  &lt;xs:element name=&quot;inventory&quot; type=&quot;bl:InventoryType&quot; /&gt;
      &lt;xs:complexType name=&quot;InventoryType&quot;&gt;
              &lt;xs:sequence&gt;
                  &lt;xs:element type=&quot;bl:ArticleType&quot; name=&quot;article&quot;  maxOccurs=&quot;unbounded&quot; /&gt;
              &lt;/xs:sequence&gt;
      &lt;/xs:complexType&gt;
          
      &lt;xs:complexType name=&quot;ArticleType&quot;&gt;
          &lt;xs:sequence&gt;
              &lt;xs:element name=&quot;articleNumber&quot; type=&quot;xs:integer&quot; /&gt;
              &lt;xs:element ref=&quot;bl:longNames&quot; minOccurs=&quot;0&quot; /&gt;
              &lt;xs:element name=&quot;barcode&quot; type=&quot;xs:integer&quot;/&gt;
          &lt;/xs:sequence&gt;
      &lt;/xs:complexType&gt;
      
      &lt;xs:element name=&quot;longNames&quot;&gt;
          &lt;xs:complexType&gt;
              &lt;xs:sequence&gt;
                  &lt;xs:element ref=&quot;bl:longName&quot; maxOccurs=&quot;unbounded&quot;/&gt;
              &lt;/xs:sequence&gt;
          &lt;/xs:complexType&gt;
          &lt;xs:unique name=&quot;uniqueLongNameLanguage&quot;&gt;
              &lt;xs:selector xpath=&quot;bl:longName&quot; /&gt;
              &lt;xs:field xpath=&quot;@language&quot; /&gt;
          &lt;/xs:unique&gt;              
      &lt;/xs:element&gt;
      
      &lt;xs:element name=&quot;longName&quot; &gt;
        &lt;xs:complexType&gt;
          &lt;xs:sequence&gt;
              &lt;xs:element name=&quot;description&quot; type=&quot;xs:string&quot; /&gt;
          &lt;/xs:sequence&gt;
          &lt;xs:attribute name=&quot;language&quot; use=&quot;required&quot;&gt;
              &lt;xs:simpleType&gt;
                  &lt;xs:restriction base=&quot;xs:string&quot;&gt;
                      &lt;xs:enumeration value=&quot;NL&quot; /&gt;
                      &lt;xs:enumeration value=&quot;FR&quot; /&gt;
                      &lt;xs:enumeration value=&quot;DE&quot; /&gt;
                  &lt;/xs:restriction&gt;
              &lt;/xs:simpleType&gt;
          &lt;/xs:attribute&gt;
      &lt;/xs:complexType&gt;
    &lt;/xs:element&gt;
  
  ...
  </code></pre>
  <h2>Reworked XML example</h2>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;no&quot;?&gt;
  &lt;inventory xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
  xmlns:xsd=&quot;http://www.w3.org/2001/XMLSchema&quot; xmlns=&quot;urn:inventoryinput&quot;&gt;
  
          &lt;article&gt;
              &lt;articleNumber&gt;3019&lt;/articleNumber&gt;
              &lt;longNames&gt;
                  &lt;longName language=&quot;NL&quot;&gt;
                      &lt;description&gt;DE TROG Ardeens bruin brood Bio 600g&lt;/description&gt;
                  &lt;/longName&gt;
                  &lt;longName language=&quot;FR&quot;&gt;
                      &lt;description&gt;DE TROG pain ardennais complet Bio 600g&lt;/description&gt;
                  &lt;/longName&gt;
              &lt;/longNames&gt;
  </code></pre>