python - 如何在python中复制命名空间
问题描述
我的输入文件是:aa.xml
<?xml version="1.0" encoding="utf-8"?>
<CSDOC xmlns:cs="http://www.google-try.com/cs"
xmlns:ci="http://www.google-try.com/ci">
<cs:body>
<cs:content>
<cs:appendix>
<blockquote>
<p>
<text>
This is a text to try this programm is correct or not. <ci:cite>My name is john</ci:cite> this is a test.
</text>
</p>
</blockquote>
</cs:appendix>
</cs:content>
</cs:body>
</CSDOC>
我的python代码是:
from lxml import etree
import xml.etree.ElementTree as ET
import copy
XMLDoc = etree.parse(open('aa.xml'))
v = '/CSDOC/cs:body/cs:content/cs:appendix/blockquote/p/text/ci:cite'
for Node in XMLDoc.xpath(v,namespaces={
'cs': 'http://www.google-try.com/cs',
'ci': 'http://www.google-try.com/ci',
}):
m2 = copy.deepcopy(Node)
print(ET.tostring(m2))
输出是:
b'<ns0:cite xmlns:ns0="http://www.google-try.com/ci">My name is john</ns0:cite> this is a test.\n\t\t\t\t\t\t'ut
期望的输出是:
<ci:cite>My name is john</ci:cite>
实际上,我只想复制选择或 xpath 元素内容,但我的代码是复制带有父元素内容且没有命名空间的选择内容。请建议如何仅使用命名空间复制 xpath 元素内容。
解决方案
推荐阅读
- c# - 视频时长是应有的两倍
- centos7 - 在没有 Sudo 的情况下启动 haproxy:启动前端 http-in:无法绑定套接字
- excel - 将多个工作簿最后一行的数据复制并粘贴到另一个工作簿中的工作表中
- c++ - 它是 Eratosthenes 筛的有效代码吗?
- amazon-web-services - Redshift/S3 数据是否混合在一起?
- cython - 为什么 Cython 使用 gcc 优化?
- opengl - 使用 OpenGL 实现更平滑的渐变过渡?
- python - KDB/Q Websocket 示例,出现 `badmsg 错误:如何在将数据发送到我的 KDB 服务器时对其进行序列化?
- sql-server - 在 SQL Server 上运行正则表达式
- delphi - 在delphi的exe中嵌入com obj