python lxml元素属性问题

python lxml元素属性问题,python,xml,python-3.x,lxml,xml-namespaces,Python,Xml,Python 3.x,Lxml,Xml Namespaces,我必须构建一个如下所示的XML文件: xmlns=8745878 数据 除了命令attribxsi:type=“UserGetRegistrationListRequest” 我无法在command元素的attrib中获取: 有人能帮我解决这个问题吗 我正在使用Python 3.5 我现在的代码是 from lxml import etree root = etree.Element("Document", protocol="OCI", xmlns="C") print(root.ta

我必须构建一个如下所示的XML文件:


xmlns=8745878
数据
除了命令attrib
xsi:type=“UserGetRegistrationListRequest”

我无法在command元素的attrib中获取

有人能帮我解决这个问题吗

我正在使用Python 3.5

我现在的代码是

from lxml import etree


root = etree.Element("Document", protocol="OCI", xmlns="C")
print(root.tag)
root.append(etree.Element("sessionId") )
sessionId=root.find("sessionId")
sessionId.text = "xmlns=78546587854"
root.append(etree.Element("command",  xmlns="http://www.w3.org/2001/XMLSchema-instance",xsitype = "UserGetRegistrationListRequest"  ) )
command=root.find("command")
userID = etree.SubElement(command, "userId")
userID.text = "data"
print(etree.tostring(root, pretty_print=True))
tree = etree.ElementTree(root)
tree.write('output.xml', pretty_print=True, xml_declaration=True,   encoding="ISO-8859-1")
然后我把这个拿回来

   <?xml version='1.0' encoding='ISO-8859-1'?>
   <Document protocol="OCI" xmlns="C">
   <sessionId>xmlns=78546587854</sessionId>
   <command xmlns="http://www.w3.org/2001/XMLSchema-instance" xsitype="UserGetRegistrationListRequest">
   <userId>data</userId>
 </command>

xmlns=78546587854
数据

QName
可用于创建
xsi:type
属性

from lxml import etree

root = etree.Element("Document", protocol="OCI", xmlns="C")

# Create sessionId element
sessionId = etree.SubElement(root, "sessionId")
sessionId.text = "xmlns=78546587854"

# Create xsi:type attribute using QName 
xsi_type = etree.QName("http://www.w3.org/2001/XMLSchema-instance", "type")

# Create command element, with xsi:type attribute
command = etree.SubElement(root, "command", {xsi_type: "UserGetRegistrationListRequest"})

# Create userId element
userID = etree.SubElement(command, "userId")
userID.text = "data"
结果XML(使用适当的
xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance“
声明):


xmlns=78546587854
数据

请注意,
xsi
前缀不需要在Python代码中明确定义。lxml为一些众所周知的名称空间uri定义了默认前缀,包括
xsi
for
http://www.w3.org/2001/XMLSchema-instance

QName
可用于创建
xsi:type
属性

from lxml import etree

root = etree.Element("Document", protocol="OCI", xmlns="C")

# Create sessionId element
sessionId = etree.SubElement(root, "sessionId")
sessionId.text = "xmlns=78546587854"

# Create xsi:type attribute using QName 
xsi_type = etree.QName("http://www.w3.org/2001/XMLSchema-instance", "type")

# Create command element, with xsi:type attribute
command = etree.SubElement(root, "command", {xsi_type: "UserGetRegistrationListRequest"})

# Create userId element
userID = etree.SubElement(command, "userId")
userID.text = "data"
结果XML(使用适当的
xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance“
声明):


xmlns=78546587854
数据
请注意,
xsi
前缀不需要在Python代码中明确定义。lxml为一些众所周知的名称空间uri定义了默认前缀,包括
xsi
for
http://www.w3.org/2001/XMLSchema-instance

A将帮助我们回答您的问题。但是,如果使用
xsi:type
属性而不将
xsi
前缀绑定到名称空间,则会导致XML名称空间格式不正确<代码>xmlns=”http://www.w3.org/2001/XMLSchema-instance“必须是
xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance“
.A将帮助我们回答您的问题。但是,如果使用
xsi:type
属性而不将
xsi
前缀绑定到名称空间,则会导致XML名称空间格式不正确<代码>xmlns=”http://www.w3.org/2001/XMLSchema-instance“必须是
xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance“