在Python中使用ElementTree更改名称空间前缀 [英] Alter namespace prefixing with ElementTree in Python

查看:231
本文介绍了在Python中使用ElementTree更改名称空间前缀的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

By default, when you call ElementTree.parse(someXMLfile) the Python ElementTree library prefixes every parsed node with it's namespace URI in Clark's Notation:


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

这使得在后面的代码中按名称访问特定节点变得很痛苦.

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

我已经阅读了有关ElementTree和名称空间的文档,并且看起来iterparse()函数应该允许我更改解析器为名称空间添加前缀的方式,但是对于我来说,我实际上无法使其更改字首.似乎在后台启动ns-start事件之前就可能发生这种情况,如以下示例所示:

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:
        ...

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

How do I make it change the prefixing behavior and what is the proper thing to return when the function ends?

推荐答案

您不需要特别使用iterparse.而是使用以下脚本:

You don't specifically need to use iterparse. Instead, the following script:

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)

其中显示了如何访问已解析树中各个节点的标准标记名.您应该能够使它适应您的特定需求.

Which shows how you can access the fully-qualified tag names of individual nodes in a parsed tree. You should be able to adapt this to your specific needs.

这篇关于在Python中使用ElementTree更改名称空间前缀的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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