【发布时间】:2018-10-08 03:30:08
【问题描述】:
我正在使用 python 3.5 中的 lxml 库来解析 xml 文件。 xml内容为:
xml_content = """
<wps:ExecuteResponse xmlns:gml="http://www.opengis.net/gml"
xmlns:ows="http://www.opengis.net/ows/1.1"
xmlns:wps="http://www.opengis.net/wps/1.0.0"
xmlns:xlink="http://www.w3.org/1999/xlink"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.opengis.net/wps/1.0.0
http://schemas.opengis.net/wps/1.0.0/wpsExecute_response.xsd"
service="WPS" version="1.0.0" xml:lang="en-US"
serviceInstance="http://192.168.2.72:5000/wps?service=WPS&request=GetCapabilities" statusLocation="http://192.168.2.72:5000/output/9fbbf322-496d-11e8-9a87-0242ac110002.xml">
<wps:Process wps:processVersion="None">
<ows:Identifier>run_checks</ows:Identifier>
<ows:Title>Run checks process</ows:Title>
<ows:Abstract>Process performing qc tool checks.</ows:Abstract>
</wps:Process>
<wps:Status creationTime="2018-04-26T16:19:41Z">
<wps:ProcessSucceeded>PyWPS Process Run checks process finished</wps:ProcessSucceeded>
</wps:Status>
<wps:DataInputs>
<wps:Input>
<ows:Identifier>filepath</ows:Identifier>
<ows:Title>Local filesystem path to the product to be checked.</ows:Title>
<wps:Data>
<wps:LiteralData dataType="string">/mnt/bubu/bebe</wps:LiteralData>
</wps:Data>
</wps:Input>
<wps:Input>
<ows:Identifier>product_type_name</ows:Identifier>
<ows:Title>The type of the product denoting group of checks to be performed.</ows:Title>
<wps:Data>
<wps:LiteralData dataType="string">dummy</wps:LiteralData>
</wps:Data>
</wps:Input>
<wps:Input>
<ows:Identifier>optional_check_idents</ows:Identifier>
<ows:Title>Comma separated identifiers of optional checks to be performed.</ows:Title>
<wps:Data>
<wps:LiteralData dataType="string">dummy</wps:LiteralData>
</wps:Data>
</wps:Input>
</wps:DataInputs>
<wps:OutputDefinitions>
<wps:Output>
<ows:Identifier>result</ows:Identifier>
<ows:Title>Result of passed checks in json format.</ows:Title>
</wps:Output>
</wps:OutputDefinitions>
<wps:ProcessOutputs>
<wps:Output>
<ows:Identifier>result</ows:Identifier>
<ows:Title>Result of passed checks in json format.</ows:Title>
<wps:Data>
<wps:LiteralData dataType="urn:ogc:def:dataType:OGC:1.1:string"> {"dummy": {"status": "ok", "message": "Dummy check has passed.", "params": "{'dummy_param1': 'dummy value1', 'dummy_param2': 'dummy value2'}"}}
</wps:LiteralData>
</wps:Data>
</wps:Output>
</wps:ProcessOutputs>
</wps:ExecuteResponse>
"""
我解析文件的python代码是:
from lxml import etree
ns = {'wps': 'http://www.opengis.net/wps/1.0.0',
'ows': 'http://schemas.opengis.net/ows/1.1.0'}
tree = etree.fromstring(xml_content)
# this works, the wps:Process tag is found successfully
wps_process_tag = tree.xpath('//wps:Process', namespaces=ns)
if len(wps_process_tag) > 0:
print('wps:Process tag found!')
# this does not work and the ows:Identifier tag is not found
ows_identifier_tag = tree.xpath('//wps:Process/ows:Identifier', namespaces=ns)
if len(ows_identifier_tag) > 0:
print('ows:Identifier tag found!')
else:
print('ows:Identifier tag not found!')
如我的示例代码所示,wps:Process 标记已正确找到。另一方面,没有找到ows:Identifier 标记,尽管它存在于xml 文档中的wps:Process 下。我已将名称空间字典提供给 tree.xpath 函数,以便从 wps 和 ows 名称空间中查找元素。但它只找到以wps: 开头的元素,它找不到以ows: 开头的元素
我检查了 URL http://schemas.opengis.net/ows/1.1.0/,它似乎是一个有效的 URL。
如何使用 lxml 找到 ows:Identifier 元素?
【问题讨论】:
-
在您的代码中,您正在执行此操作:
'ows': 'http://schemas.opengis.net/ows/1.1.0'。但在 XML 中,ows命名空间声明了不同的 URI:http://www.opengis.net/ows/1.1。更正代码中的 URI,看看是否可以修复它。 -
你是对的!我完全忽略了我代码中的
owsURI 与xml 文档中的owsURI 不同。我更正了代码中的 URI,如下所示:ns = {'wps': 'http://www.opengis.net/wps/1.0.0', 'ows': 'http://www.opengis.net/ows/1.1'},我的问题已解决。我想我最初是在 Firefox 浏览器中显示 xml 文档,我在 xsi:schemaLocation 中复制 URI,这与 xmlns .... -
我已将我的评论作为答案,因为它似乎成功了。
标签: xml python-3.x xpath lxml xml-namespaces