title: Different results of XMLLint and Xerces on XSD Schema validation?
body: <p>Context: I'm trying to find a way to validate XSD and XML against XSD that is automatable for integration into a software build-process. The input is files, and for the output an exit code or reliably regex’able console output would be sufficient. I am operating in a Windows environment.</p>
  
  <p>As XML and XSD input I have taken an example from Wikipedia (article "XML Schema (W3C)").</p>
  
  <p>As tools I have chosen to try <a href="ftp://ftp.zlatkovic.com/libxml/" rel="nofollow">XMLLint</a> and <a href="http://xerces.apache.org/xerces-c/stdinparse-3.html" rel="nofollow">Xerces</a>.</p>
  
  <p>The issue is, XMLLint and Xerces give different results.</p>
  
  <p>Which leads me to my questions: What other options to evaluate the tools, do I have? How am I supposed to decide which tool to choose?</p>
  
  <p>XML data example from Wikipedia, SimpleAddress.xml:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;Address xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:noNamespaceSchemaLocation="SimpleAddress.xsd"&gt;
    &lt;Recipient&gt;Mr. Walter C. Brown&lt;/Recipient&gt;
    &lt;House&gt;49&lt;/House&gt;
    &lt;Street&gt;Featherstone Street&lt;/Street&gt;
    &lt;Town&gt;LONDON&lt;/Town&gt;
    &lt;PostCode&gt;EC1Y 8SY&lt;/PostCode&gt;
    &lt;Country&gt;UK&lt;/Country&gt;
  &lt;/Address&gt;
  </code></pre>
  
  <p>XSD schema example from Wikipedia, SimpleAddress.xsd:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema"&gt;
    &lt;xs:element name="Address"&gt;
      &lt;xs:complexType&gt;
        &lt;xs:sequence&gt;
          &lt;xs:element name="Recipient" type="xs:string" /&gt;
          &lt;xs:element name="House" type="xs:string" /&gt;
          &lt;xs:element name="Street" type="xs:string" /&gt;
          &lt;xs:element name="Town" type="xs:string" /&gt;
          &lt;xs:element name="County" type="xs:string" minOccurs="0" /&gt;
          &lt;xs:element name="PostCode" type="xs:string" /&gt;
          &lt;xs:element name="Country" minOccurs="0"&gt;
            &lt;xs:simpleType&gt;
              &lt;xs:restriction base="xs:string"&gt;
                &lt;xs:enumeration value="IN" /&gt;
                &lt;xs:enumeration value="DE" /&gt;
                &lt;xs:enumeration value="ES" /&gt;
                &lt;xs:enumeration value="UK" /&gt;
                &lt;xs:enumeration value="US" /&gt;
              &lt;/xs:restriction&gt;
            &lt;/xs:simpleType&gt;
          &lt;/xs:element&gt;
        &lt;/xs:sequence&gt;
      &lt;/xs:complexType&gt;
    &lt;/xs:element&gt;
  &lt;/xs:schema&gt;
  </code></pre>
  
  <p>First my tests with XMLLint:</p>
  
  <pre><code>$ xmllint.exe --noout --schema SimpleAddress.xsd SimpleAddress.xml
  SimpleAddress.xml validates
  
  $ xmllint.exe --noout --schema http://www.w3.org/2001/XMLSchema.xsd SimpleAddress.xsd
  SimpleAddress.xsd validates
  </code></pre>
  
  <p>XMLLint gives me also meaningful error results when I intentionally put an extra attribute or tag in the file to be validated.</p>
  
  <p>With XMLLint, I also tried to validate the W3C’s XML Schema Definition against itself:</p>
  
  <pre><code>$ xmllint.exe --noout --schema http://www.w3.org/2001/XMLSchema.xsd http://www.w3.org/2001/XMLSchema.xsd
  http://www.w3.org/2001/XMLSchema.xsd validates
  </code></pre>
  
  <p>Second my tests with Xerces, that yields a lot of warnings and errors:</p>
  
  <pre><code>$ StdInParse.exe -n -s -f -v=always &lt; SimpleAddress.xml
  stdin: 1 ms (7 elems, 2 attrs, 19 spaces, 56 chars)
  
  $ StdInParse.exe -n -s -f -v=always &lt; SimpleAddress.xsd
  Error at (file stdin, line 2, char 87): no declaration found for element 'xs:schema'
  Error at (file stdin, line 2, char 87): attribute 'elementFormDefault' is not declared for element     'xs:schema'
  Error at (file stdin, line 2, char 87): attribute '{http://www.w3.org/2000/xmlns/}xs' is not declared  for element 'xs:schema'
  Error at (file stdin, line 3, char 30): no declaration found for element 'xs:element'
  (…)
  </code></pre>
  
  <p>For the following tests I have added the “xmlns:xsi” and “xsi:schemaLocation” attributes to the opening tag of SimpleAddress.xsd:</p>
  
  <pre><code>&lt;xs:schema 
    elementFormDefault="qualified" 
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.w3.org/2001/XMLSchema http://www.w3.org/2001/XMLSchema.xsd"
  &gt;
  </code></pre>
  
  <p>XMLLint yields the same result:</p>
  
  <pre><code>$ xmllint.exe --noout --schema http://www.w3.org/2001/XMLSchema.xsd SimpleAddress.xsd
  SimpleAddress.xsd validates
  </code></pre>
  
  <p>Xerces yields even more warnings and errors, now concerning the W3C’s documents too:</p>
  
  <pre><code>$ StdInParse.exe -n -s -f -v=always &lt; SimpleAddress.xsd
  Warning at (file http://www.w3.org/2001/datatypes.dtd, line 99, char 7): attribute 'id' has already been declared for element 'xs:simpleType'
  Warning at (file http://www.w3.org/2001/datatypes.dtd, line 122, char 7): attribute 'id' has already been declared for element 'xs:list'
  Warning at (file http://www.w3.org/2001/datatypes.dtd, line 130, char 7): attribute 'id' has already been declared for element 'xs:union'
  Warning at (file http://www.w3.org/2001/datatypes.dtd, line 140, char 20): attribute 'id' has already been declared for element 'xs:maxExclusive'
  (…)
  Error at (file stdin, line 15, char 68): unable to find validator for simple type of attribute 'maxOccurs'
  Error at (file stdin, line 16, char 56): unable to find validator for simple type of attribute 'maxOccurs'
  Error at (file stdin, line 17, char 50): unable to find validator for simple type of attribute 'maxOccurs'
  </code></pre>
  
  <p>In conclusion XMLLint seems to do what I need, however it has one major drawback. It requires me to specify the XSD file to validate against as a separate argument, as it seems to be unable to read the according xmlns attribute from the XML input files.</p>
  
  <p>On the other hand there’s Xerces. It seems to be widely used and claims to be faithful to the very same standards used in these kind of documents, but yields a lot of errors and warnings even about the W3C’s documents. So, I’m asking myself, if I’m using it right? I would like to get it to work as it wouldn’t require me to find a workaround for XMLLint’s necessary extra argument.</p>