Home > Mobile >  Adding an attribute to an XML tag without affecting namespace declarations
Adding an attribute to an XML tag without affecting namespace declarations

Time:02-05

I'm trying to add an attribute value to a tag in an XML string when the attribute is missing, and print out the modified string. Code is patchwork that I've roughly gleaned from various sources, so apologies if this isn't best practice.

Here's what the code looks like-

import xml.etree.ElementTree as ET
import time

xml_string = """
<message>
    <info xmlns="urn:xmpp:info" />
</message>
"""

root = ET.fromstring(xml_string)
ns = {'ns': 'urn:xmpp:info'}
for info in root.findall(".//ns:info", ns):
    sent_time = info.attrib.get("sent_time_millis")
    if not sent_time:
        sent_time = str(int(time.time() * 1000))
        info.attrib["sent_time_millis"] = sent_time
    print(ET.tostring(root, encoding='unicode'))
    break

This results in something like this when run-

<message xmlns:ns0="urn:xmpp:info">
    <ns0:info sent_time_millis="1675533312576" />
</message>

When the desired output should be something like this-

<message>
    <info xmlns="urn:xmpp:info" sent_time_millis="1675533086777" />
</message>

I'm missing something basic, I'm sure. How should I go about this modification?

Thanks.

CodePudding user response:

ElementTree fiddles with namespaces in a way that can be annoying (even though the semantics does not change). lxml does not do this, so a solution is to use that library instead.

Install lxml and change import xml.etree.ElementTree as ET to from lxml import etree as ET in the code.

  • Related