Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9272243
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T15:49:33+00:00 2026-06-18T15:49:33+00:00

I have an XPath query that looks like this, with both single and double

  • 0

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']"
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T15:49:35+00:00Added an answer on June 18, 2026 at 3:49 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an XML document that looks like this. It may be that it's
I have an XML value and an XPath query like this: XML: <Firms> <Firm
I have a big xml document that looks like this: <Node name=foo> <Node name=16764764625>
Imagine I have some XML that looks like this: DECLARE @xml XML SET @xml
I have an XPath query that's trying to grab the parent of a specific
I'm using xpath to query my xml for result items that have the name
I have a snippet of XML that looks like <body> Some text.... <nodeA>....</nodeA> more
I have an xpath query that returns the description of a td (with a
Okay, so I've got some example xml that looks like this: <Node name=details> <Node
I have a malformed HTML that I can't change. Running a XPath Query doesn't

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.