Python ElementTree 模块:使用“find"、“findall"方法时如何忽略 XML 文件的命名空间来定位匹配元素

2022-01-14 00:00:00 python elementtree find namespaces findall

问题描述

我想使用findall"的方法在ElementTree模块中定位源xml文件的一些元素.

I want to use the method of "findall" to locate some elements of the source xml file in the ElementTree module.

但是,源 xml 文件 (test.xml) 具有命名空间.我将 xml 文件的一部分截断为示例:

However, the source xml file (test.xml) has namespace. I truncate part of xml file as sample:

<?xml version="1.0" encoding="iso-8859-1"?>
<XML_HEADER xmlns="http://www.test.com">
    <TYPE>Updates</TYPE>
    <DATE>9/26/2012 10:30:34 AM</DATE>
    <COPYRIGHT_NOTICE>All Rights Reserved.</COPYRIGHT_NOTICE>
    <LICENSE>newlicense.htm</LICENSE>
    <DEAL_LEVEL>
        <PAID_OFF>N</PAID_OFF>
        </DEAL_LEVEL>
</XML_HEADER>

示例python代码如下:

The sample python code is below:

from xml.etree import ElementTree as ET
tree = ET.parse(r"test.xml")
el1 = tree.findall("DEAL_LEVEL/PAID_OFF") # Return None
el2 = tree.findall("{http://www.test.com}DEAL_LEVEL/{http://www.test.com}PAID_OFF") # Return <Element '{http://www.test.com}DEAL_LEVEL/PAID_OFF' at 0xb78b90>

虽然可以,但是因为有命名空间{http://www.test.com}",所以在每个标签前面加命名空间很不方便.

Although it can works, because there is a namespace "{http://www.test.com}", it's very inconvenient to add a namespace in front of each tag.

使用find"、findall"等方法时如何忽略命名空间?

How can I ignore the namespace when using the method of "find", "findall" and so on?


解决方案

最好先解析XML文档,然后再修改结果中的标签,而不是修改XML文档本身.这样你就可以处理多个命名空间和命名空间别名:

Instead of modifying the XML document itself, it's best to parse it and then modify the tags in the result. This way you can handle multiple namespaces and namespace aliases:

from io import StringIO  # for Python 2 import from StringIO instead
import xml.etree.ElementTree as ET

# instead of ET.fromstring(xml)
it = ET.iterparse(StringIO(xml))
for _, el in it:
    prefix, has_namespace, postfix = el.tag.partition('}')
    if has_namespace:
        el.tag = postfix  # strip all namespaces
root = it.root

这是基于这里的讨论:http://bugs.python.org/issue18304

更新: rpartition 而不是 partition 确保您在 postfix 中获得标签名称,即使有没有命名空间.因此,您可以将其浓缩:

Update: rpartition instead of partition makes sure you get the tag name in postfix even if there is no namespace. Thus you could condense it:

for _, el in it:
    _, _, el.tag = el.tag.rpartition('}') # strip ns

相关文章