Skip to content

Instantly share code, notes, and snippets.

@musketyr
Last active Aug 29, 2015
Embed
What would you like to do?
Merge XML Schemas
println "Inlining schema COSDCOSD_XMLSchema-v5-0-4r.xsd"
File newOne = inlineSchema('COSDCOSD_XMLSchema-v5-0-4r.xsd')
println "Inlined schema COSDCOSD_XMLSchema-v5-0-4r.xsd to $newOne"
File inlineSchema(String schemaLocation) {
saveNodeToFile(inlineIncludes(schemaLocation), schemaLocation.replace('.xsd', '.all.xsd'))
}
Node inlineIncludes(String xsdFileName, Set<String> included = []) {
File file = new File(xsdFileName)
XmlParser parser = new XmlParser()
Node schema = parser.parse(file)
Node destination = new Node(null, schema.name(), schema.attributes())
for (Node child in schema.children()) {
if (child.name() == 'xs:include' && !(child.@schemaLocation in included)) {
included << child.@schemaLocation
Node otherSchema = inlineIncludes(child.@schemaLocation, included)
otherSchema.children().each { Node toBeIncluded ->
destination.append(toBeIncluded)
}
} else {
destination.append(child)
}
}
destination
}
File saveNodeToFile(Node node, String destination) {
File file = new File(destination)
file.withWriter {
new XmlNodePrinter(new PrintWriter(it)).print(node)
}
file
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment