eclipse-ee4j / jaxb-ri

Jaxb RI
https://eclipse-ee4j.github.io/jaxb-ri/
BSD 3-Clause "New" or "Revised" License
202 stars 111 forks source link

Issue with XML validation against XSD file with online imported XSD files #1153

Open Tomas-Kraus opened 6 years ago

Tomas-Kraus commented 6 years ago

In my XML schema, many online XSD files have been imported in. When I tried to make the validation of an XML against XSD file with the following code:

  URL url = MachineXMLServiceImpl.class.getResource(xsdFilePath);
  SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
  Schema schema = sf.newSchema(url);

  JAXBContext jaxbContext;
  jaxbContext = JAXBContext.newInstance(Class.forName(className));

  Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
  unmarshaller.setSchema(schema);
  unmarshaller.unmarshal(xmlReader);

The following error occurred.

Invalid XML file. An issue occurred in parsing with the following error message.

org.xml.sax.SAXParseException: src-resolve: Cannot resolve the name 'type_from_import_online_xml_schema' to a(n) 'element declaration' component.

I can't download all referred XSD files manually, as these are more than forty XSD files.

By searching, I found two solutions. The first solution is to download all the XSD files and change manually all the online URLs of schemaLocation to the local relative paths. But as that change need to be done in more than 300 places. It is not a very practical solution for us.

The second solution is to follow this code. Which I found it here.

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

InputStream impFis = BR0301.class.getClassLoader()
                .getResourceAsStream("imported.xsd");
InputStream mainFis = BR0301.class.getClassLoader()
                .getResourceAsStream("main.xsd");

Source main = new StreamSource(mainFis);
Source imp = new StreamSource(impFis);
Source[] schemaFiles = new Source[] {imp, main};
Schema schema = schemaFactory.newSchema(schemaFiles);
schema.newValidator().validate(new DOMSource(ervBericht));

But the refferred code isn't very sufficient either because we need for all XSD files to create an InputStream and Source object and finally add all of them in the schema.

During generation of Java classes we create the catalog.cat file to rewrite system id like the following code.

REWRITE_SYSTEM "http://www.exampleUrl.com/test/1.0"  "test/1.0"  
SYSTEM "http://www.example.com/xsdFile.xsd" "imports/xsdFile.xsd"

Is there any way to use the catalog file also for the validation of the XML file against XSD file?

Tomas-Kraus commented 6 years ago
Express85 commented 5 years ago

Tomas-Kraus, have you found a solution for your problem?

ARossi00 commented 5 years ago

Dear @Tomas-Kraus and @Express85 , I have the same problem . Have you found any solution for this problem ? My logged error is

src-resolve: impossibile risolvere il nome "ext:UBLExtensions" in un componente element declaration.

Thanks