在python 2.7(使用etree 1.3)中,我可以在这样的元素上禁止XML前缀:
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
功能。我试图以与版本1.2.6的python 2.6的etree兼容的方式删除前缀。这是我尝试过的:
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" />'
这不是我想要的。前缀仍然存在但是空白。有没有办法彻底删除它们?
答案 0 :(得分:18)
查看source code for ElementTree in python2.6后,:
在fixtag
函数中进行了硬编码。作为一种解决方法,这就是我所做的:
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')
如果这篇文章过时,代码将保持here。
答案 1 :(得分:6)
我在文件的顶部创建了一个方法,只需在标记字符串所在的位置使用它。我把它命名为'ns_tag',但你可以随心所欲地命名它。
def ns_tag(tag):
return str( ElementTree.QName('http://some.namespace/api/4/', tag) )
示例:
root = ElementTree.fromstring(xml)
success = root.find(ns_tag('success'))
if success.text == 'true':
for node in root.find(ns_tag('items')):
id = node.find(ns_tag('id')).text
...
答案 2 :(得分:1)
为了从标签中删除命名空间,我使用此代码,其中taget对象是TreeBuilder的子类。在开始和结束两种方法中,我们删除了不需要的命名空间。
from xml.etree.ElementTree import XML, XMLParser, tostring, TreeBuilder
class StripNamespace(TreeBuilder):
def start(self, tag, attrib):
index = tag.find('}')
if index != -1:
tag = tag[index+1:]
super(StripNamespace, self).start(tag, attrib)
def end(self, tag):
index = tag.find('}')
if index != -1:
tag = tag[index+1:]
super(StripNamespace, self).end(tag)
target = StripNamespace()
parser = XMLParser(target=target)
with open("DmsDesc.xml") as f:
content = f.read()
root = XML(content, parser=parser)
print tostring(root)
简单而漂亮。
答案 3 :(得分:0)
这是一个黑客,但这对我来说在Jython 2.5.2上运作良好
ns=re.match('^\{([^\}]+)\}', mydoc.getroot().tag ).group(1)
etree._namespace_map[ns]='STRIPME'
etree.tostring( mydoc.getroot() ).replace( 'STRIPME:', '' )