xpath 错误,XPath 位置 > = 1 预期



我正在尝试从字符串解析xml。

下面是字符串中的xml。

<xc:Application class="bril::lumistore::Application" id="111" instance="0" logpolicy="inherit" network="local" service="lumistore">
<ns4:properties xsi:type="soapenc:Struct">
<ns4:datasources soapenc:arrayType="xsd:ur-type[1]" xsi:type="soapenc:Array">
<ns4:item soapenc:position="[0]" xsi:type="soapenc:Struct">
<ns4:properties xsi:type="soapenc:Struct">
<ns4:bus xsi:type="xsd:string">brildata</ns4:bus>
<ns4:topics xsi:type="xsd:string">tcds,beam,bestlumi,bcm1fagghist,bcm1flumi,bcm1fbkg,pltaggzero,pltlumizero,hfoclumi,hfOcc1Agg,bunchmask,ScopeData,atlasbeam,hfetlumi,hfEtSumAgg,hfafterglowfrac,hfEtPedestal,dtlumi,bunchlength,radmonraw,radmonflux,radmonlumi,pltslinklumi,bcm1futca_bkg12,bcm1futca_background,bcm1futcalumi,remuslumi,remuslumi_5514,remuslumi_5515,remuslumi_5516,remuslumi_5517,bcm1futca_agg_hist</ns4:topics>
</ns4:properties>
</ns4:item>
</ns4:datasources>
<ns4:maxstalesec xsi:type="xsd:unsignedInt">30</ns4:maxstalesec>
<ns4:checkagesec xsi:type="xsd:unsignedInt">10</ns4:checkagesec>
<ns4:maxsizeMB xsi:type="xsd:unsignedInt">120</ns4:maxsizeMB>
<ns4:fileformat xsi:type="xsd:string">hd5</ns4:fileformat>
<ns4:filepath xsi:type="xsd:string">/scratch/central_current</ns4:filepath>
<ns4:nrowperwbuf xsi:type="xsd:unsignedInt">102</ns4:nrowperwbuf>
<ns4:workinterval xsi:type="xsd:unsignedInt">50000</ns4:workinterval>
</ns4:properties>
</xc:Application>

这是我用来解析字符串

的代码
import xml.etree.ElementTree as ET
root = ET.fromstring(xml)
node = root.find(field['xpath'], ns)

,

field['xpath'] = ".//xc:Application[@class='bril::lumistore::Application']/lst:properties/lst:datasources/lst:item[0]/lst:properties/lst:topics"

ns = {'xc': 'http://path/XMLConfiguration-30', 'lst': 'urn:application-urn:bril::lumistore::Application'}

我得到以下错误

XPath position >= 1 expected
Traceback (most recent call last):
File "/usr/lib64/python3.6/xml/etree/ElementPath.py", line 263, in iterfind
selector = _cache[cache_key]
KeyError: (".//xc:Application[@class='bril::lumistore::Application']/lst:properties/lst:datasources/lst:item[0]/lst:properties/lst:topics", (('lst', 'urn:application-urn:bril::lumistore::Application'), ('xc', 'http://path/XMLConfiguration-30')))
During handling of the above exception, another exception occurred:
SyntaxError: XPath position >= 1 expected

任何帮助都是非常感谢的

注意:使用Python 2.7似乎工作得很好,但不是Python 3.6。

XPath的位置从1开始;不是0。

所以位置谓词[0]在:

lst:item[0]

不会选择任何东西。

如果要选择lst:datasources的第一个lst:item子节点,使用:

lst:item[1]

最新更新