How to "canonicalize" arbitrary xml (by reordering all attributes and elements)

I have code that generates a * .xsd file from a set of jaxb-annotated classes:

JAXBContext context = //build from set of classes
final DOMResult result = new DOMResult(); //will hold xsd output
context.generateSchema(new SchemaOutputResolver() {
    @Override
    public Result createOutput(String namespaceUri, String suggestedFileName) throws IOException {
       return result;
    }
});
Document doc = result.getNode();
OutputFormat format = new OutputFormat(doc);
format.setIndenting(true);
StringWriter writer = new StringWriter();
XMLSerializer serializer = new XMLSerializer(writer, format);
serializer.serialize(doc);
String xsd = writer.toString();

the problem is that it xsdproduces (stored in xsd) in a random order - 2 works with the same input, can generate logically identical xsds, but in different orders of elements, which leads to chaos with diff tools when writing it to a file.

How do I "canonicalize" xml inside xsd?

I saw some other xslt links in related questions, but all I saw required listing the elements in advance. im looking for something that works on any xml input.

+4
1

. , XSD. , ; , ( ), .

, , , XML.

, "" , ( -, -, -,...) XSLT .

( , , ), ( XSLT, , - ).

, ( xsd:choice , , , ,...). , , . , , XSD XSLT ( XML).

, , , ; XSLT .

+2

All Articles