简体   繁体   中英

Comment out an element using lxml

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)))

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)

Result:

<root xmlns="foo">
 <a>
  <!--<b xmlns="foo">AAA</b>
 --></a>
</root>

Manipulating namespaces is often harder than you might suspect. See https://stackoverflow.com/a/31870245/407651 .

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()

Result:

<?xml version="1.0" encoding="utf-8"?>
<root mlns="foo">
 <a>
  <!--<b>AAA</b>-->
 </a>
</root>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM