lxml preserves attributes order?



I was writing my aplication using minidom but minidom does not preserve attribute order(sorts alphabetically), so I decided to do it using lxml.


However in the following lines of code I'm not getting the desired order:



import lxml.etree as ET
SATNS = "link_1"
NS = "link_2"
location_attribute = '{%s}schemaLocation' % NS
root = ET.Element('{%s}Catalogo' % SATNS, nsmap={'catalogocuentas':SATNS}, attrib=
{location_attribute: 'http://ift.tt/VgOhhW'}, Ano="2014", Mes="02", TotalCtas="219", RFC="ALF040329CX6", Version="1.0")
print (ET.tostring(root, pretty_print=True))


This is what I'm expecting to get:



<catalogocuentas:Catalogo xmlns:catalogocuentas="link_1"
xmlns:xsi="link_2" xsi:schemaLocation="http://ift.tt/VgOhhW"
Ano="2014" Mes="02" TotalCtas="219" RFC="XXX010101XXX" Version="1.0">
</catalogocuentas:Catalogo>


Which is in the order that I filled in:



root=ET.element(...)


But I'm getting the next, that has no order:



<catalogocuentas:Catalogo xmlns:catalogocuentas="link_1"
xmlns:xsi="link_2" RFC="ALF040329CX6" Version="1.0"
Mes="02" xsi:schemaLocation="http://ift.tt/VgOhhW" Ano="2014" TotalCtas="219">
</catalogocuentas:Catalogo>


Is there a way to fix this problem?


Thanks in advance!!


No comments:

Post a Comment