Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
869 views
in Technique[技术] by (71.8m points)

ruby - Escape single quote in XPath with Nokogiri?

I have an XPath query that looks like this, with both single and double quotes. How do I escape the apostrophe properly so that the query works?

I tried:

"//li[text()='Frank's car']"

but it doesn't seem to do it for me.

Any ideas?

 "//li[text()='Frank's car']"
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

XPath doesn’t have any way of escaping special characters, so this is a little tricky. A solution in this specific case would be to use double quotes instead of single quotes in the XPath expression:

text()="Frank's car"

If you did this, you’d have to escape the quotes from Ruby if you used double quotes around the whole expression:

"//li[text()="Frank's car"]"

You could use single quotes here if you aren’t doing any interpolation, and then escape the single quote:

'//li[text()="Frank's car"]'

A better option would perhaps be to make use of Ruby’s flexible quoting, so that none of the quotes would need escaping ,e.g.:

%{//li[text()="Frank's car"]}

Note that all the examples here doing escaping in Ruby, so that the string that reaches the XPath processor is //li[text()="Frank's car"].

The more general case, when the text is variable that could contain single or double quotes is more difficult. XPath’s string literals can’t contain both types of quotes; you need to construct the string using the XPath concat function.

For example, if you wanted to match the string "That's mine", he said., you would need to do something like:

text()=concat('"That', "'", 's mine", he said.')

And then you’d have to escape the quotes from Ruby (using %{} would be easiest).

I found another question on SO dealing with this issue in C#, and a thread on the Nokogiri mailing list, both of which might be worth looking at if you need to take this further.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...