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

xsd element name not resolved from an including schemasource

    Details

      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.

        Attachments

          Activity

            People

            • Assignee:
              joehw Joe Wang
              Reporter:
              webbuggrp Webbug Group
            • Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

              • Created:
                Updated: