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

1.6: DOM level 1 documents cannot be validated with a Schema, works with 1.5

    Details

    • Type: Bug
    • Status: Closed
    • Priority: P3
    • Resolution: Fixed
    • Affects Version/s: 6
    • Fix Version/s: 1.4.0
    • Component/s: xml
    • Subcomponent:
    • Resolved In Build:
      1.4
    • CPU:
      x86
    • OS:
      linux, windows_xp
    • Verification:
      Verified

      Backports

        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&#39;>\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.

          Attachments

            Issue Links

              Activity

                People

                • Assignee:
                  spericas Santiago Pericasgeertsen
                  Reporter:
                  ryeung Roger Yeung (Inactive)
                • Votes:
                  0 Vote for this issue
                  Watchers:
                  1 Start watching this issue

                  Dates

                  • Created:
                    Updated:
                    Resolved:
                    Imported:
                    Indexed: