【问题标题】:With Python 3 and lxml, how to extract the Version number from a SOAP WSDL?使用 Python 3 和 lxml,如何从 SOAP WSDL 中提取版本号?
【发布时间】:2018-03-12 04:49:08
【问题描述】:

当我使用 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

【问题讨论】:

  • 您是否真的在命名空间映射中注册了 wsdl 命名空间,我忘记了它的名字? (否则,您必须使用全名,而不仅仅是映射名称。)另外,您真的可以像wsdl:.//Version 那样进行搜索并期望命名空间附加到每个子路径而不是显式指定wsdl:Version? (如果这部分很愚蠢,请原谅我;自从使用名称空间处理 XML 已经有一段时间了……)

标签: python soap wsdl lxml


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    感谢评论的好心人,这是一个修改后的解决方案,可以打印版本号。我所能做的就是通配符搜索。此外,迭代器跳过了 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)
    

    【讨论】:

      猜你喜欢
      • 2016-05-11
      • 1970-01-01
      • 2019-07-16
      • 1970-01-01
      • 2018-03-20
      • 2011-01-06
      • 1970-01-01
      • 2013-04-17
      • 1970-01-01
      相关资源
      最近更新 更多