您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

使用Python ElementTree创建不带ns0命名空间的SVG / XML文档

使用Python ElementTree创建不带ns0命名空间的SVG / XML文档

我只是想通了,无法删除问题,所以这里是:

etree.register_namespace("","http://www.w3.org/2000/svg")
@H_404_5@

我认为这仅适用于Python 2.7。

解决方法

我正在Python 2.7中使用ElementTree构建SVG文档。这是代码:

from xml.etree import ElementTree as etree

root = etree.XML('<svg width="100%" height="100%" version="1.1" xmlns="http://www.w3.org/2000/svg"></svg>')
root.append(etree.Element("path"))
root[0].set("d","M1 1 L2 2 Z")
print etree.tostring(root,encoding='iso-8859-1')

生成输出:

<?xml version='1.0' encoding='iso-8859-1'?>
<ns0:svg xmlns:ns0="http://www.w3.org/2000/svg" height="100%" version="1.1" width="100%"><path d="M1 1 L2 2 Z" /></ns0:svg>

这不会解析为有效的SVG。如何删除ns0名称空间?

喜欢与人分享编程技术与工作经验,欢迎加入编程之家官方交流群!
from xml.etree import ElementTree as etree

root = etree.XML('<svg width="100%" height="100%" version="1.1" xmlns="http://www.w3.org/2000/svg"></svg>')
root.append(etree.Element("path"))
root[0].set("d","M1 1 L2 2 Z")
print etree.tostring(root,encoding='iso-8859-1')
<?xml version='1.0' encoding='iso-8859-1'?>
<ns0:svg xmlns:ns0="http://www.w3.org/2000/svg" height="100%" version="1.1" width="100%"><path d="M1 1 L2 2 Z" /></ns0:svg>

我认为这仅适用于Python 2.7。

我正在Python 2.7中使用ElementTree构建SVG文档。这是代码:

生成输出:

这不会解析为有效的SVG。如何删除ns0名称空间?

我认为这仅适用于Python 2.7。

python 2022/1/1 18:34:52 有234人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶