lxml.etree 和 xml.etree.ElementTree 添加没有前缀的命名空间(ns0、ns1 等)

发布于 2024-10-07 05:13:47 字数 489 浏览 0 评论 0原文

有没有任何解决方案可以添加不带前缀的名称空间(我的意思是这些 ns0、ns1),该名称空间适用于所有 etree 实现,或者每个都有可行的解决方案?

现在我有以下解决方案:

  • lxml - 元素的 nsmap 参数
  • (c)ElementTree(python 2.6+) - 使用空字符串作为前缀注册命名空间方法

问题是 ( c)python 2.5中的ElementTree,我知道有 _namespace_map 属性,但将其设置为空字符串创建无效的XML,将其设置为 None 添加默认的 ns0 等命名空间,有什么可行的解决方案吗?

我想

Element('foo', {'xmlns': 'http://my_namespace_url.org/my_ns'})

这是一个坏主意?

感谢您的帮助

There is any solution to add namespaces without prefix(i mean these ns0, ns1) which working on all the etree implementations or there are working solutions for each one?

For now I have solutions for:

  • lxml - nsmap argument of Element
  • (c)ElementTree(python 2.6+) - register namespace method with empty string as a prefix

The problem is (c)ElementTree in python 2.5, I know there is _namespace_map attribute but setting it to empty string creating invalid XML, setting it to None adding default ns0 etc. namespaces, is there any working solution?

I guess

Element('foo', {'xmlns': 'http://my_namespace_url.org/my_ns'})

is a bad idea?

Thanks for help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

莳間冲淡了誓言ζ 2024-10-14 05:13:47

我刚刚为你工作了。

定义您自己的前缀:

unique = 'bflmpsvz'

my_namespaces = {
                 'http://www.topografix.com/GPX/1/0' :    unique,
                 'http://www.groundspeak.com/cache/1/0' : 'groundspeak',
                }
xml.etree.ElementTree._namespace_map.update( my_namespaces )

然后,替换/删除输出上的前缀:

def writeDown(data, output_filename):

    data.write(output_filename)
    txt = file(output_filename).read()
    txt = txt.replace(unique+':','')
    file(output_filename,'w').write(txt)

可能有更好的解决方案。

I have just work around for you.

Define your own prefix:

unique = 'bflmpsvz'

my_namespaces = {
                 'http://www.topografix.com/GPX/1/0' :    unique,
                 'http://www.groundspeak.com/cache/1/0' : 'groundspeak',
                }
xml.etree.ElementTree._namespace_map.update( my_namespaces )

And then, replace/remove the prefix on the output:

def writeDown(data, output_filename):

    data.write(output_filename)
    txt = file(output_filename).read()
    txt = txt.replace(unique+':','')
    file(output_filename,'w').write(txt)

Probably, there is better solution.

梦在夏天 2024-10-14 05:13:47

我使用了 Jiri 的想法,但在 unique 也是默认命名空间的情况下我添加了额外的一行:

def writeDown(data, output_filename):

    data.write(output_filename)
    txt = file(output_filename).read()
    txt = txt.replace(unique+':','')
    txt = txt.replace('xmlns:'+unique,'xmlns')
    file(output_filename,'w').write(txt)

I used Jiri's idea, but I added an extra line in the case when unique is also the default namespace:

def writeDown(data, output_filename):

    data.write(output_filename)
    txt = file(output_filename).read()
    txt = txt.replace(unique+':','')
    txt = txt.replace('xmlns:'+unique,'xmlns')
    file(output_filename,'w').write(txt)
喜你已久 2024-10-14 05:13:47

我正在使用 Python 3.3.1,以下内容对我有用:

xml.etree.ElementTree.register_namespace('', 'http://your/uri')
data.write(output_filename)

好处是您不必访问私有 xml.etree.ElementTree._namespace_map 作为 Jiri建议。

我发现 Python 2.7.4 中也提供了同样的功能。

I'm using Python 3.3.1 and the following works for me:

xml.etree.ElementTree.register_namespace('', 'http://your/uri')
data.write(output_filename)

The upside is that you don't have to access the private xml.etree.ElementTree._namespace_map as Jiri suggested.

I see the same is also available in Python 2.7.4.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文