0

I am trying to validate an XML String against two Strings that contains an XSD. One XSD includes the other. I get the error: "Cannot resolve the name 'ServiceSpecificationSchema:ServiceIdentifier' to a(n) 'type definition' component."

It looks like, that my code doesnt recognize the second XSD file. Others solved that problem by using a LSResourceResolver ( seen here: How to validate an XML file using Java with an XSD having an include? )

But in that exampe the files are stored local. Is there a good way, that this method works with my XSD strings?

Any hint would be appreciated.

My code so far:

        SchemaFactory factory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");

        Schema schema = factory.newSchema(new SAXSource[]
                {
                        (new SAXSource(new InputSource(new StringReader(XSD)))),
                        (new SAXSource(new InputSource(new StringReader(XSD2))))
                });
        Validator validator = schema.newValidator();

        validator.validate(new StreamSource(new StringReader(inputXml)));
Simon
  • 925
  • 3
  • 13
  • 30

1 Answers1

-1

Finally i found a solution.

This works for me:

@Service
public class ResourceResolverImpl implements LSResourceResolver {
private ILoadFromSRService iLoadFromSRService;

@Autowired
public ResourceResolverImpl(ILoadFromSRService iLoadFromSRService){
    this.iLoadFromSRService = iLoadFromSRService;
}

public LSInput resolveResource(String type,
                               String namespaceURI,
                               String publicId,
                               String systemId,
                               String baseURI) {
    String string =iLoadFromSRService.getServiceBaseTypeSchema();
    string = string.replace("\n", "").replace("\t", "");
    InputStream resourceAsStream = new ByteArrayInputStream( string.getBytes());
    return new LSInputImpl(publicId, systemId, resourceAsStream);
    }
}
Simon
  • 925
  • 3
  • 13
  • 30