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?
This is surprisingly difficult to do.
Take a look at the XPath Recommendation, and you’ll see that it defines a literal as:
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:
will match this XML text:
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.:
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
concatto produce the text that it’s going to match:So that leads us to this, which is a lot more complicated than I’d like it to be:
And yes, I tested it with all the edge cases. That’s why the logic is so stupidly complex: