看后在python2.6的对ElementTree的源代码,则
:是硬的编码
fixtag功能。作为解决方法,这是我所做的:
from xml.etree import ElementTree as etreeif 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'] = Noneelse: #For etree > 1.3, use register_namespace function etree.register_namespace('', 'http://some.namespace')
如果此帖子过时,则在此处维护代码。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)