I am trying to sort the xml based on code in the field tag.
Below is the code implementation I tried but it is printing only one field
Sorting should be mainly done on code attribute
import xml.etree.ElementTree as ET
xmlData = '''<data>
<values>
<field code="6">rst</field>
<show>qwerty6</show>
<chip/>
<normal/>
</values>
<values>
<field code="5">opq</field>
<show>qwerty5</show>
<chip/>
<normal/>
</values>
<values>
<field code="4">klm</field>
<show>qwerty4</show>
</values>
<values>
<field code="3">hij</field>
<show>qwerty3</show>
<chip/>
<normal/>
</values>
<values>
<field code="2">efg</field>
<show>qwerty2</show>
<chip/>
<normal/>
</values>
<values>
<field code="1">abc</field>
<show>qwerty1</show>
</values>
</data>'''
tree = ET.fromstring(xmlData)
myvalues = tree.find('values')
for el in myvalues:
el[:] = sorted(el, key=lambda e: (e.tag, e.attrib['code']))
tree[:] = myvalues
xmlstr = ET.tostring(tree, encoding="utf-8", method="xml")
print(xmlstr.decode("utf-8"))