Last active
August 29, 2015 14:06
-
-
Save musketyr/b7fb235b9623c6a827d4 to your computer and use it in GitHub Desktop.
Merge XML Schemas
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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