Elementtree设置属性顺序 [英] Elementtree setting attribute order

查看:512
本文介绍了Elementtree设置属性顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写python脚本来标准化用于配置网站和网站表单的通用XML文件。但是,要做到这一点,我想要么保持元素的原始属性顺序,要么更好地能够以预定义的方式重新排列它们。目前,我尝试将大多数XML解析器的属性顺序重写为字母数字。由于这些XML文件是人工读取/编写和维护的,因此它不太有用。

I am trying to write a python script to standardise generic XML files, used to configure websites and website forms. However to do this I would like to either maintain the original attribute ordering of the elements, or even better be able to rearrange them in a pre-defined way. Currently most xml parsers I have tried re-write the attribute order to be alpha-numeric. As these XML files are human read/written and maintained, this isn't too useful.

例如,XML中的通用元素可能看起来像这样;

For example a generic element may look like this in the XML;

<Question QuestionRef="XXXXX" DataType="Integer" Text="Question Text" Availability="Shown" DefaultAnswer="X">

但是,一旦通过elementtree并重写为新文件,它将更改为:

However once passed through elementtree and re-written to a new file this is changed to:

<Question Availability="Shown" DataType="Integer" DefaultAnswer="X" PartType="X" QuestionRef="XXXXX" Text="Question Text">

该脚本的目的是标准化大量XML文件,以提高之间的可读性同事以及元素属性中包含的信息具有不同的重要性级别(例如,QuestionRef非常重要),表明需要合理地对属性进行排序。

As the aim of the script is to standardise a large number of XML files in order to increase readability between colleagues and that the information contained within the element's attributes have varying levels of significance (Eg. QuestionRef is highly important), dicates that attributes need to be sensibly ordered.

我了解python字典(存储在其中的属性)自然是无序的,XML规范指出属性的排序无关紧要,但这是人类可读性的因素是其背后的驱动力剧本。

I understand that python dicts (which attributes are stored in) are naturally unordered and XML specification states attribute ordering is insignificant, but this the human readability factor is the driving force behind the script.

在与此类似的其他问题(关于堆栈溢出)中,我看到它指出pxdom可以做到这一点(问题链接:链接),但是我找不到关于如何执行此操作的任何提及pxdom文档或使用Google搜索。那么,有什么方法可以维护属性的顺序或使用当前的XML解析器对其进行定义?最好不要诉诸hotpatching:)!

In other questions (on Stack Overflow) similar to this one I have seen it remarked that pxdom can do this (question link: link), but I cannot find any mention of how it may to do this in pxdom documentation or using a google search. So is there some way to maintain an order of attributes or define it with current XML parsers? Preferably without resorting to hotpatching :)!

任何人都可以提供的帮助将不胜感激:)。

Any help anyone can provide would be greatly appreciated :).

推荐答案

按如下所述应用猴子补丁::

ElementTree.py 文件中,有一个名为的函数 _serialize_xml ;

在此函数中;应用以下提到的补丁;

Apply monkey patch as mentioned below::
in ElementTree.py file, there is a function named as _serialize_xml;
in this function; apply the below mentioned patch;

        ##for k, v in sorted(items):  # remove the sorted here
        for k, v in items:
            if isinstance(k, QName):
                k = k.text
            if isinstance(v, QName):
                v = qnames[v.text]
            else:
                v = _escape_attrib(v, encoding)
            write(" %s=\"%s\"" % (qnames[k], v))

此处;删除 sorted(items),使其像我在上面所做的那样,只是 items



也可以禁用基于名称空间的排序(因为在上述补丁中;当xml属性存在名称空间时,排序仍然存在;否则,如果不存在名称空间,则上述方法有效精细);因此,将所有 {} 替换为 ElementTree中的 collections.OrderedDict()。 py


here; remove the sorted(items) and make it just items like i have done above.

Also to disable sorting based on namespace(because in above patch; sorting is still present when namespace is present for xml attribute; otherwise if namespace is not present; then above is working fine); so to do that, replace all {} with collections.OrderedDict() from ElementTree.py

现在,将所有属性添加到该xml元素中后,您便拥有了一个顺序中的所有属性。


在执行以上所有操作之前;阅读在 ElementTree.py

Now you have all attributes in a order as you have added them to that xml element.

Before doing all of above; read the copyright message by Fredrik Lundh that is present in ElementTree.py

这篇关于Elementtree设置属性顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆