python - 在 Python 中使用 ElementTree 更改命名空间前缀

标签 python xml namespaces elementtree

默认情况下,当您调用 ElementTree.parse(someXMLfile) 时,Python ElementTree 库会在每个解析的节点前面加上 Clark 表示法中的命名空间 URI:

    {http://example.org/namespace/spec}mynode

This makes accessing specific nodes by name a huge pain later in the code.

I've read through the docs on ElementTree and namespaces and it looks like the iterparse() function should allow me to alter the way the parser prefixes namespaces, but for the life of me I can't actually make it change the prefix. It seems like that may happen in the background before the ns-start event even fires as in this example:

for event, elem in iterparse(source):
    if event == "start-ns":
        namespaces.append(elem)
    elif event == "end-ns":
        namespaces.pop()
    else:
        ...

如何让它改变前缀行为以及函数结束时返回的正确内容是什么?

最佳答案

您不需要特别使用 iterparse。取而代之的是以下脚本:

from cStringIO import StringIO
import xml.etree.ElementTree as ET

NS_MAP = {
    'http://www.red-dove.com/ns/abc' : 'rdc',
    'http://www.adobe.com/2006/mxml' : 'mx',
    'http://www.red-dove.com/ns/def' : 'oth',
}

DATA = '''<?xml version="1.0" encoding="utf-8"?>
<rdc:container xmlns:mx="http://www.adobe.com/2006/mxml"
                 xmlns:rdc="http://www.red-dove.com/ns/abc"
                 xmlns:oth="http://www.red-dove.com/ns/def">
  <mx:Style>
    <oth:style1/>
  </mx:Style>
  <mx:Style>
    <oth:style2/>
  </mx:Style>
  <mx:Style>
    <oth:style3/>
  </mx:Style>
</rdc:container>'''

tree = ET.parse(StringIO(DATA))
some_node = tree.getroot().getchildren()[1]
print ET.fixtag(some_node.tag, NS_MAP)
some_node = some_node.getchildren()[0]
print ET.fixtag(some_node.tag, NS_MAP)

生产

('mx:Style', None)
('oth:style2', None)

这显示了如何访问已解析树中各个节点的完全限定标签名称。您应该能够根据您的特定需求进行调整。

关于python - 在 Python 中使用 ElementTree 更改命名空间前缀,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1249876/

相关文章:

python - ir.attachment在不同型号可以查看不同方式

XSLT 1.0 添加命名空间 + xsi :type to element

javascript - 覆盖 Javascript 函数中的回调

由于引号,SQL to XML 无法创建正确的 XMLNAMESPACE(我认为)

python 2.7 文件困难

python - 锯齿波交易错误 : "Tried to set unauthorized address"

python - 如何正确地与集合联合

python - uwsgi + django 导入错误

xml - 使用 Linux Bash 脚本从 XML 文件中获取数据

javascript - JavaScript 的 XML 解析器