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

XML validation with schema fails to find top level element definition

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Not an Issue
    • Icon: P3 P3
    • None
    • 6
    • xml
    • x86
    • windows_xp

      FULL PRODUCT VERSION :
      C:\Documents and Settings\ndjc>java -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 :
      Microsoft Windows XP [Version 5.1.2600]

      A DESCRIPTION OF THE PROBLEM :
      XML validation with schema fails to find the top level element definition using Java 6. It worked with Java 5.

      Given the following W3C Schema:
      ================================================
      <?xml version="1.0"?>
      <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
         <xs:element name="import">
         </xs:element>
      </xs:schema>
      ================================================
      and the following instance document:
      ================================================
      <?xml version="1.0" encoding="US-ASCII"?>
      <import>
      </import>
      ================================================

      validation fails in Java 6 with the following error:

      cvc-elt.1: Cannot find the declaration of element 'import'

      Might be similar to bug 6465764?

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Run the enclosed Java program with Java 5 (succeeds) and Java 6 (fails).

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Validation should succeed on Java 6.

      ACTUAL -
      Validation succeeds with Java 5, and fails with Java 6, with the error message cvc-elt.1: Cannot find the declaration of element 'import'

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

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      package validate;

      import java.io.*;

      import javax.xml.XMLConstants;
      import javax.xml.parsers.*;
      import javax.xml.transform.Source;
      import javax.xml.transform.dom.DOMSource;
      import javax.xml.transform.stream.StreamSource;
      import javax.xml.validation.*;

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


      /**
       * A simple tester for XML validation.
       */
      public final class MyValidator
      {
          private boolean valid;


          private class ValidatorErrorHandler implements ErrorHandler
          {
              public void fatalError(SAXParseException e) throws SAXException
              {
                  throw e;
              }


              public void error(SAXParseException e) throws SAXException
              {
                  System.err.printf("Validation error on line %d: %s%n",e.getLineNumber(),
                      e.getLocalizedMessage());
                  valid = false;
              }


              public void warning(SAXParseException e) throws SAXException
              {
                  System.err.printf("Validation warning on line %d: %s%n",e.getLineNumber(),
                      e.getLocalizedMessage());
              }
          }


          private void validate()
          {
              try
              {
                  DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
                  DocumentBuilder builder = builderFactory.newDocumentBuilder();
                  Document document = builder.parse(new FileInputStream(new File("file.xml")));
                  SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
                  Source source = new StreamSource("schema.xsd");
                  Schema schema = schemaFactory.newSchema(source);
                  Validator validator = schema.newValidator();
                  validator.setErrorHandler(new ValidatorErrorHandler());
                  valid = true;
                  validator.validate(new DOMSource(document));
                  if (valid)
                  {
                      System.out.println("XML validated successfully!");
                  }
                  else
                  {
                      System.out.println("XML validation failed");
                  }
              }
              catch (Exception e)
              {
                  System.out.println("Validation caused an exception");
                  e.printStackTrace();
              }

          }


          /**
           * Main entry point for XML validation testing.
           * @param args Command line arguments
           */
          public static void main(String... args)
          {
              new MyValidator().validate();
          }
      }

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

      CUSTOMER SUBMITTED WORKAROUND :
      Cannot upgrade to Java 6 until this is resolved.

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

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

      Release Regression From : 5.0
      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
            keshah Ketan Shah (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: