Is there an open source command-line tool (for Linux) to diff XML files which ignores the element order?
Example input file a.xml
:
First your XML examples are not valid, because they lack a root element. I added a root element. This is a.xml
:
And this is b.xml
:
You can create a canonical form for the comparison by merging the siblings with the same name attribute and sorting by the tag name and the value.
In order to merge the sibling elements with the same name you have to ignore the elements which name is the same like a preceding sibling and take the remaining. This can be done on the second element level by the following Xpath:
*[not(@name = preceding-sibling::*/@name)]
You have to take the name of those elements in order to select all the child elements which have a parent with this name. After that you have to sort by name and value. This makes it possible to transform both files into this canonical form:
This will do the transformation: