Hiện không thể thêm namespace vào XML document trực tiếp vì chưa hỗ trợ trong built-in Python xml package. Cần thêm namespace như attribute bình thường của tag. Ví dụ:
import xml.dom.minidom
doc = xml.dom.minidom.Document()
element = doc.createElementNS('https://hello.world/ns', 'ex:el')
element.setAttribute("xmlns:ex", "https://hello.world/ns")
doc.appendChild(element)
print(doc.toprettyxml())
Nhận document:
<?xml version="1.0" ?> <ex:el xmlns:ex="https://example.net/ns"/>