使用 Python 3 和 lxml,如何从 SOAP WSDL 中提取版本号?

With Python 3 and lxml, how to extract the Version number from a SOAP WSDL?

当我使用 WSDL 文件的一个子集进行测试时,文件和代码中省略了名称空间,它工作正常。

#  for reference, these are the final lines from the WSDL
#
#   <wsdl:service name="Shopping">
#           <wsdl:documentation>
#               <Version>1027</Version>
#           </wsdl:documentation>
#       <wsdl:port binding="ns:ShoppingBinding" name="Shopping">
#           <wsdlsoap:address location="http://open.api.ebay.com/shopping"/>
#       </wsdl:port>
#   </wsdl:service>
#</wsdl:definitions>

from lxml import etree
wsdl = etree.parse('http://developer.ebay.com/webservices/latest/ShoppingService.wsdl')
print(wsdl.findtext('wsdl:.//Version'))   # wish this would print 1027

/Library/Frameworks/Python.framework/Versions/3.6/bin/python3.6 "/Users/matecsaj/Google Drive/Projects/collectibles/eBay/figure-it3.py"
    Traceback (most recent call last):
      File "src/lxml/_elementpath.py", line 79, in lxml._elementpath.xpath_tokenizer (src/lxml/_elementpath.c:2414)
    KeyError

在处理上述异常的过程中,又发生了一个异常:

Traceback (most recent call last):
File "/Users/matecsaj/Google Drive/Projects/collectibles/eBay/figure-it3.py", line 14, in <module>
print(wsdl.findtext('wsdl:.//Version'))   # wish this would print 1027
File "src/lxml/etree.pyx", line 2230, in lxml.etree._ElementTree.findtext (src/lxml/etree.c:69049)
File "src/lxml/etree.pyx", line 1552, in lxml.etree._Element.findtext (src/lxml/etree.c:60629)
File "src/lxml/_elementpath.py", line 329, in lxml._elementpath.findtext (src/lxml/_elementpath.c:10089)
File "src/lxml/_elementpath.py", line 311, in lxml._elementpath.find (src/lxml/_elementpath.c:9610)
File "src/lxml/_elementpath.py", line 300, in lxml._elementpath.iterfind (src/lxml/_elementpath.c:9282)
File "src/lxml/_elementpath.py", line 277, in lxml._elementpath._build_path_iterator (src/lxml/_elementpath.c:8675)
File "src/lxml/_elementpath.py", line 82, in xpath_tokenizer (src/lxml/_elementpath.c:2542)
SyntaxError: prefix 'wsdl' not found in prefix map

Process finished with exit code 1

xml 中定义了命名空间,因此要访问您需要定义命名空间的 link 的元素。请查看以下代码是否有帮助:

wsdlLink = "http://schemas.xmlsoap.org/wsdl/"
wsdl = etree.parse('http://developer.ebay.com/webservices/latest/ShoppingService.wsdl')
print(wsdl.findtext('{'+wsdlLink+'}//Version'))

感谢发表评论的好心人,这里是一个修改后的解决方案,可以打印版本号。我所能做的就是通配符搜索。此外,迭代器跳过了 Version 元素,所以我必须从它的父元素中获取它。够用了。

from lxml import etree
wsdlLink = "http://schemas.xmlsoap.org/wsdl/"
wsdl = etree.parse('http://developer.ebay.com/webservices/latest/ShoppingService.wsdl')
for element in wsdl.iter('{'+wsdlLink+'}*'):
    if 'documentation' in element.tag:
        for child in element:
            print(child.text)