I need to add a prefix to my attribute in my xml elements like:
<Database xmlns:xsi="http://www.w3.org/" xmlns="http://www.staubli.com/">
<Data name="pPointTraj" access="public" xsi:type="array" type="PointTraj" size="2">
<Field name="pPoint" xsi:type="array" type="pointRx" size="1">
I am talking about the xmlns:
and xsi:
elements here.
So ideally I want to do that:
Database = ET.Element("Database", xmlns:xsi="http://www.w3.org/")
Datas = ET.SubElement(Database, "Datas")
Data = ET.SubElement(Datas, "Data", name="pPointTraj", access="public", xsi:type="array", type="PointTraj", size="2")
From what I've tried I can put it without the term behind the :
but if I put the prefix I get a syntax error.
I am stuck here and couldn't find my answer on the internet, can someone help me on this? thank you in advance.
EDIT : QName
@mzjn shared this answer to another question so I tried it but I still have things that I want to get rid of and I don't know how.
from xml.etree import ElementTree as ET
NS1 = "http://www.w3.org/"
ET.register_namespace("xsi", NS1)
qname1 = ET.QName(NS1, "D") # Element QName
root = ET.Element("Database", {qname1:""},xmlns="http://www.staubli.com/")
print(ET.tostring(root).decode())
The code above gives me
<Database xmlns:xsi="http://www.w3.org/" xsi:D="" xmlns="http://www.staubli.com/" />
And I want to get rid of the xsi:D=""
. Do you know how I can achieve this? Thank you.