James A. Rosen James A. Rosen - 8 months ago 51
Ruby Question

How do I test XML equality in Ruby?

Clearly I need to (a) convert both strings to canonical XML or (b) compare their parse-trees. The following doesn't work because the document object returned doesn't have a sensible


Nokogiri.XML(doc_a) == Nokogiri.XML(doc_b)

Nor does the following, because Nokogiri's
leaves some internal whitespace:

Nokogiri.XML(doc_a).to_xml == Nokogiri.XML(doc_b).to_xml

This is a reasonable approximation of equality (and will work for most cases), but it's not quite right:

Nokogiri.XML(doc_a).to_xml.squeeze(' ') == Nokogiri.XML(doc_b).to_xml.squeeze(' ')

I'm already using Nokogiri, so I'd prefer to stick with it, but I'll use whatever library works.

sgt sgt

If you are looking for structural equality and don't care about the order of tags and attributes, probably the xml-simple library is a good choice. It converts the xml into ruby's data structures (hashes and lists) which can be safely compared with the == operator.