Maurice M.
Maurice M.

Reputation: 11

Xpath attribute does not always work and gives Invalid predicate

I have an xml file with experimental data that I am trying to read out in Python with pandas.

The data is separated into 2 experiments, which each have 2 wavelengths (and more subnodes). I can select an attribute for the wavelength and it gives me a readout without any problems, but for both Experiments. If I try to also select an attribute for the wavelength I get the "XPathEvalError: Invalid predicate" error.

Here's the code when it works without the attribute for the Experiment Section

import pandas as pa

a = pa.read_xml(path_or_buffer=filepath, xpath="//doc:experimentSection[@sectionName]/doc:plateSection/doc:microplateData/doc:wave[@waveID=1]/doc:well[@wellID]/doc:oneDataSet", namespaces={"doc": "http://moleculardevices.com/microplateML"})

Here's the same code when it includes the experiment section attribute "Experiment#1" and now it returns Invalid predicate. If the attribute is changed to something that is not in the original file it returns "Invalid expression". So it does seem to recognize the attribute to be present

import pandas as pa

a = pa.read_xml(path_or_buffer=filepath, xpath="//doc:experimentSection[@sectionName=Experiment#1]/doc:plateSection/doc:microplateData/doc:wave[@waveID=1]/doc:well[@wellID]/doc:oneDataSet", namespaces={"doc": "http://moleculardevices.com/microplateML"})

I've copied the contents of the xml file into pastebin. The file works as expected if reverted back to xml.

Thanks for the help

I already tried finding other solutions on the internet without much success. The expected output is a 2 column dataframe with rawData and timeData for a selected Experiment and Wavelength, but only the selection of Wavelength works

Upvotes: 1

Views: 30

Answers (1)

keithwalsh
keithwalsh

Reputation: 866

The xpath needs quotes for attribute values if they include special characters such as "#".

a = pa.read_xml(
    path_or_buffer=filepath,
    xpath=(
        "//doc:experimentSection[@sectionName='Experiment#1']"
        "/doc:plateSection/doc:microplateData"
        "/doc:wave[@waveID='1']/doc:well[@wellID]/doc:oneDataSet"
    ),
    namespaces={"doc": "http://moleculardevices.com/microplateML"}
)

Upvotes: 0

Related Questions