Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

On Linux, how could I generate a diff between two XML files?

Ideally, I would like to be able configure it to some things strict, or loosen some things, like whitespace, or attribute order.

I'll often care that the files are functionally the same, but diff by itself, would be annoying to use, especially if the XML file doesn't have a lot of linebreaks.

For example, the following should really be okay to me:

<tag att1="one" att2="two">
  content
</tag>

<tag att2="two" att1="one">
  content
</tag>
share|improve this question

6 Answers 6

up vote 24 down vote accepted

One approach would be to first turn both XML files into Canonical XML, and compare the results using diff. For example, xmllint can be used to canonicalize XML.

$ xmllint --c14n one.xml > 1.xml
$ xmllint --c14n two.xml > 2.xml
$ diff 1.xml 2.xml
share|improve this answer
1  
Never knew about the --c14n switch in xmllint. That's handy. –  qedi Dec 10 '09 at 20:21
1  
You can do it in one line too vimdiff <(xmllint --c14n one.xml) <(xmllint --c14n two.xml) –  Nathan Villaescusa Mar 3 '13 at 1:53

Diffxml gets the basic functionality correct, though it doesn't seem to offer many options for configuration.

share|improve this answer
    
It's not quite there yet, but it looks promising at least. –  qedi Dec 7 '09 at 17:02

Tried to use @Jukka Matilainen's answer but had problems with white-space (one of the files was a huge one-liner). Using --format helps to skip white-space differences.

xmllint --format one.xml > 1.xml  
xmllint --format two.xml > 2.xml  
diff 1.xml 2.xml  

Note: Use vimdiff command for side-by-side comparison of the xmls.

share|improve this answer
    
In my case two.xml was generated from one.xml by a script. So I just needed to check what was added/removed by the script. –  GuruM Aug 8 '12 at 10:36

I use Beyond Compare to compare all types of text based files. They produce versions for Windows and Linux.

share|improve this answer
    
Plain text comparisons would say the two lines differed, whereas the OP wants them to be reported as the same. –  ChrisF Dec 7 '09 at 16:33
    
i.e. Canonically compare the XML. –  Chris W. Rea Dec 9 '09 at 20:08

SD Smart Differencer compares documents based on structure as opposed to actual layout.

There's an XML Smart Differencer. For XML, that means matching order of tags and content. It should note that the text string in the specific fragment you indicated was different. It presently doesn't understand the XML notion of tag attributes indicating whether whitespace is normalized vs. significant.

share|improve this answer

Jukka's answer did not work for me, but it did point to Canonical XML. Neither --c14n nor --c14n11 sorted the attributes, but i did find the --exc-c14n switch did sort the attributes. --exc-c14n is not listed in the man page, but described on the command line as "W3C exclusive canonical format".

$ xmllint --exc-c14n one.xml > 1.xml
$ xmllint --exc-c14n two.xml > 2.xml
$ diff 1.xml 2.xml

$ xmllint | grep c14
    --c14n : save in W3C canonical format v1.0 (with comments)
    --c14n11 : save in W3C canonical format v1.1 (with comments)
    --exc-c14n : save in W3C exclusive canonical format (with comments)

$ rpm -qf /usr/bin/xmllint
libxml2-2.7.6-14.el6.x86_64
libxml2-2.7.6-14.el6.i686

$ cat /etc/system-release
CentOS release 6.5 (Final)

Warning --exc-c14n strips out the xml header whereas the --c14n prepends the xml header if not there.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.