XML validation with loading schemas at runtime, failure depending on schema order

I am trying to perform XML validation. I am provided with a list of diagrams at runtime (possibly wrapped in a jar). Validation fails or fails based on the order in which I provide the schemas in SchemaFactory.

That's what I'm doing:

private void validateXml(String xml, List<URI> schemas){ Source[] source = new StreamSource[schemas.size()]; int i=0; for (URI f : schemas){ source[i++] = new StreamSource(f.openStream()); } SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NA_URI); sf.setResourceResolver(new MyClassPathResourceResolver()); Schema schema = schemaFactory.newSchema(source); Validator validator = schema.newValidator(); validator.validate(new StreamSource(new ByteArrayInputStream(xml.getBytes())); 

again, this fails if the transferred set of schemas does not start with the schema to which the xml referrs root element belongs. Is there a fix for this, or am I doing something wrong?

+6
source share
2 answers

By default, Xerces will ignore the schema document if it already has a schema document for the same namespace. This behavior can be changed using the factory option.

http://apache.org/xml/features/validation/schema/handle-multiple-imports

+5
source

First, you must install an instance of the org.xml.sax.ErrorHandler object on an XML reader by calling the registerErrorHandler () method. You may receive warnings to help you understand the problem.

Secondly, you should know which xml library you are using. Call schemaFactory.getClass (). GetName () in your code and print it. After you know the library, you can refer to your documentation if it supports the function of turning on / off the import of several schemes.

0
source

Source: https://habr.com/ru/post/922315/


All Articles