Ich brauche xml zu erzeugen, die wie folgt aussieht:Generierung von XML mit LXML - Attribut Namespace
<definitions xmlns:ex="http://www.example1.org" xmlns="http://www.example2.org">
<typeRef xmlns:ns2="xyz">text</typeRef>
</definitions>
Mein Code sieht wie folgt aus:
class XMLNamespaces:
ex = 'http://www.example1.org'
xmlns = 'http://www.example2.org'
root = Element('definitions', xmlns='http://www.example2.org', nsmap = {'ex':XMLNamespaces.ex})
type_ref = SubElement(root, 'typeRef')
type_ref.attrib[QName(XMLNamespaces.xmlns, 'ns2')] = 'xyz'
type_ref.text = 'text'
tree = ElementTree(root)
tree.write('filename.xml', pretty_print=True)
Das Ergebnis sieht so aus:
<definitions xmlns:ex="http://www.example1.org" xmlns="http://www.example2.org">
<typeRef xmlns:ns0="http://www.example2.org" ns0:ns2="xyz">text</typeRef>
</definitions>
Also hier ist meine Frage:
Wie man das Attribut aussehen lässt xmlns: ns2 = "xyz" statt xmlns: ns0 = "http://www.example2.org" ns0: ns2 = "xyz"?