Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
具有多个名称空间的python lxml findall_Python_Xpath_Lxml_Xml Namespaces - Fatal编程技术网

具有多个名称空间的python lxml findall

具有多个名称空间的python lxml findall,python,xpath,lxml,xml-namespaces,Python,Xpath,Lxml,Xml Namespaces,我正试图用lxml解析一个具有多个名称空间的XML文档,但我一直坚持让findall()方法返回一些东西 我的XML: <MeasurementRecords xmlns="http://www.company.com/common/rsp/2012/07" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLoca

我正试图用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>
给出:

ValueError: empty namespace prefix is not supported in ElementPath
阅读后,我尝试了一些实验:

>>root.nsmap
{'xsi':'http://www.w3.org/2001/XMLSchema-instance“,无:http://www.company.com/common/rsp/2012/07'}
>>>nsmap['foo']=nsmap[None]
>>>nsmap.pop(无)
'http://www.company.com/common/rsp/2012/07'
>>>nsmap
{'xsi':'http://www.w3.org/2001/XMLSchema-instance“,“foo”:”http://www.company.com/common/rsp/2012/07'}
>>>xpath(“//MeasurementRecords”,名称空间=nsmap)
[]
>>>xpath('/foo:MeasurementRecords',namespace=nsmap)
[]
>>>xpath('/foo:MeasurementRecords/HistoryRecords',namespace=nsmap)
[]
但这似乎没有帮助

因此,更多的实验:

>>> tree.findall('//{http://www.company.com/common/rsp/2012/07}MeasurementRecords')
[]
>>> print root
<Element {http://www.company.com/common/rsp/2012/07}MeasurementRecords at 0x6ffffda5290>
>>> print tree
<lxml.etree._ElementTree object at 0x6ffffda5368>
>>> for node in tree.iter():
...     print node
...
<Element {http://www.company.com/common/rsp/2012/07}MeasurementRecords at 0x6ffffda5290>
<Element {http://www.company.com/common/rsp/2012/07}HistoryRecords at 0x6ffffda5cf8>
<Element {http://www.company.com/common/rsp/2012/07}ValueItemId at 0x6ffffda5f38>
...etc...
>>> tree.findall("//HistoryRecords", namespaces=nsmap)
[]
>>> tree.findall("//foo:MeasurementRecords/HistoryRecords", namespaces=nsmap)
[]
>>tree.findall('//{http://www.company.com/common/rsp/2012/07}测量记录’)
[]
>>>打印根
>>>打印树
>>>对于树中的节点。iter():
...     打印节点
...
等
>>>tree.findall(“//HistoryRecords”,名称空间=nsmap)
[]
>>>tree.findall(“//foo:MeasurementRecords/HistoryRecords”,名称空间=nsmap)
[]

我被难住了。我不知道出了什么问题。

如果您从以下内容开始:

>>> tree = etree.parse(open('data.xml'))
>>> root = tree.getroot()
>>> 
这将无法找到任何元素

>>> root.findall('{http://www.company.com/common/rsp/2012/07}MeasurementRecords')
[]
…但那是因为
root
MeasurementRecords
元素;信息技术 不包含任何
测量记录
元素。另一方面 另一方面,以下几点效果很好:

>>> root.findall('{http://www.company.com/common/rsp/2012/07}HistoryRecords')
[<Element {http://www.company.com/common/rsp/2012/07}HistoryRecords at 0x7fccd0332ef0>]
>>> 
因此:

  • findall
    find
    方法需要
    {…namespace…}ElementName
    语法
  • xpath
    方法需要名称空间前缀(
    ns:ElementName
    ),它在提供的
    namespaces
    映射中查找这些前缀。前缀不必与原始文档中使用的前缀匹配,但名称空间url必须匹配
所以这是可行的:

>>> root.find('{http://www.company.com/common/rsp/2012/07}HistoryRecords/{http://www.company.com/common/rsp/2012/07}ValueItemId')
<Element {http://www.company.com/common/rsp/2012/07}ValueItemId at 0x7fccd0332a70>
>>> root.xpath('/a:MeasurementRecords/a:HistoryRecords/a:ValueItemId',namespaces=nsmap)
[<Element {http://www.company.com/common/rsp/2012/07}ValueItemId at 0x7fccd0330830>]
>>root.find('{http://www.company.com/common/rsp/2012/07}历史记录/{http://www.company.com/common/rsp/2012/07}ValueItemId')
或者这是有效的:

>>> root.find('{http://www.company.com/common/rsp/2012/07}HistoryRecords/{http://www.company.com/common/rsp/2012/07}ValueItemId')
<Element {http://www.company.com/common/rsp/2012/07}ValueItemId at 0x7fccd0332a70>
>>> root.xpath('/a:MeasurementRecords/a:HistoryRecords/a:ValueItemId',namespaces=nsmap)
[<Element {http://www.company.com/common/rsp/2012/07}ValueItemId at 0x7fccd0330830>]
>>root.xpath('/a:MeasurementRecords/a:HistoryRecords/a:ValueItemId',namespaces=nsmap)
[]

太棒了!非常感谢。是否可以仅从一个
ValueItemId
元素返回
Value
节点?我试过了,但没有成功:
root.xpath('/a:MeasurementRecords/a:HistoryRecords[a:ValueItemId=“100\u 00001004\u 3788\u Resource-0.customId\u WSx Data Precip Type”]//a:Value',namespace=nsmap)
可能会有所帮助。
>>> root.xpath('/a:MeasurementRecords/a:HistoryRecords/a:ValueItemId',namespaces=nsmap)
[<Element {http://www.company.com/common/rsp/2012/07}ValueItemId at 0x7fccd0330830>]