title: Xerces XMLSchemaFactory: Cannot find the declaration of element
body: <p>I wish to validate an XML file (which has no namespace, nor schema declaration in the XML file) against an XSD file.</p>
  
  <p>The XML file looks like:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;report&gt; ... &lt;/report&gt;
  </code></pre>
  
  <p>And the XSD file, is:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"&gt;
    &lt;xs:element name="report"&gt;
      ...
    &lt;/xs:element&gt;
  &lt;/xs:schema&gt;
  </code></pre>
  
  <p>The code is similar to the following:</p>
  
  <pre><code>SchemaFactory xsdFac = SchemaFactory.newInstance(
     XMLConstants.W3C_XML_SCHEMA_NS_URI);
  xsdFac.setErrorHandler( ... );
  Schema xsd = xsdFac.newSchema(xsdUrl);  // xsdUrl works
  Validator xsdValidator = xsd.newValidator();
  xsdValidator.validate(new DOMSource(doc));  // doc is correct
  </code></pre>
  
  <p>I have spent 3 hours on this, as it works locally on my PC, it used to work on the server (I think), but now it does not work on the server. I tried to identify differences between my PC and the server, but they both use the same JARs, and so on.</p>
  
  <p>Anyway, I have identified the following difference. I was not passing a class name to the <code>SchemaFactory.newInstance</code> method (almost certainly a <a href="http://www.databasesandlife.com/java-always-explicitly-specify-which-xml-parser-to-use/" rel="nofollow">mistake</a>), when i printed out the class name of the <code>xsdFac</code>, I saw it was different between locally and on the server. I don't think I want to get in to why that was the case (I have no idea), I think it's better to find one which works, and explicitly specify it.</p>
  
  <ul>
  <li>On my PC (<strong>works</strong>) it was <code>com.sun.org.apache.xerces.internal.jaxp.validation.XMLSchemaFactory</code>. If I explicitly specify that on the <code>newInstance</code> call, then it works on the PC and on the server.</li>
  <li>On the server (<strong>didn't work</strong>), it was <code>org.apache.xerces.jaxp.validation.XMLSchemaFactory</code>. If I explicitly set that on the <code>newInstance</code> call, then I get the same error on both the PC and the server.</li>
  </ul>
  
  <p>Notice the <code>com.sun</code> at the start of the one that works.</p>
  
  <p>So at least I have a solution, that's good. But, I think I shouldn't be using <code>com.sun</code> classes explicitly in my code?</p>
  
  <p>Other information:</p>
  
  <ul>
  <li>The error I get is: <code>org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration of element 'report'</code></li>
  <li>This is the same error I get when, on the working version, I change the element name to something else, something that doesn't exist in the schema. So I think it just means, "I understand your schema, and I understand the XML, and you've used an element that isn't declared anywhere in the schema."</li>
  <li><code>xercesImpl-2.9.1.jar</code> is in my project (both on the server and on the PC).</li>
  <li>This JAR contains an <code>XMLSchemaFactory.class</code> under the package specified on the non-working version (i.e. would lead me to believe it's there, findable, and should work, and after all I don't get any exceptions relating to classes not found)</li>
  <li>The <code>doc</code> object I am parsing is, in both cases, a <code>org.apache.xerces.dom.DeferredDocumentImpl</code></li>
  </ul>
  
  <p>So, my question is: I would like to use the explicit Xerces implementation of the schema factory (I think), as I am including the JAR and I have a Document object from Xerces and basically I am using the Xerces validator anyway (in the working case of <code>com.sun</code>).</p>
  
  <p>Has anyone has a similar experience?</p>