Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-6526547

JAXP behaving different for schema validation in Java 1.5 and Java 1.6

XMLWordPrintable

    • x86
    • windows_xp

      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.

            spericas Santiago Pericasgeertsen
            ndcosta Nelson Dcosta (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: