在ElementTree 1.2中禁止名称空间前缀 [英] Suppressing namespace prefixes in ElementTree 1.2

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

问题描述

在python 2.7(带有etree 1.3)中,我可以在这样的元素上取消XML前缀:

In python 2.7 (with etree 1.3), I can suppress the XML prefixes on elements like this:

Python 2.7.1 (r271:86832, Jun 16 2011, 16:59:05) 
[GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import xml.etree.ElementTree as etree
>>> etree.VERSION
'1.3.0'
>>> something = etree.Element('{http://some.namespace}token')
>>> etree.tostring(something)
'<ns0:token xmlns:ns0="http://some.namespace" />'
>>> etree.register_namespace('', 'http://some.namespace')
>>> etree.tostring(something)
'<token xmlns="http://some.namespace" />'

在1.3中添加了 register_namespace 函数。我正在尝试以与python 2.6的etree版本1.2.6兼容的方式删除前缀。这是我尝试过的方法:

The register_namespace function was added in 1.3. I'm trying to remove the prefix in a way that is compatible with python 2.6's etree at version 1.2.6. Here's what I've tried:

Python 2.6.7 (r267:88850, Jul 31 2011, 19:30:54) 
[GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import xml.etree.ElementTree as etree
>>> etree.VERSION
'1.2.6'
>>> something = etree.Element('{http://some.namespace}token')
>>> etree.tostring(something)
'<ns0:token xmlns:ns0="http://some.namespace" />'
>>> etree._namespace_map['http://some.namespace'] = ''
>>> etree.tostring(something)
'<:token xmlns:="http://some.namespace" />'

这不是我想要的。前缀仍然存在,但为空白。有什么办法可以完全删除它们?

This is not what I want. The prefixes are still there but are blank. Is there any way to remove them completely?

推荐答案

查看 python2.6中ElementTree的源代码 fixtag 函数中进行了硬编码。作为解决方法,这是我所做的:

After looking at the source code for ElementTree in python2.6, the : is hard coded in the fixtag function. As a workaround, here's what I did:

from xml.etree import ElementTree as etree

if etree.VERSION[0:3] == '1.2':
    #in etree < 1.3, this is a workaround for supressing prefixes

    def fixtag(tag, namespaces):
        import string
        # given a decorated tag (of the form {uri}tag), return prefixed
        # tag and namespace declaration, if any
        if isinstance(tag, etree.QName):
            tag = tag.text
        namespace_uri, tag = string.split(tag[1:], "}", 1)
        prefix = namespaces.get(namespace_uri)
        if namespace_uri not in namespaces:
            prefix = etree._namespace_map.get(namespace_uri)
            if namespace_uri not in etree._namespace_map:
                prefix = "ns%d" % len(namespaces)
            namespaces[namespace_uri] = prefix
            if prefix == "xml":
                xmlns = None
            else:
                if prefix is not None:
                    nsprefix = ':' + prefix
                else:
                    nsprefix = ''
                xmlns = ("xmlns%s" % nsprefix, namespace_uri)
        else:
            xmlns = None
        if prefix is not None:
            prefix += ":"
        else:
            prefix = ''

        return "%s%s" % (prefix, tag), xmlns

    etree.fixtag = fixtag
    etree._namespace_map['http://some.namespace'] = None
else:
    #For etree > 1.3, use register_namespace function
    etree.register_namespace('', 'http://some.namespace')

如果此帖子过时,请在此处维护代码

In case this post ever gets out of date, the code is maintained here.

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

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