Comparing XML documents

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.

On Sat, Nov 20, 2010 at 7:17 PM, Toby R. [email protected] 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.

Maybe using GitHub - flavorjones/lorax: XML/HTML diff generator, based on Nokogiri.

On Sat, Nov 20, 2010 at 5:17 AM, Toby R. [email protected] 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.

require ‘active_support’
require ‘active_support/xml_mini’
require ‘active_support/core_ext/hash’

doc1 = Hash.from_xml <<END

1
2

END

doc2 = Hash.from_xml <<END

1
2

END

assert_equal doc1, doc2

#=> pass

doc3 = Hash.from_xml <<END

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}
end
end

assert_equal doc1, foo(doc3)

#=> true

http://www.eggheadcafe.com/software/aspnet/36146538/compare-2-xml-files-with-libxmlruby.aspx

— On Sat, 11/20/10, Michael F. [email protected] wrote:

From: Michael F. [email protected]
Subject: Re: Comparing XML documents
To: “ruby-talk ML” [email protected]
Date: Saturday, November 20, 2010, 6:47 AM

On Sat, Nov 20, 2010 at 7:17 PM, Toby R. [email protected] 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.

Maybe using GitHub - flavorjones/lorax: XML/HTML diff generator, based on Nokogiri.

On 20.11.2010 22:39, [email protected] wrote:

On Sat, Nov 20, 2010 at 5:17 AM, Toby R.[email protected] 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.

doc3 = Hash.from_xml<<END
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