title: Using dom4j DOMDocument to feed validator.validate(DOMSource) fails in java 1.6 (xsi:noNamespaceSchemaLocation is not allowed), works in 1.5
body: <p>Using dom4j DOMDocument to feed validator.validate(DOMSource) fails in java 1.6 (with xsi:noNamespaceSchemaLocation is not allowed to appear in root element), works in 1.5</p>
  
  <p>I'm finding the following problem quite intractable (OK, that's an understatement) - any insights will be appreciated.  Currently it seems like the best idea is to drop dom4j in favour of e.g. XOM (http://stackoverflow.com/questions/831865/what-java-xml-library-do-you-recommend-to-replace-dom4j).</p>
  
  <p>I've been validating in memory XML created from dom4j 'new DOMDocument()' - but this will not work with Java 6.</p>
  
  <p>The following call to validate(source) of a dom4j (1.6.1) DOMDocument derived DOMSource works with Java 1.5.x but fails with Java 1.6.x:</p>
  
  <pre><code>public void validate() throws Exception {
      SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
      schemaFactory.setErrorHandler(null);
      Schema schemaXSD = schemaFactory.newSchema(new URL(getSchemaURLString()));
      Validator validator = schemaXSD.newValidator();
      DOMSource source = new DOMSource(getDocument());
      validator.validate(source);
  }
  </code></pre>
  
  <p>getSchemaURLString() is also used to add the xsi:noNamespaceSchemaLocation attribute to the root node, i.e.:
  xsi:noNamespaceSchemaLocation="http://localhost:8080/integration/xsd/fqlResponseSchema-2.0.xsd" </p>
  
  <p>The exception follows:</p>
  
  <pre><code>Exception:  org.xml.sax.SAXParseException: cvc-complex-type.3.2.2: Attribute 'xsi:noNamespaceSchemaLocation' is not allowed to appear in element 'specialfields'.;                
  complex-type.3.2.2: Attribute 'xsi:noNamespaceSchemaLocation' is not allowed to appear in element 'specialfields'.
  at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:195)
  at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.error(ErrorHandlerWrapper.java:131)
  at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:384)
  at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:318)
  at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator$XSIErrorReporter.reportError(XMLSchemaValidator.java:417)
  at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.reportSchemaError(XMLSchemaValidator.java:3182)
  at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.processAttributes(XMLSchemaValidator.java:2659)
  at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.handleStartElement(XMLSchemaValidator.java:2066)
  at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.startElement(XMLSchemaValidator.java:705)
  at com.sun.org.apache.xerces.internal.jaxp.validation.DOMValidatorHelper.beginNode(DOMValidatorHelper.java:273)
  at com.sun.org.apache.xerces.internal.jaxp.validation.DOMValidatorHelper.validate(DOMValidatorHelper.java:240)
  at com.sun.org.apache.xerces.internal.jaxp.validation.DOMValidatorHelper.validate(DOMValidatorHelper.java:186)
  at com.sun.org.apache.xerces.internal.jaxp.validation.ValidatorImpl.validate(ValidatorImpl.java:104)
  at javax.xml.validation.Validator.validate(Validator.java:127)
  </code></pre>
  
  <p>Here's the start of the XML - generated after disabling the call to validator.validate(source):</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; 
  &lt;meetings xsi:noNamespaceSchemaLocation="http://localhost:8080/integration/xsd/fqlResponseSchema-2.0.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"&gt;
  .............
  &lt;/meetings&gt;
  </code></pre>
  
  <p>And of the XSD:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8" ?&gt;
  &lt;xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"&gt;
    &lt;xs:element name="meetings"&gt;
      &lt;xs:complexType&gt;
        &lt;xs:choice&gt;
      &lt;xs:sequence&gt;
            &lt;xs:element minOccurs="0" maxOccurs="1" ref="summary" /&gt;
            &lt;xs:element minOccurs="0" maxOccurs="unbounded" ref="meeting" /&gt;
          &lt;/xs:sequence&gt;
          &lt;xs:element ref="error" /&gt;
        &lt;/xs:choice&gt;
      &lt;/xs:complexType&gt;
    &lt;/xs:element&gt;
    &lt;xs:element name="summary"&gt;
  ................
  </code></pre>
  
  <p>So my root element is being rejected because it contains a xsi:noNamespaceSchemaLocation attribute.  And the schema itself does not specify that as a valid attribute of my root element?</p>
  
  <p>At this point it seems to me that I need to give up on dom4j for this task and switch to one of the other solutions, for example as outlined here:</p>
  
  <p>But I'd like to know what I've done wrong at any rate!</p>
  
  <p>Thanks in advance.</p>