JDK-8168883 : xsd element name not resolved from an including schemasource
  • Type: Bug
  • Component: xml
  • Sub-Component: javax.xml.validation
  • Affected Version: 8,9
  • Priority: P4
  • Status: Open
  • Resolution: Unresolved
  • OS: generic
  • CPU: generic
  • Submitted: 2016-10-25
  • Updated: 2018-09-11
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
tbdUnresolved
Description
FULL PRODUCT VERSION :
java version "1.8.0_101"
Java(TM) SE Runtime Environment (build 1.8.0_101-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.101-b13, mixed mode)

ADDITIONAL OS VERSION INFORMATION :
Microsoft Windows [Version 10.0.10586]

A DESCRIPTION OF THE PROBLEM :
This problem was originally presented by me here:
http://stackoverflow.com/questions/40128190/xsd-element-name-not-resolved-in-unit-test

Text below is mostly a quotation of myself.

I am trying to validate xml files in a junit unit test. My simplified code is attached. Schemas are accessible from http://www.unece.org/ and http://www.gs1.org/

I was lucky to have some time to run my code through a debugger. It was pretty heavy stuff. For example com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDHandler.class contains over 100 imports and over 4000 lines of code.

Anyhow, I am willing to think that there is a programming error in com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaLoader.loadGrammar(XMLInputSource) that loads xsd grammars, or schemas or schemasources in our context.

First grammar loaded for http://www.unece.org/cefact/namespaces/StandardBusinessDocumentHeader namespace is BasicTypes.xsd.

My findings are that, that grammar is mapped by its namespace and after it is included by other schemas it somehow is prevententing the includers', that share the namespace, grammars from being mapped. I can succesfully refer from EPCglobal-epcis-1_1.xsd to any type defined in BasicTypes.xsd, but referring to any other element or type in http:/ /www.unece.org/cefact/namespaces/StandardBusinessDocumentHeader namespace that is not defined in BasicTypes.xsd results in a name resolving error.

My theory is backed up by that, if I move any type definition or element declaration to BasicTypes.xsd I can succesfully refer from EPCglobal-epcis-1_1.xsd to that type or element .



STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Download xsd files from mentioned sources, place them in folders according to source code and run the junit test. If asked for I can provide the xsd files also.

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Schema object is built correctly
ACTUAL -
Exception is thrown from 

schemaFactory.newSchema(sources);



ERROR MESSAGES/STACK TRACES THAT OCCUR :
Exception message:
target/test-classes/epcis11/xsd/EPCglobal-epcis-1_1.xsd; lineNumber: 46; columnNumber: 60; src-resolve: Cannot resolve the name 'sbdh:StandardBusinessDocumentHeader' to a(n) 'element declaration' component.

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
package test;

import java.net.URL;
import java.nio.file.Paths;

import javax.xml.XMLConstants;
import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;

import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;

public class Epcis11MessageCreatorForSoTest {

    private static Schema schema;

    private final static String[] XSD_FILES = { 
            "epcis11/xsd/BasicTypes.xsd", 
            "epcis11/xsd/DocumentIdentification.xsd",
            "epcis11/xsd/Partner.xsd",
            "epcis11/xsd/Manifest.xsd", 
            "epcis11/xsd/BusinessScope.xsd",
            "epcis11/xsd/StandardBusinessDocumentHeader.xsd", 
            "epcis11/xsd/EPCglobal.xsd",
            "epcis11/xsd/EPCglobal-epcis-1_1.xsd",
            "epcis11/xsd/EPCglobal-epcis-query-1_1.xsd",
            "epcis11/xsd/EPCglobal-epcis-masterdata-1_1.xsd",
    };

    @BeforeClass
    public static void beforeClass() throws Exception {
        try {
            System.setProperty("jaxp.debug", "10");

            SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);

            Source[] sources = new Source[XSD_FILES.length];
            int i = 0;
            for (String xsdfile : XSD_FILES) {
                URL resource = Epcis11MessageCreatorForSoTest.class.getClassLoader().getResource(xsdfile); 
                String systemId = Paths.get(resource.toURI()).toFile().getAbsolutePath();
                StreamSource ss = new StreamSource(
                        Epcis11MessageCreatorForSoTest.class.getClassLoader().getResourceAsStream(xsdfile),systemId);

                sources[i] = ss;
                i++;
            }
            schema = schemaFactory.newSchema(sources);

        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }
    @Test   
    public void testFoo() {
        Assert.assertTrue(true);
    }

}
---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
I can, unwillingly, consolidate all type definitions and element declarations in namespace http://www.unece.org/cefact/namespaces/StandardBusinessDocumentHeader that are used in this project into one xsd source file and then the schema is built correctly.


Comments
To reproduce the issue run the attached junit test case. Following are the results: JDK 8u112- Fail JDK 8u122 ea - Fail JDK 9-ea + 137 - Fail Following is the output: JUnit version 4.4 JAXP: using thread context class loader (sun.misc.Launcher$AppClassLoader@55f96302) for search JAXP: Looking up system property 'javax.xml.validation.SchemaFactory:http://www.w3.org/2001/XMLSchema' JAXP: The property is undefined. JAXP: found null in $java.home/jaxp.properties JAXP: attempting to use the platform default XML Schema validator JAXP: createInstance(com.sun.org.apache.xerces.internal.jaxp.validation.XMLSchemaFactory) JAXP: loaded com.sun.org.apache.xerces.internal.jaxp.validation.XMLSchemaFactory from jar:file:/D:/JDK8u111/jre/lib/rt.jar!/com/sun/org/apache/xerces/internal/jaxp/validation/XMLSchemaFactory.class JAXP: factory 'com.sun.org.apache.xerces.internal.jaxp.validation.XMLSchemaFactory' was found for http://www.w3.org/2001/XMLSchema org.xml.sax.SAXParseException; systemId: file:///D:/Java7Workspace/TestCases/src/epics11/xsd/EPCglobal-epcis-1_1.xsd; lineNumber: 46; columnNumber: 60; src-resolve: Cannot resolve the name 'sbdh:StandardBusinessDocumentHeader' to a(n) 'element declaration' component. at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:203) at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.error(ErrorHandlerWrapper.java:134) at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:396) at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDHandler.reportSchemaErr(XSDHandler.java:4156) at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDHandler.reportSchemaError(XSDHandler.java:4139) at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDHandler.getGlobalDecl(XSDHandler.java:1745) at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDElementTraverser.traverseLocal(XSDElementTraverser.java:170) at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDHandler.traverseLocalElements(XSDHandler.java:3612) at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDHandler.parseSchema(XSDHandler.java:636) at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaLoader.loadSchema(XMLSchemaLoader.java:613) at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaLoader.loadGrammar(XMLSchemaLoader.java:572) at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaLoader.loadGrammar(XMLSchemaLoader.java:538) at com.sun.org.apache.xerces.internal.jaxp.validation.XMLSchemaFactory.newSchema(XMLSchemaFactory.java:255) at Epcis11MessageCreatorForSoTest.beforeClass(Epcis11MessageCreatorForSoTest.java:48) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.junit.internal.runners.ClassRoadie.runBefores(ClassRoadie.java:49) at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:36) at org.junit.internal.runners.JUnit4ClassRunner.run(JUnit4ClassRunner.java:42) at org.junit.internal.runners.CompositeRunner.runChildren(CompositeRunner.java:33) at org.junit.internal.runners.CompositeRunner.run(CompositeRunner.java:28) at org.junit.runner.JUnitCore.run(JUnitCore.java:130) at org.junit.runner.JUnitCore.run(JUnitCore.java:109) at org.junit.runner.JUnitCore.run(JUnitCore.java:100) at org.junit.runner.JUnitCore.runMain(JUnitCore.java:81) at org.junit.runner.JUnitCore.main(JUnitCore.java:44) E Time: 0.584 There was 1 failure: 1) Epcis11MessageCreatorForSoTest org.xml.sax.SAXParseException; systemId: file:///D:/Java7Workspace/TestCases/src/epics11/xsd/EPCglobal-epcis-1_1.xsd; lineNumber: 46; columnNumber: 60; src-resolve: Cannot resolve the name 'sbdh:StandardBusinessDocumentHeader' to a(n) 'element declaration' component. at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:203) at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.error(ErrorHandlerWrapper.java:134) at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:396) at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDHandler.reportSchemaErr(XSDHandler.java:4156) at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDHandler.reportSchemaError(XSDHandler.java:4139) at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDHandler.getGlobalDecl(XSDHandler.java:1745) at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDElementTraverser.traverseLocal(XSDElementTraverser.java:170) at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDHandler.traverseLocalElements(XSDHandler.java:3612) at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDHandler.parseSchema(XSDHandler.java:636) at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaLoader.loadSchema(XMLSchemaLoader.java:613) at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaLoader.loadGrammar(XMLSchemaLoader.java:572) at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaLoader.loadGrammar(XMLSchemaLoader.java:538) at com.sun.org.apache.xerces.internal.jaxp.validation.XMLSchemaFactory.newSchema(XMLSchemaFactory.java:255) at Epcis11MessageCreatorForSoTest.beforeClass(Epcis11MessageCreatorForSoTest.java:48) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.junit.internal.runners.ClassRoadie.runBefores(ClassRoadie.java:49) at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:36) at org.junit.internal.runners.JUnit4ClassRunner.run(JUnit4ClassRunner.java:42) at org.junit.internal.runners.CompositeRunner.runChildren(CompositeRunner.java:33) at org.junit.internal.runners.CompositeRunner.run(CompositeRunner.java:28) at org.junit.runner.JUnitCore.run(JUnitCore.java:130) at org.junit.runner.JUnitCore.run(JUnitCore.java:109) at org.junit.runner.JUnitCore.run(JUnitCore.java:100) at org.junit.runner.JUnitCore.runMain(JUnitCore.java:81) at org.junit.runner.JUnitCore.main(JUnitCore.java:44) FAILURES!!! Tests run: 0, Failures: 1
28-10-2016