JDK-6531160 : 1.6: DOM level 1 documents cannot be validated with a Schema, works with 1.5
  • Type: Bug
  • Component: xml
  • Sub-Component: org.w3c.dom
  • Affected Version: 6
  • Priority: P3
  • Status: Closed
  • Resolution: Fixed
  • OS: linux,windows_xp
  • CPU: x86
  • Submitted: 2007-03-05
  • Updated: 2012-06-11
  • Resolved: 2007-03-07
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
Other JDK 6 JDK 7
1.4.0 1.4Fixed 6u2Fixed 7Fixed
Related Reports
Duplicate :  
Duplicate :  
Duplicate :  
Relates :  
Description
FULL PRODUCT VERSION :
java version "1.6.0"
Java(TM) SE Runtime Environment (build 1.6.0-b105)
Java HotSpot(TM) Client VM (build 1.6.0-b105, mixed mode, sharing)


ADDITIONAL OS VERSION INFORMATION :
Windows XP, Linux Fedora core 4

A DESCRIPTION OF THE PROBLEM :
A DOM generated by code from a DocumentBuilder using DOM level 1 methods (createElement, etc) fails validation against an XML schema.  This used to work in JDK 1.5.  See attached test case.

See  com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator, around line 1840:

                fCurrentElemDecl = sGrammar.getGlobalElementDecl(element.localpart);

The localpart of the qname is used to lookup the root element; for an Element node created with Document.createElement, the local part is always null.

The same XML document read from a file and parsed with DocumentBuilder.parse does pass the validation.

Suspect this is related to bug id. 6465764.  Raised it separately because this problem relates to validation of new documents.

Need a workround because we do this a lot.

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Run the test case with JDK 1.6, you get a validation error.

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
No validation error
ACTUAL -
A validation error

ERROR MESSAGES/STACK TRACES THAT OCCUR :
cvc-elt.1: Cannot find the declaration of element 'root'.


REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
import java.io.StringReader;

import javax.xml.XMLConstants;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;

import org.w3c.dom.Document;
import org.xml.sax.SAXParseException;

public class validateerror {

  private static final String XSD =
     "<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema'>\n" +
     "  <xs:element name='root' type='xs:string'/>\n" +
     "</xs:schema>";
    
  public static void main(String ... args) throws Exception {
    SchemaFactory          fact    = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    Schema                 schema  = fact.newSchema(new StreamSource(new StringReader(XSD)));
    DocumentBuilderFactory docfact = DocumentBuilderFactory.newInstance();

    docfact.setNamespaceAware(true);
            
    Document doc = docfact.newDocumentBuilder().newDocument();
    
    doc.appendChild(doc.createElement("root"));

    try {
      schema.newValidator().validate(new DOMSource(doc));
    } catch (SAXParseException e) {
      System.out.printf("Validation failed: %s%n", e.getMessage());
    }
  }
}

---------- END SOURCE ----------

Release Regression From : 5.0u10
The above release value was the last known release where this 
bug was not reproducible. Since then there has been a regression.

Comments
WORK AROUND Added possible workaround to #6529766.
16-03-2007

EVALUATION After the last merge with Apache, some we brought over was not DOM level 1 compliant code. Wrote a unit test based on this CR and fix the problem. The JAXP 1.4 nightly build from Java.net (https://jaxp.dev.java.net) should have a fix for this CR --and we have also marked the fix for JDK 6.0 update release.
07-03-2007