I am trying to parse a xml from a string.
Below is the xml in the string.
<xc: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>
This is the code i use to parse the string
import xml.etree.ElementTree as ET
root = ET.fromstring(xml)
node = root.find(field['xpath'], ns)
where
field['xpath'] = ".//xc:Application[@class='bril::lumistore::Application']/lst:properties/lst:datasources/lst:item[0]/lst:properties/lst:topics"
and
ns = {'xc': 'http://path/XMLConfiguration-30', 'lst': 'urn:application-urn:bril::lumistore::Application'}
I get the following error
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
Any help is much appreciated
Note: Usin Python 2.7 seems to work fine, not with Python 3.6 though.
CodePudding user response:
Positions in XPath start at 1; not 0.
So the positional predicate [0]
in:
lst:item[0]
isn't valid.
If you want to select the first lst:item
child of lst:datasources
, use:
lst:item[1]