United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
Bug ID: 6531160 1.6: DOM level 1 documents cannot be validated with a Schema, works with 1.5
6531160 : 1.6: DOM level 1 documents cannot be validated with a Schema, works with 1.5

Details
Type:
Bug
Submit Date:
2007-03-05
Status:
Closed
Updated Date:
2012-06-11
Project Name:
JDK
Resolved Date:
2007-03-07
Component:
xml
OS:
linux,windows_xp
Sub-Component:
org.w3c.dom
CPU:
x86
Priority:
P3
Resolution:
Fixed
Affected Versions:
6
Fixed Versions:
1.4.0

Related Reports
Backport:
Backport:
Duplicate:
Duplicate:
Duplicate:
Relates:

Sub Tasks

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.
                                     
2007-03-16
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.
                                     
2007-03-07



Hardware and Software, Engineered to Work Together