似乎无法删除"ns0:"命名空间声明

 历史本轻狂 发布于 2023-01-17 21:57

我要做的就是读取一个本地.xml文件(将其编码为UTF-8,使其具有正确的标题,然后重新保存文件).但是,当我运行以下内容时,它会在每个XML元素中添加可怕的"ns0:"声明:

import xml.etree.ElementTree as ET
import sys, os

# note that this is the *module*'s `register_namespace()` function
# WTF THIS SHOULD WORK....
ET.register_namespace("", "http://www.w3.org/2000/svg")

tree = ET.ElementTree() # instantiate an object of *class* `ElementTree`
tree.parse('//cbweb1/inetpub/x/sitemap/sitemap_index.xml')

tree.write('//cbweb1/inetpub/x/sitemap/test.xml', encoding = 'utf-8', xml_declaration=True)

我究竟做错了什么??

仅供参考,这是Python 2.7.x(已经尝试过3.4)

编辑:

输入:


  
    http://www.example.com/something.xml
    2014-05-01
  

输出:



  
    http://www.example.com/something.xml
    2014-05-01
  

Joshua Taylo.. 7

如果原始输入中的默认命名空间是http://www.sitemaps.org/schemas/sitemap/0.9,如下所示:


然后,如果你将其设置为其他东西

ET.register_namespace("", "http://www.w3.org/2000/svg")

你需要在输出中声明一些其他命名空间http://www.sitemaps.org/schemas/sitemap/0.9.相反,您应该将其设置为相同的值:

ET.register_namespace("", "http://www.sitemaps.org/schemas/sitemap/0.9")

(或者,您可能只是尝试根本不将其设置为任何内容;也许默认情况下,输入中存在的输出中将使用相同的命名空间.)

1 个回答
  • 如果原始输入中的默认命名空间是http://www.sitemaps.org/schemas/sitemap/0.9,如下所示:

    <sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
    

    然后,如果你将其设置为其他东西

    ET.register_namespace("", "http://www.w3.org/2000/svg")
    

    你需要在输出中声明一些其他命名空间http://www.sitemaps.org/schemas/sitemap/0.9.相反,您应该将其设置为相同的值:

    ET.register_namespace("", "http://www.sitemaps.org/schemas/sitemap/0.9")
    

    (或者,您可能只是尝试根本不将其设置为任何内容;也许默认情况下,输入中存在的输出中将使用相同的命名空间.)

    2023-01-17 22:07 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有