I'm using Python (2.7/3.8) and working with some complex XML's that are compared together. The order of the XML's can be different, and I'm building a function that acts as a rule for sorting (looking at node attributes, and then node children).
I've taken a look at a few different related questions, but neither are working for my scenario:
I'm able to sort using key=lambda child: child.tag, however I generally want to use the attributes rather than the tag name.
At it's most basic case, I want to be able to sort by attribute name, checking to see if any of ['id', 'label', 'value'] exist as attributes, and using that as the key. Regardless of that, I can't seem to figure out why child.tag works to sort, but child.get('id') does not.
import xml.etree.ElementTree as etree
input = '''
<root>
<node id="7"></node>
<node id="10"></node>
<node id="5"></node>
</root>
'''
root = etree.fromstring(input)
root[:] = sorted(root, key=lambda child: child.get('id'))
xmlstr = etree.tostring(root, encoding="utf-8", method="xml")
print(xmlstr.decode("utf-8"))
Which returns:
<root>
<node id="7" />
<node id="5" />
<node id="10" />
</root>
Expected:
<root>
<node id="5" />
<node id="7" />
<node id="10" />
</root>
EDIT
As deadshot mentioned, wrapping child.get('id') with int() does fix the issue, however the code has to additionally work for inputs that have both letters + numbers, for example id="node1", "node15", etc.
For example:
<root>
<node id="node10" />
<node id="node7" />
<node id="node5" />
</root>
Expected:
<root>
<node id="node5" />
<node id="node7" />
<node id="node10" />
</root>
id="node1", "node15"and expected output