Discussion:
Comparing two XML documents
Matt Wynne
2011-01-26 15:26:54 UTC
Permalink
I have a problem. I have a test that needs to assert that one XML document looks exactly like another XML document.

I couldn't find a way to compare them as Nokogiri::XML::Documents, so I converted them both to strings with #to_xml and compared the strings.

Trouble is, sometimes the attributes one of the nodes are rendered into the string in a different order to the other one, so you get an error of the form:

Diff:
@@ -5,7 +5,7 @@
<Description/>
<Context>
<BackgroundLabels>
- <Label mode="FILTER" name="included"/>
+ <Label name="included" mode="FILTER"/>
</BackgroundLabels>
</Context>
<LookAndFeel nonprofit="false"/>
(RSpec::Expectations::ExpectationNotMetError)

Most annoying.

I've seen a couple of RSpec matchers in blog posts that walk and compare XML fragments but I'm surprised there isn't something more concrete that already exists. I feel like I'm missing a way to do it within an XML library, for example.

Any clues out there?

cheers,
Matt

matt-***@public.gmane.org
07974 430184
Kam Dahlin
2011-01-26 19:19:45 UTC
Permalink
Aaron Patterson (author of Nokogiri) addressed this issue directly here: http://rubyforge.org/pipermail/nokogiri-talk/2009-April/000258.html
I don't think document comparison was ever implemented in Nokogiri though.

Cheers,

kam
Post by Matt Wynne
I have a problem. I have a test that needs to assert that one XML document looks exactly like another XML document.
I couldn't find a way to compare them as Nokogiri::XML::Documents, so I converted them both to strings with #to_xml and compared the strings.
@@ -5,7 +5,7 @@
<Description/>
<Context>
<BackgroundLabels>
- <Label mode="FILTER" name="included"/>
+ <Label name="included" mode="FILTER"/>
</BackgroundLabels>
</Context>
<LookAndFeel nonprofit="false"/>
(RSpec::Expectations::ExpectationNotMetError)
Most annoying.
I've seen a couple of RSpec matchers in blog posts that walk and compare XML fragments but I'm surprised there isn't something more concrete that already exists. I feel like I'm missing a way to do it within an XML library, for example.
Any clues out there?
cheers,
Matt
07974 430184
_______________________________________________
rspec-users mailing list
http://rubyforge.org/mailman/listinfo/rspec-users
Pat Maddox
2011-01-31 08:26:51 UTC
Permalink
I load my XML docs into a hash using Hash#from_xml and then compare the hashes.
Post by Matt Wynne
I have a problem. I have a test that needs to assert that one XML document looks exactly like another XML document.
I couldn't find a way to compare them as Nokogiri::XML::Documents, so I converted them both to strings with #to_xml and compared the strings.
@@ -5,7 +5,7 @@
<Description/>
<Context>
<BackgroundLabels>
- <Label mode="FILTER" name="included"/>
+ <Label name="included" mode="FILTER"/>
</BackgroundLabels>
</Context>
<LookAndFeel nonprofit="false"/>
(RSpec::Expectations::ExpectationNotMetError)
Most annoying.
I've seen a couple of RSpec matchers in blog posts that walk and compare XML fragments but I'm surprised there isn't something more concrete that already exists. I feel like I'm missing a way to do it within an XML library, for example.
Any clues out there?
cheers,
Matt
07974 430184
_______________________________________________
rspec-users mailing list
http://rubyforge.org/mailman/listinfo/rspec-users
Matt Wynne
2011-01-31 22:16:33 UTC
Permalink
Post by Pat Maddox
I load my XML docs into a hash using Hash#from_xml and then compare the hashes.
Bullseye! Thanks Pat.
Post by Pat Maddox
Post by Matt Wynne
I have a problem. I have a test that needs to assert that one XML document looks exactly like another XML document.
I couldn't find a way to compare them as Nokogiri::XML::Documents, so I converted them both to strings with #to_xml and compared the strings.
@@ -5,7 +5,7 @@
<Description/>
<Context>
<BackgroundLabels>
- <Label mode="FILTER" name="included"/>
+ <Label name="included" mode="FILTER"/>
</BackgroundLabels>
</Context>
<LookAndFeel nonprofit="false"/>
(RSpec::Expectations::ExpectationNotMetError)
Most annoying.
I've seen a couple of RSpec matchers in blog posts that walk and compare XML fragments but I'm surprised there isn't something more concrete that already exists. I feel like I'm missing a way to do it within an XML library, for example.
Any clues out there?
cheers,
Matt
07974 430184
_______________________________________________
rspec-users mailing list
http://rubyforge.org/mailman/listinfo/rspec-users
_______________________________________________
rspec-users mailing list
http://rubyforge.org/mailman/listinfo/rspec-users
cheers,
Matt

matt-***@public.gmane.org
07974 430184

Continue reading on narkive:
Loading...