I've spend a lot of time to validate XML
against multiple XSD
in Spring. Even when I give all XSD
schemas to SchemaFactory it does not work because main schema can't see import schema
declared in main XSD file. Even when I give this schemas as files it does not work, because Spring's
resource files can't be resolved to absolute path
.
<xs:import namespace="http://test.com/types" schemaLocation="types.xsd"/>
implementation("org.apache.ws.xmlschema:xmlschema-core:2.2.4")
XSD's
(it will automatically find other files if there this schemaLocation="..."
), another for our Validator
: @Bean
fun schema(): XsdSchemaCollection {
return CommonsXsdSchemaCollection(
ClassPathResource("xsd/main.xsd")
).also { it.setInline(true) }
}
@Bean
fun myValidator(schema: XsdSchemaCollection): XmlValidator {
return schema.createValidator()
}
@Autowired
private val myValidator: XmlValidator
fun validate(data: String): Array<SAXParseException> {
return myValidator.validate(StreamSource(data.byteInputStream()))
}
Array<SAXParseException>
will contain list of validation exceptions if any of course
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.