我需要获取这个xml:
<s:Envelope xmlns:a="http://www.w3.org/2005/08/addressing" xmlns:s="http://www.w3.or/2003/05/soap-envelope">
<s:Header>
<a:Action s:mustUnderstand="1">Action</a:Action>
</s:Header>
</s:Envelope>
据我所知,< Action >节点和它的属性"mustUnderstand“位于不同的名称空间下。我现在所取得的成就:
from lxml.etree import Element, SubElement, QName, tostring
class XMLNamespaces:
s = 'http://www.w3.org/2003/05/soap-envelope'
a = 'http://www.w3.org/2005/08/addressing'
root = Element(QName(XMLNamespaces.s, 'Envelope'), nsmap={'s':XMLNamespaces.s, 'a':XMLNamespaces.a})
header = SubElement(root, QName(XMLNamespaces.s, 'Header'))
action = SubElement(header, QName(XMLNamespaces.a, 'Action'))
action.attrib['mustUnderstand'] = "1"
action.text = 'Action'
print tostring(root, pretty_print=True)
和结果:
<s:Envelope xmlns:a="http://www.w3.org/2005/08/addressing" xmlns:s="http://www.w3.org/2003/05/soap-envelope">
<s:Header>
<a:Action mustUnderstand="1">http://schemas.xmlsoap.org/ws/2004/09/transfer/Create</a:Action>
</s:Header>
</s:Envelope>
正如我们所看到的,在"mustUnderstand“属性前面没有名称空间前缀。那么用lxml可以得到"s: mustUnderstand“吗?如果是,那是怎么做的?
https://stackoverflow.com/questions/25132998
复制相似问题