title: how to check lxml element tree strings?
body: <p>I have a list of lxml element trees. I would like to store in a dictionary the number of times a sub-tree appears in any of subtress of the list of trees. For example </p>
  
  <pre><code>tree1='''&lt;A attribute1="1"&gt;&lt;B&gt;&lt;C/&gt;&lt;/B&gt;&lt;/A&gt;'''
  tree2='''&lt;A attribute1="1"&gt;&lt;D&gt;&lt;C attribute="2"/&gt;&lt;/D&gt;&lt;/A&gt;'''
  tree3='''&lt;E attribute1="1"&gt;&lt;B&gt;&lt;C/&gt;&lt;/B&gt;&lt;/E&gt;'''
  list_trees=[tree1,tree2,tree3]
  print list_trees
  from collections import defaultdict
  from lxml import etree as ET
  mydict=defaultdict(int) 
  for tree in list_trees:
      root=ET.fromstring(tree)
      for sub_root in root.iter():
          print ET.tostring(sub_root)
          mydict[ET.tostring(sub_root)]+=1
  print mydict
  </code></pre>
  
  <p>I get the following correct result:</p>
  
  <pre><code>defaultdict(&lt;type 'int'&gt;, {'&lt;E attribute1="1"&gt;&lt;B&gt;&lt;C/&gt;&lt;/B&gt;&lt;/E&gt;': 1, '&lt;C/&gt;': 2, '&lt;A attribute1="1"&gt;&lt;D&gt;&lt;C attribute="2"/&gt;&lt;/D&gt;&lt;/A&gt;': 1, '&lt;B&gt;&lt;C/&gt;&lt;/B&gt;': 2, '&lt;C attribute="2"/&gt;': 1, '&lt;D&gt;&lt;C attribute="2"/&gt;&lt;/D&gt;': 1, '&lt;A attribute1="1"&gt;&lt;B&gt;&lt;C/&gt;&lt;/B&gt;&lt;/A&gt;': 1})
  </code></pre>
  
  <p>This only works in this particular example. However, In the general case, xmls can be identical but have different ordering of attributes, or extra white spaces or new lines that don't matter. However, this general case will break my system. I know that there have been posts about how to check 2 identical xml trees, however, i would like to convert the xmls into strings in order to do this particular application described above (easily keeping unique trees as string allows for easy comparisons and more flexibility in the future) and also be able to store it in sql nicely. How can an xml be made into a string in a consistent matter, regardless of orderings, or extra spaces, extra lines?</p>
  
  <p>editing for giving the case that does not work:
  These 3 xml trees are identical, they just have different ordering of attributes or extra spaces or new lines.</p>
  
  <pre><code>tree4='''&lt;A attribute1="1" attribute2="2"&gt;&lt;B&gt;&lt;C/&gt;&lt;/B&gt;&lt;/A&gt;'''
  tree5='''&lt;A attribute1="1"     attribute2="2"   &gt;
  &lt;B&gt;&lt;C/&gt;&lt;/B&gt;&lt;/A&gt;'''
  tree6='''&lt;A attribute2="2" attribute1="1"&gt;&lt;B&gt;&lt;C/&gt;&lt;/B&gt;&lt;/A&gt;'''
  </code></pre>
  
  <p>My output gives the following:</p>
  
  <pre><code>defaultdict(&lt;type 'int'&gt;, {'&lt;B&gt;&lt;C/&gt;&lt;/B&gt;': 3, '&lt;A attribute1="1" attribute2="2"&gt;&lt;B&gt;&lt;C/&gt;&lt;/B&gt;&lt;/A&gt;': 1, '&lt;A attribute1="1" attribute2="2"&gt;\n&lt;B&gt;&lt;C/&gt;&lt;/B&gt;&lt;/A&gt;': 1, '&lt;C/&gt;': 3, '&lt;A attribute2="2" attribute1="1"&gt;&lt;B&gt;&lt;C/&gt;&lt;/B&gt;&lt;/A&gt;': 1})
  </code></pre>
  
  <p>However, the output should be:</p>
  
  <pre><code>defaultdict(&lt;type 'int'&gt;, {'&lt;B&gt;&lt;C/&gt;&lt;/B&gt;': 3, '&lt;A attribute1="1" attribute2="2"&gt;&lt;B&gt;&lt;C/&gt;&lt;/B&gt;&lt;/A&gt;': 3, '&lt;C/&gt;': 3})
  </code></pre>