Search code examples
javaxmlxsdxml-parsing

org.apache.ws.commons.schema.XmlSchemaException: /absolutepath/myadditionalschema.xsd (No such file or directory) when parts of schema are imported


I am using apache xmlschema-core 2.3.0 to parse xsd schema. The schema is in resources folder on the classpath.

The filePath is /myschemas/xsd/myschema.xsd and it points to classpath resource in resource folder. It has import to myadditionalschema.xsd. The additional schema is in the same place.

 try (InputStream inputStream = this.getClass().getResourceAsStream(filePath)) {
            XmlSchema schema = new XmlSchemaCollection().read(new StreamSource(inputStream));
            (...some business logic)
            return entries;
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            throw new IllegalStateException(e);
        }

As a result I am getting

org.apache.ws.commons.schema.XmlSchemaException: /home/user/myproject/mavenmodule/myadditionalschema.xsd (No such file or directory)  

The schema itself has import declaration:

 <xs:import schemaLocation="myadditionalschema.xsd"
              namespace="http://test"/>

So as u can see, schema parts that should be imported are not imported, and they are searched in root folder of my maven module instead classpath /myschemas/xsd/


Solution

  • Faced the same problem, try using setBaseUri(String baseUri) in XmlSchemaCollection, this helps me.