Forum Discussion
weinmang
16 years agoNew Contributor
Specific issues with multiple imported schemas
Groovy defaults to using the Sun internal XMLSchemaFactory. That factory is not as
full featured as the current implementation in Xerces 2.10. Please use the Xerces
XMLSchemaFactory an NOT the javax SchemaFactory.
Multiple schemas must be stacked in reverse order. For Example:
Schema a.xsd imports schema b.xsd
Schema b.xsd imports schema c.xst
Create a Source array with new StreamSource for c.xsd
followed by new StreamSource for b.xsd
followed by new StreamSource for a.xsd
Scenario:
Result schema imports profile schema and common schema
Profile schema imports page schema and common schema
Page schema imports app schema and common schema
App schema imports common schema
Working example:
def resultFIS = new StreamSource(new FileInputStream(resultXSD))
def profileFIS = new StreamSource(new FileInputStream(profileXSD))
def pageFIS = new StreamSource(new FileInputStream(pageXSD))
def appFIS = new StreamSource(new FileInputStream(appXSD))
def commonFIS = new StreamSource(new FileInputStream(commonXSD))
def schemaFiles = [commonFIS, appFIS, pageFIS, profileFIS, resultFIS]
def factory = XMLSchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI)
def schema = factory.newSchema((Source[])schemaFiles.toArray())
def validator = schema.newValidator()
validator.validate(new StreamSource(new FileInputStream(resultXML)))
Groovy defaults to using the Sun internal XMLSchemaFactory. That factory is not as
full featured as the current implementation in Xerces 2.10. Please use the Xerces
XMLSchemaFactory an NOT the javax SchemaFactory.
Multiple schemas must be stacked in reverse order. For Example:
Schema a.xsd imports schema b.xsd
Schema b.xsd imports schema c.xst
Create a Source array with new StreamSource for c.xsd
followed by new StreamSource for b.xsd
followed by new StreamSource for a.xsd
Scenario:
Result schema imports profile schema and common schema
Profile schema imports page schema and common schema
Page schema imports app schema and common schema
App schema imports common schema
Working example:
def resultFIS = new StreamSource(new FileInputStream(resultXSD))
def profileFIS = new StreamSource(new FileInputStream(profileXSD))
def pageFIS = new StreamSource(new FileInputStream(pageXSD))
def appFIS = new StreamSource(new FileInputStream(appXSD))
def commonFIS = new StreamSource(new FileInputStream(commonXSD))
def schemaFiles = [commonFIS, appFIS, pageFIS, profileFIS, resultFIS]
def factory = XMLSchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI)
def schema = factory.newSchema((Source[])schemaFiles.toArray())
def validator = schema.newValidator()
validator.validate(new StreamSource(new FileInputStream(resultXML)))