title: Compare XML snippets?
body: <p>Building on <a href="https://stackoverflow.com/questions/794331/xml-comparison-in-c">another SO question</a>, how can one check whether two well-formed XML snippets are semantically equal.  All I need is "equal" or not, since I'm using this for unit tests.</p>
  
  <p>In the system I want, these would be equal (note the order of 'start'
  and 'end'):</p>
  
  <pre><code>&lt;?xml version='1.0' encoding='utf-8' standalone='yes'?&gt;
  &lt;Stats start="1275955200" end="1276041599"&gt;
  &lt;/Stats&gt;
  
  # Reordered start and end
  
  &lt;?xml version='1.0' encoding='utf-8' standalone='yes'?&gt;
  &lt;Stats end="1276041599" start="1275955200" &gt;
  &lt;/Stats&gt;
  </code></pre>
  
  <p>I have lmxl and other tools at my disposal, and a simple function that only allows reordering of attributes would work fine as well!  </p>
  
  <hr>
  
  <p>Working snippet based on IanB's answer:</p>
  
  <pre><code>from formencode.doctest_xml_compare import xml_compare
  # have to strip these or fromstring carps
  xml1 = """    &lt;?xml version='1.0' encoding='utf-8' standalone='yes'?&gt;
      &lt;Stats start="1275955200" end="1276041599"&gt;&lt;/Stats&gt;"""
  xml2 = """     &lt;?xml version='1.0' encoding='utf-8' standalone='yes'?&gt;
      &lt;Stats end="1276041599" start="1275955200"&gt;&lt;/Stats&gt;"""
  xml3 = """ &lt;?xml version='1.0' encoding='utf-8' standalone='yes'?&gt;
      &lt;Stats start="1275955200"&gt;&lt;/Stats&gt;"""
  
  from lxml import etree
  tree1 = etree.fromstring(xml1.strip())
  tree2 = etree.fromstring(xml2.strip())
  tree3 = etree.fromstring(xml3.strip())
  
  import sys
  reporter = lambda x: sys.stdout.write(x + "\n")
  
  assert xml_compare(tree1,tree2,reporter)
  assert xml_compare(tree1,tree3,reporter) is False
  </code></pre>