From: Robert Klemme Date: 2010-11-22T00:15:34+09:00 Subject: Re: Comparing XML documents On 20.11.2010 22:39, brabuhr@gmail.com wrote: > On Sat, Nov 20, 2010 at 5:17 AM, Toby Rodwell wrote: >> I'd like to check the equivalence of two XML documents. Searching this >> forum I found some short and elegant code, but it considered docs >> unequal if their elements were in a different order. Any >> suggestions/advice? Thanks in advance. From an XML point of view documents with different child ordering are not equivalent. So you will not be likely to find standard XML tools that do the comparison - at least not without further adjusting (e.g. ordering elements before feeding them to the comparison or converting them to something else as were suggested. > require 'active_support' > require 'active_support/xml_mini' > require 'active_support/core_ext/hash' > > doc1 = Hash.from_xml< > 1 > 2 > > END > > doc2 = Hash.from_xml< > 1 > 2 > > END > > # assert_equal doc1, doc2 > #=> pass > > doc3 = Hash.from_xml< > 2 > 1 > > END > > # assert_equal doc1, doc3 > #=> fail > > def foo thing > case thing > when Hash > thing.each{|_, t| foo t} > when Array > thing.sort! > thing.each{|t| foo t} I would rather use thing.sort.each{|t| foo t} to avoid side effects from the test. Tests are generally considered read only and if they have side effects you'll run into weird issues that will be hard to hunt down. > end > end > > # assert_equal doc1, foo(doc3) > #=> true > Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/