dynobo
dynobo

Reputation: 675

python xpath: single quotationmarks and double quotationmarks

I hope this is a small problem:

I want to search for a text, that can contain doublequotes " and/or singelquotes '. Now I can use this:

"//a[contains(text(), '"+ mytext +"')]" 

or this:

'//a[contains(text(), "'+ mytext +'")]'

but if i have mytext is something like this:

a'b"c 

i get (of course) a xpath-error (Invalid XPath-Expression). How can I avoid it?

Upvotes: 2

Views: 523

Answers (2)

Michael Kay
Michael Kay

Reputation: 163272

In XPath 2.0 the delimiting quotes of a string literal can be included in the literal by doubling: "He said, ""I can't""".

In XPath 1.0, such a string can't be written as a literal, but it can be expressed using concat(): concat("He said, ", '"', "I can't", '"')

Of course, if such an XPath expression is then to be written as a string literal in a host language such as Python, the quotes must be further escaped according to Python rules.

Upvotes: 2

tdelaney
tdelaney

Reputation: 77337

Use Python triple quotes r"""a'b"c""". Inside the xpath, use an escape .

Upvotes: -1

Related Questions