使用lxml基于属性值

时间:2016-03-15 17:54:07

标签: python xml sorting lxml

我试图根据属性值对文档中的一些子元素进行排序,虽然实际的排序函数似乎正在工作,但新近排序的元素的拼接似乎并非如此。

from lxml import etree

def getkey(elem):
    # Used for sorting elements by @LIN.
    # returns a tuple of ints from the exploded @LIN value
    # '1.0' -> (1,0)
    # '1.0.1' -> (1,0,1)
    return tuple([int(x) for x in elem.get('LIN').split('.')])

xml_str = """<Interface>
                <Header></Header>
                <PurchaseOrder>
                    <LineItems>
                        <Line LIN="2.0"></Line>
                        <Line LIN="3.0"></Line>
                        <Line LIN="1.0"></Line>
                    </LineItems>
                </PurchaseOrder>
            </Interface>"""

root = etree.fromstring(xml_str)
lines = root.findall("PurchaseOrder/LineItems/Line")
lines[:] = sorted(lines, key=getkey)
res_lines = [x.get('LIN') for x in lines]
print res_lines

print etree.tostring(root, pretty_print=True)

当我执行上面的代码时,我会看到lines列表在打印['1.0', '2.0', '3.0']时排序正确。但是,由于tostring()打印出以下内容,因此XML树不会更新。

<Interface>
  <Header/>
  <PurchaseOrder>
    <LineItems>
      <Line LIN="2.0"/>
      <Line LIN="3.0"/>
      <Line LIN="1.0"/>
    </LineItems>
  </PurchaseOrder>
</Interface>

我知道了如何从http://effbot.org/zone/element-sort.htm进行排序,它说拼接应该是我需要更新元素顺序的所有内容,但似乎确实如此。我意识到lxml与elementtree不是100%兼容,所以作为一个完整性检查我用elementtree替换了lxml导入并获得了完全相同的结果。

1 个答案:

答案 0 :(得分:7)

这将对输出进行排序和写入:

angular.module('app').directive('tabNext', function () {
return {
    restrict: 'A',
    link: function (scope, elem) {

        elem.bind('keyup', function (e) {
            var code = e.keyCode || e.which;
            if (code === 13) {
                e.preventDefault();
                var eIDX = -1;
                for (var i = 0; i < this.form.elements.length; i++) {
                    if (elem.eq(this.form.elements[i])) {
                         eIDX = i;
                         break;
                    }
                }
                if (eIDX === -1) {
                    return;
                }
                var j = eIDX + 1;
                var theform = this.form;
                while (j !== eIDX) {
                    if (j >= theform.elements.length){
                        j = 0;
                    }
                    if ((theform.elements[j].type !== "hidden") && (theform.elements[j].type !== "file")
                            && (theform.elements[j].name !== theform.elements[eIDX].name) 
                            && (! theform.elements[j].disabled) 
                            && (theform.elements[j].tabIndex >= 0)) {
                        if (theform.elements[j].type === "select-one") {
                            theform.elements[j].focus();
                        } else if (theform.elements[j].type === "button") {
                            theform.elements[j].focus();
                        } else {
                            theform.elements[j].focus();
                            theform.elements[j].select();
                        }
                        return;
                        break;
                    }
                    j++;
                }
            }
        });
    }
}});

或使用您自己的代码进行打印:

import xml.etree.ElementTree as ET

tree = ET.parse("in.xml")

def getkey(elem):
    # Used for sorting elements by @LIN.
    # returns a tuple of ints from the exploded @LIN value
    # '1.0' -> (1,0)
    # '1.0.1' -> (1,0,1)
    return float(elem.get('LIN'))

container = tree.find("PurchaseOrder/LineItems")

container[:] = sorted(container, key=getkey)

tree.write("new.xml")

输出:

import xml.etree.ElementTree as ET

tree = ET.fromstring(xml_str)

def getkey(elem):
    # Used for sorting elements by @LIN.
    # returns a tuple of ints from the exploded @LIN value
    # '1.0' -> (1,0)
    # '1.0.1' -> (1,0,1)
    return float(elem.get('LIN'))

root = etree.fromstring(xml_str)
lines = root.find("PurchaseOrder/LineItems")
lines[:] = sorted(lines, key=getkey)

密钥为In [12]: print (etree.tostring(root, pretty_print=True)) <Interface> <Header/> <PurchaseOrder> <LineItems> <Line LIN="1.0"/> <Line LIN="2.0"/> <Line LIN="3.0"/> </LineItems> </PurchaseOrder> </Interface> ,您要查找root.find("PurchaseOrder/LineItems")元素并对其进行排序。