rakku
rakku

Reputation: 73

python lxml xpath cannot parse apostrophe quote

I am trying to read a parameter from an XML file with folowing path:

parameter path in xml = "node1/node2/param's name"

parser = etree.parse(xml_file_path, etree.XMLParser(encoding='utf-8', recover=True, huge_tree=True))
parser.xpath("./SPLIT/NODE[contains(text(), 'node1')]/SPLIT/NODE[contains(text(), 'node2')]/SPLIT/NODE[contains(text(), 'param's name')]")

Due to a single quote in the node name, I am getting this eeror - {XPathEvalError}Invalid Expression The same code works fine if there is no single quote.

I tried finding the solution in lxml xpath documentation but could not find any mechanism to skip this single quote. I did find a similar qustion but it does not have any answer. I also tried replacing single quote with ' but didn't work. Please let me know if there is a way to skip single quote or if I am doing something wrong here.

Upvotes: 1

Views: 205

Answers (1)

supputuri
supputuri

Reputation: 14135

You can try the below options.

parser.xpath("""./SPLIT/NODE[contains(text(), "node1")]/SPLIT/NODE[contains(text(), "node2")]/SPLIT/NODE[contains(text(), "param's name")]""")

In short

.//NODE[text()="node1/node2/param's name"]

Upvotes: 1

Related Questions