小编Jak*_*s42的帖子

python lxml findall 具有多个命名空间

我正在尝试使用 lxml 解析具有多个命名空间的 XML 文档,但我一直坚持使用 findall() 方法返回某些内容。

我的 XML:

<MeasurementRecords xmlns="http://www.company.com/common/rsp/2012/07"
                    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"         
                    xsi:schemaLocation="http://www.company.com/common/rsp/2012/07 RSP_EWS_V1.6.xsd">
    <HistoryRecords>
        <ValueItemId>100_0000100004_3788_Resource-0.customId_WSx Data Precip Type</ValueItemId>
            <List>
                <HistoryRecord>
                    <Value>60</Value>
                    <State>Valid</State>
                    <TimeStamp>2016-04-20T12:40:00Z</TimeStamp>
                </HistoryRecord>
            </List>
        </HistoryRecords>
    <HistoryRecords>
</MeasurementRecords>
Run Code Online (Sandbox Code Playgroud)

我的代码:

from lxml import etree
from pprint import pprint

RSPxmlFile = '/home/user/Desktop/100_0000100004_3788_20160420144011263_records.xml'

with open (RSPxmlFile, 'rt') as f:
    tree = etree.parse(f)

root = tree.getroot()

for node in tree.findall('MeasurementRecords', root.nsmap):
    print node
    print "parameter = ", node.text
Run Code Online (Sandbox Code Playgroud)

给出:

ValueError: empty namespace prefix is not supported in ElementPath
Run Code Online (Sandbox Code Playgroud)

阅读本文后我尝试了一些实验:

>>> root.nsmap …
Run Code Online (Sandbox Code Playgroud)

python xpath lxml xml-namespaces

5
推荐指数
1
解决办法
4845
查看次数

标签 统计

lxml ×1

python ×1

xml-namespaces ×1

xpath ×1