我知道已经有一些从XML节点中检索特定属性的示例,但是我在使用名称空间的过程中并未取得成功。我可以检索没有任何名称空间的属性,例如this example。
假设我有以下“ example.wsdl”文件:
<?xml version="1.0" encoding="UTF-8"?><wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" name="Name of the file">
<wsdl:documentation>Documentation of the file</wsdl:documentation>
</wsdl:definitions>
我想检索节点“ wsdl:defintions”的“名称”属性 我已经尝试过以下操作:
from lxml import etree
tree = etree.parse("example.wsdl")
rootWSDL = tree.getroot()
print(tree.find('./wsdl:definitions',rootWSDL.nsmap).attrib['name'])
但是,上面的代码返回了一个空列表,并显示以下消息:
AttributeError:'NoneType'对象没有属性'attrib'
对于它的价值,我正在使用的Python版本是3.7.5