Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compare XML files

Tags:

diff

xml

I have two XML files (XSD) which are generated by some tool.
The tool doesn't preserve the order of elements so although the content is equal comparing it as text will result as the files are different.
Is there some tool that can sort the elements before comparing and will enable text comparison of the documents? Of course the sorting needs to be done recursively.

Data example:
File A:

<xml>
  <A/>
  <B/>
</xml>

File B:

<xml>
  <B/>
  <A/>
</xml>
like image 405
Avner Levy Avatar asked Aug 29 '12 10:08

Avner Levy


1 Answers

I had a similar problem and I eventually found: http://superuser.com/questions/79920/how-can-i-diff-two-xml-files

That post suggests doing a canonical XML sort then doing a diff. The following should work for you if you are on Linux, Mac, or if you have Windows with something like Cygwin installed:

$ xmllint --c14n FileA.xml > 1.xml
$ xmllint --c14n FileB.xml > 2.xml
$ diff 1.xml 2.xml
like image 185
James Oravec Avatar answered Oct 16 '22 12:10

James Oravec