title: XML Schema: Uniqueness constraints
body: <p>After hours of trying I'm still not able to get this simple example to do I want. The goal is very simple: A xml-Document with Notes is only valid if every <code>Node</code> is assigned a unique <code>NoteID</code>.</p>
  
  <p>Here is my <code>Notes.xsd</code>:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
              targetNamespace="http://xml.netbeans.org/schema/Notes"
              xmlns:tns="http://xml.netbeans.org/schema/Notes"
              elementFormDefault="qualified"&gt;
      &lt;xsd:element name="Notes"&gt;
          &lt;xsd:complexType&gt;
              &lt;xsd:sequence&gt;
                  &lt;xsd:element name="Note" maxOccurs="unbounded"&gt;
                      &lt;xsd:complexType&gt;
                          &lt;xsd:sequence&gt;
                              &lt;xsd:element name="NoteID" type="xsd:positiveInteger"/&gt;
                              &lt;xsd:element name="Content"  type="xsd:string"/&gt;
                          &lt;/xsd:sequence&gt;
                      &lt;/xsd:complexType&gt;
                      &lt;xsd:unique name="newKey"&gt;
                          &lt;xsd:selector xpath="."/&gt;
                          &lt;xsd:field xpath="NoteID"/&gt;
                      &lt;/xsd:unique&gt;
                  &lt;/xsd:element&gt;
              &lt;/xsd:sequence&gt;
          &lt;/xsd:complexType&gt;
      &lt;/xsd:element&gt;
  &lt;/xsd:schema&gt;
  </code></pre>
  
  <p>And <code>Notes.xml</code>:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;ns0:Notes  xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
              xmlns:ns0='http://xml.netbeans.org/schema/Notes'
              xsi:schemaLocation='http://xml.netbeans.org/schema/Notes Notes.xsd'&gt;
      &lt;ns0:Note&gt;
          &lt;ns0:NoteID&gt;1&lt;/ns0:NoteID&gt;
          &lt;ns0:Content&gt;this&lt;/ns0:Content&gt;
      &lt;/ns0:Note&gt;
      &lt;ns0:Note&gt;
          &lt;ns0:NoteID&gt;1&lt;/ns0:NoteID&gt;
          &lt;ns0:Content&gt;is a&lt;/ns0:Content&gt;
      &lt;/ns0:Note&gt;
      &lt;ns0:Note&gt;
          &lt;ns0:NoteID&gt;3&lt;/ns0:NoteID&gt;
          &lt;ns0:Content&gt;test&lt;/ns0:Content&gt;
      &lt;/ns0:Note&gt;
  &lt;/ns0:Notes&gt;
  </code></pre>
  
  <p>And I have no idea why this validates:</p>
  
  <pre><code>$ xmllint --noout -schema Notes.xsd Notes.xml 
  Notes.xml validates
  </code></pre>