问题描述
是否可以在保留注释内部原始元素的同时用python的lxml注释掉xml元素?我尝试了以下
Is it possible to comment out an xml element with python's lxml while preserving the original element rendering inside the comment? I tried the following
elem.getparent().replace(elem, etree.Comment(etree.tostring(elem, pretty_print=True)))
但是 tostring()
添加了名称空间声明.
but tostring()
adds the namespace declaration.
推荐答案
已注释掉的元素的名称空间是从根元素继承的.演示:
The namespace of the commented-out element is inherited from the root element. Demo:
from lxml import etree
XML = """
<root xmlns='foo'>
<a>
<b>AAA</b>
</a>
</root>"""
root = etree.fromstring(XML)
b = root.find(".//{foo}b")
b.getparent().replace(b, etree.Comment(etree.tostring(b)))
print etree.tostring(root)
结果:
<root xmlns="foo">
<a>
<!--<b xmlns="foo">AAA</b>
--></a>
</root>
操作名称空间通常比您可能想像的要难.参见 https://stackoverflow.com/a/31870245/407651 .
Manipulating namespaces is often harder than you might suspect. See https://stackoverflow.com/a/31870245/407651.
我的建议是使用 BeautifulSoup ,实际上并不真正在乎名称空间( soup.find('b')
返回 b
元素,即使它位于 foo
命名空间中).
My suggestion here is to use BeautifulSoup, which in practice does not really care about namespaces (soup.find('b')
returns the b
element even though it is in the foo
namespace).
from bs4 import BeautifulSoup, Comment
soup = BeautifulSoup(XML, "xml")
b = soup.find('b')
b.replace_with(Comment(str(b)))
print soup.prettify()
结果:
<?xml version="1.0" encoding="utf-8"?>
<root mlns="foo">
<a>
<!--<b>AAA</b>-->
</a>
</root>
这篇关于使用lxml注释掉元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!