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 346175
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T11:09:09+00:00 2026-05-12T11:09:09+00:00

I use the following XPATH Query to list the object under a site. ListObject[@Title=’SomeValue’]

  • 0

I use the following XPATH Query to list the object under a site. ListObject[@Title='SomeValue']. SomeValue is dynamic. This query works as long as SomeValue does not have an apostrophe (‘). Tried using escape sequence also. Didn’t work.

What am I doing wrong?

  • 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-05-12T11:09:10+00:00Added an answer on May 12, 2026 at 11:09 am

    This is surprisingly difficult to do.

    Take a look at the XPath Recommendation, and you’ll see that it defines a literal as:

    Literal ::=   '"' [^"]* '"' 
                | "'" [^']* "'"
    

    Which is to say, string literals in XPath expressions can contain apostrophes or double quotes but not both.

    You can’t use escaping to get around this. A literal like this:

    'Some'Value'
    

    will match this XML text:

    Some'Value
    

    This does mean that it’s possible for there to be a piece of XML text that you can’t generate an XPath literal to match, e.g.:

    <elm att="&quot;&apos"/>
    

    But that doesn’t mean it’s impossible to match that text with XPath, it’s just tricky. In any case where the value you’re trying to match contains both single and double quotes, you can construct an expression that uses concat to produce the text that it’s going to match:

    elm[@att=concat('"', "'")]
    

    So that leads us to this, which is a lot more complicated than I’d like it to be:

    /// <summary>
    /// Produce an XPath literal equal to the value if possible; if not, produce
    /// an XPath expression that will match the value.
    /// 
    /// Note that this function will produce very long XPath expressions if a value
    /// contains a long run of double quotes.
    /// </summary>
    /// <param name="value">The value to match.</param>
    /// <returns>If the value contains only single or double quotes, an XPath
    /// literal equal to the value.  If it contains both, an XPath expression,
    /// using concat(), that evaluates to the value.</returns>
    static string XPathLiteral(string value)
    {
        // if the value contains only single or double quotes, construct
        // an XPath literal
        if (!value.Contains("\""))
        {
            return "\"" + value + "\"";
        }
        if (!value.Contains("'"))
        {
            return "'" + value + "'";
        }
    
        // if the value contains both single and double quotes, construct an
        // expression that concatenates all non-double-quote substrings with
        // the quotes, e.g.:
        //
        //    concat("foo", '"', "bar")
        StringBuilder sb = new StringBuilder();
        sb.Append("concat(");
        string[] substrings = value.Split('\"');
        for (int i = 0; i < substrings.Length; i++ )
        {
            bool needComma = (i>0);
            if (substrings[i] != "")
            {
                if (i > 0)
                {
                    sb.Append(", ");
                }
                sb.Append("\"");
                sb.Append(substrings[i]);
                sb.Append("\"");
                needComma = true;
            }
            if (i < substrings.Length - 1)
            {
                if (needComma)
                {
                    sb.Append(", ");                    
                }
                sb.Append("'\"'");
            }
    
        }
        sb.Append(")");
        return sb.ToString();
    }
    

    And yes, I tested it with all the edge cases. That’s why the logic is so stupidly complex:

        foreach (string s in new[]
        {
            "foo",              // no quotes
            "\"foo",            // double quotes only
            "'foo",             // single quotes only
            "'foo\"bar",        // both; double quotes in mid-string
            "'foo\"bar\"baz",   // multiple double quotes in mid-string
            "'foo\"",           // string ends with double quotes
            "'foo\"\"",         // string ends with run of double quotes
            "\"'foo",           // string begins with double quotes
            "\"\"'foo",         // string begins with run of double quotes
            "'foo\"\"bar"       // run of double quotes in mid-string
        })
        {
            Console.Write(s);
            Console.Write(" = ");
            Console.WriteLine(XPathLiteral(s));
            XmlElement elm = d.CreateElement("test");
            d.DocumentElement.AppendChild(elm);
            elm.SetAttribute("value", s);
    
            string xpath = "/root/test[@value = " + XPathLiteral(s) + "]";
            if (d.SelectSingleNode(xpath) == elm)
            {
                Console.WriteLine("OK");
            }
            else
            {
                Console.WriteLine("Should have found a match for {0}, and didn't.", s);
            }
        }
        Console.ReadKey();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The following Sitecore query works for me in the XPath Builder: /sitecore/content/Home/CareAndTreatment//*[@Title='Birth Marks and
I use the following to get a list of project files that need to
I use the following code to compile a cpp file to object file. g++
What XPath query could I use to solve the below. I'm actually using nokogiri
I tend to use external JAXB bindings files for my Schema-to-Java compilation. This works
I'm trying to use XPath in PHP and I get too many elements. This
I have the following HTML: <td onclick=fbm_bet('53987976','1','6','') class=bfbhlt> Is it possible to use Xpath
for example i use following command to find a record SELECT `users`.`mail` FROM `users`
I would like to use following sql to avoid constructing sql dynamically: SELECT CommentID,
I am working on a PHP project. There, I often use following syntax to

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.