Keep lxml from creating self-closing tags

I have a (old) tool that does not understand self-closing tags, for example <STATUS/>. So, we need to serialize our XML files open / close tags as follows: <STATUS></STATUS>.

I currently have:

>>> from lxml import etree

>>> para = """<ERROR>The status is <STATUS></STATUS>.</ERROR>"""
>>> tree = etree.XML(para)
>>> etree.tostring(tree)
'<ERROR>The status is <STATUS/>.</ERROR>'

How can I serialize with open / closed tags?

<ERROR>The status is <STATUS></STATUS>.</ERROR>

Decision

Courtesy of wildwilhelm , below :

>>> from lxml import etree

>>> para = """<ERROR>The status is <STATUS></STATUS>.</ERROR>"""
>>> tree = etree.XML(para)
>>> for status_elem in tree.xpath("//STATUS[string() = '']"):
...     status_elem.text = ""
>>> etree.tostring(tree)
'<ERROR>The status is <STATUS></STATUS>.</ERROR>'
+4
source share
1 answer

The tag seems to be <STATUS>assigned an attribute text None:

>>> tree[0]
<Element STATUS at 0x11708d4d0>
>>> tree[0].text
>>> tree[0].text is None
True

If you set the texttag attribute <STATUS>to an empty string, you should get what you are looking for:

>>> tree[0].text = ''
>>> etree.tostring(tree)
'<ERROR>The status is <STATUS></STATUS>.</ERROR>'

, , DOM text, XML. - :

# prevent creation of self-closing tags
for node in tree.iter():
    if node.text is None:
        node.text = ''
+3

Source: https://habr.com/ru/post/1667959/


All Articles