问题
I know this question has been asked in the past, but they have all been dated a few years back. I am wondering if there has been any changes made to Python modules such as lxml, minidom, or etree that will allow us to preserve the attribute order in XML files without patching.
I need the order to be preserved as the program I am supplying the files to relies on it.
If there are no updates, what's the easiest way to implement this?
回答1:
The insignificance of attribute ordering is not a technical limitation to be lifted in future implementations of XML libraries.
Attribute order is insignificant per the XML Recommendation:
Note that the order of attribute specifications in a start-tag or empty-element tag is not significant.
Thus, you should not base your design upon any particular ordering of attributes in XML.
In the very rare circumstances where an application absolutely must order XML attributes, see the section on attribute processing in the XML Normalization Recommendation or the Canonical XML Recommendation.
来源:https://stackoverflow.com/questions/51234136/preserving-xml-attribute-order