JDK-6526547 : JAXP behaving different for schema validation in Java 1.5 and Java 1.6
  • Type: Bug
  • Component: xml
  • Sub-Component: jaxp
  • Affected Version: 6
  • Priority: P3
  • Status: Closed
  • Resolution: Duplicate
  • OS: windows_xp
  • CPU: x86
  • Submitted: 2007-02-20
  • Updated: 2012-06-11
  • Resolved: 2007-09-26
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
1.4.0Resolved
Related Reports
Duplicate :  
Description
FULL PRODUCT VERSION :
jdk 1.6

ADDITIONAL OS VERSION INFORMATION :
Windows XP

A DESCRIPTION OF THE PROBLEM :
I have an application where a DOM document is generated on the fly and then needs to be validated against the XML-schema.
Using JDK 1.5 (jdk1.5.0_01) this works well and the DOM is correctly validated.
When changing to JDK 1.6 (jdk1.6.0) the validation complains that the first attribute on the root element is invalid (cvc-complex-type.3.2.2: Attribute 'CreationDateTime' is not allowed to appear in element 'ODM'), this although this is a required attribute according to the schema.

Here is a snippet of my code:

//create a SchemaFactory capable of understanding WXS schemas
SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
// load a WXS schema, represented by a Schema instance
Schema schema = factory.newSchema(schemaFile);
System.out.println("schema = " + schema);
// create a Validator instance, which can be used to validate an instance document
Validator validator = schema.newValidator();
System.out.println("validator = " + validator);
ErrorHandlerImpl errorhandler = new ErrorHandlerImpl();
errorhandler.setErrorWarningVector(errorWarningVector); // some other info
errorhandler.setSchemaInformation(schemaInformation); // some other info
validator.setErrorHandler(errorhandler);
validator.validate(new DOMSource(doc)); // doc is DOM document

what I observed is that with JDK 1.5 and with JDK 1.6 the Schema objects are not the same:
JDK 1.5: com.sun.org.apache.xerces.internal.jaxp.validation.xs.SchemaImpl
JDK 1.6
com.sun.org.apache.xerces.internal.jaxp.validation.XMLSchema

When using JDK 1.6, everything becomes suddenly a bit strange.
When I first write the DOM document to file, and then do the validation using that file, everything goes fine.
But when I use the DOM document directly, validation fails.

The code:

// workaround : print the DOM document to file
File tempFile = new File("temp.xml");
DOMPrinter pr = new DOMPrinter(doc, tempFile);
pr.printDomDocument();
// end of workaround
SchemaFactory factory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
try {
// load a WXS schema, represented by a Schema instance
Schema schema = factory.newSchema(schemaFile);
Validator validator = schema.newValidator();
// read the document from the file
DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
domFactory.setNamespaceAware(true);
DocumentBuilder builder = domFactory.newDocumentBuilder();
Document mydoc = builder.parse(tempFile);
// now validate using the document from file
validator.validate(new DOMSource(mydoc)); // OK: validates fine
// validate the DOM document that was the source for writing to file
valdator.validate(new DOMSource(doc)); // gives an error (invalid attribute on root element)

P.S. Yes I did set setNamespaceAware(true) when building the original DOM.
P.S. See also http://forum.java.sun.com/thread.jspa?threadID=5134606
P.S. It was reported that the example given in http://java.sun.com/j2se/1.5.0/docs/api/javax/xml/validation/package-summary.html does not work either anymore when using Java 1.6

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
see above, or try sample at http://java.sun.com/j2se/1.5.0/docs/api/javax/xml/validation/package-summary.html


EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
validator does not report an error
ACTUAL -
validator reports an error

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
take code from http://java.sun.com/j2se/1.5.0/docs/api/javax/xml/validation/package-summary.html
---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
See above: write DOM to file, read DOM from file and validate (see above)

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
EVALUATION A fix to support validation of DOM level 1 documents has been checked into the nightly build of JAXP at Java.net. Please see CR 6531160 for further details.
07-03-2007

EVALUATION The issue with the example here, http://java.sun.com/j2se/1.5.0/docs/api/javax/xml/validation/package-summary.html is that the factory does not have namespace processing enabled. Apparently, this isn't the problem with this CR. However, without the code that builds the DOM in question, it won't be easy to find the problem. I'm marking this CR as INCOMPLETE.
20-02-2007