1

I have two .xsd files in the same src/main/resource folder:

  1. outer.xsd
  2. inner.xsd

outer.xsd looks like this:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" 
           xmlns:tns="http://project.domain.com/model" 
           targetNamespace="http://project.domain.com/model"
           elementFormDefault="qualified" attributeFormDefault="unqualified">
  <xs:include schemaLocation="inner.xsd"/>
  <xs:element name="innerObject" type="tns:InnerObjectType"/>
</xs:schema>

and inner.xsd looks like this:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           xmlns:model="http://project.domain.com/model"
           targetNamespace="http://project.domain.com/model"
           elementFormDefault="qualified" attributeFormDefault="qualified" version="1.0">
  <xs:complexType name="InnerObjectType">
    <xs:sequence>
      <xs:element name="property1" type="xs:string"/>
      <xs:element name="property2" type="xs:string"/>
    </xs:sequence>
  <xs:complexType>
</xs:schema>

Now, in another project, I'm using outer.xsd to validate input XML files like so:

SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
try {
    ClassLoader classLoader = objectType.getClassLoader();
    InputStream stream = classLoader.getResourceAsStream(schemaPath);
    Source schemaSource = new StreamSource(stream);
    Schema schema = factory.newSchema(schemaSource);
    ...
} catch (SAXException exception) {...}

On the line where I call newSchema, I am getting the following exception:

src-resolve: Cannot resolve the name 'tns:InnerObjectType' to a(n) 'type definition' component.

It appears that this code is able to find the outer schema by path, but during the process of resolving the inner type, it cannot find the inner schema.

Anybody have a suggestion of how I can get it to resolve that inner type?

Note: I was able to use outer.xsd to generate an XML file in eclipse, so I am relatively certain that the structure of the .xsd files is correct ... it's just that whatever the context is when outer.xsd is being resolved doesn't have access to inner.xsd.

A parallel question is: How can I find out what the context is in which the type is being resolved? (ie ... where is it looking for inner.xsd?)

Eric
  • 1,414
  • 3
  • 16
  • 35
  • 1
    This question [Validate an XML File Against Multiple Schema Definitions](http://stackoverflow.com/questions/1094893/validate-an-xml-file-against-multiple-schema-definitions) might contain the answer. – Verhagen Oct 20 '15 at 20:56
  • 1
    You r using element of type `"tns:InnerObjectType"` in `outer.xsd` with `xmlns:tns="http://project.domain.com/model"`, but your `InnerObjectType` is defined in `inner.xsd` with `targetNamespace="http://project.domain.com/model/model"`, which is different. In the first case the prefix path ends with **model**, in the second case the targetNamespace path ends with **model/model**. – MockerTim Oct 20 '15 at 21:01
  • Thanks @MockerTim ... it turns out that was just a typo in my question and isn't represented in the code. I've fixed the typo in my question. – Eric Oct 27 '15 at 17:58
  • See my solution here: https://stackoverflow.com/a/59666291/2341765 – Shafiul Jan 09 '20 at 14:40

0 Answers0