【发布时间】:2020-03-18 10:55:07
【问题描述】:
我知道已经有一些从 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”的“name”属性 我尝试过以下操作:
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
【问题讨论】:
标签: python python-3.x xml wsdl