kTest
kTest

Reputation: 367

Java Validate XML Schema with XSDs that import each other

I have the following xsd setup:

The real xsds cannot be modified and they are in use from years.

I am looking to validate an xml file against the xsd in Java.

Code (kotlin) looks like the following -

val factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI)
val schema = factory.newSchema(arrayOf(
   StreamSource(getResourceAsStream("validation.xsd")),
   StreamSource(getResourceAsStream("layout.xsd")),
   StreamSource(getResourceAsStream("root.xsd"))
))

The call to newSchema throws an error that it cannot resolve a type definition element declared in root.xsd while loading the first schema file validation.xsd. If I change the order of sources to first load root.xsd then I get the same error for a type declared in one of the other xsd files.

The only way to resolve this seems to be to include all of these in a single xsd file. This worked as expected but the xml file to be validated against these xsds has namespace declarations for the individual xsd files. Combining all of the xsds means that the namespace declarations on xml have to be updated. We are not allowed to make this update.

xmllint is able to successully run the validations

xmllint --schema  root.xsd ../sample.xml --noout

Any thoughts much appreciated.

Thanks

Upvotes: 1

Views: 16

Answers (0)

Related Questions