XQuery searches for text with a "single" quote

I can't figure out how to look for text containing single quotes using XPATH.

For example, I added a quote to the title of this question. Next line

$x("//*[text()='XQuery looking for text with 'single' quote']") 

Returns an empty array.

However, if I try the following

 $x("//*[text()=\"XQuery looking for text with 'single' quote\"]") 

It returns a link for the page title, but I would like to be able to accept both single and double quotes, so I cannot just adapt it for a single / double quote.

You can try it in the Chrome or Firebug console on this page.

+8
escaping xpath
source share
6 answers

Here is a hackaround (Thanks to Dimitre Novatchev) that will allow me to search for any text in xpaths, regardless of whether it contains single or double quotes. Implemented in JS, but can be easily translated into other languages.

 function cleanStringForXpath(str) { var parts = str.match(/[^'"]+|['"]/g); parts = parts.map(function(part){ if (part === "'") { return '"\'"'; // output "'" } if (part === '"') { return "'\"'"; // output '"' } return "'" + part + "'"; }); return "concat(" + parts.join(",") + ")"; } 

If I'm looking for I'm reading "Harry Potter" , I could do the following

 var xpathString = cleanStringForXpath( "I'm reading \"Harry Potter\"" ); $x("//*[text()="+ xpathString +"]"); // The xpath created becomes // //*[text()=concat('I',"'",'m reading ','"','Harry Potter','"')] 

Here's a (much shorter) version of Java. This is exactly the same as JavaScript if you delete the type information. Thanks https://stackoverflow.com/users/1850609/acdcjunior

 String escapedText = "concat('"+originalText.replace("'", "', \"'\", '") + "', '')";! 
+11
source share

In XPath 2.0 and XQuery 1.0, a string literal delimiter can be included in a string literal by doubling it:

 let $a := "He said ""I won't""" 

or

 let $a := 'He said "I can''t"' 

The agreement is borrowed from SQL.

+5
source share

This is an example :

 /*/*[contains(., "'") and contains(., '"') ]/text() 

When this XPath expression is applied to the following XML document:

 <text> <t>I'm reading "Harry Potter"</t> <t>I am reading "Harry Potter"</t> <t>I am reading 'Harry Potter'</t> </text> 

the desired, correct result is selected (one text node):

 I'm reading "Harry Potter" 

Here's a confirmation using XPath Visualizer (a free, open source tool created 12 years ago that taught XPath in an interesting way for thousands of people):

enter image description here

Perhaps the problem is that you cannot specify this XPath expression as a string in the programming language you are using - this is not an XPath problem, but a problem in your knowledge of your programming language.

+4
source share

Also, if you used XQuery instead of XPath, as the title says, you can also use xml entities:

  "&quot; for double and &apos; for single quotes" 

they also work in single quotes

+1
source share

You can do this using regex. For example (as ES6 code):

 export function escapeXPathString(str: string): string { str = str.replace(/'/g, `', "'", '`); return `concat('${str}', '')`; } 

This replaces everything ' in the input string with ', "'", ' .

Last , '' important because concat('string') is a bug.

+1
source share

Well, I was in the same quest, and after a moment I found that there was no support for xpath for this, quiet disappointment! But we can always get around this!

I needed something simple and straightforward. I came in order to install my own apostrophe replacement , a kind of unique code (something that you will not encounter in your XML text), I chose // apos // for example. Now you put this in both your XML text and your xpath request . (in the case of xml, you did not always write, we can replace the replacement function of any editor). Now how do we do it? we usually search this, extract the result, and replace //// apos // with '.

Below are some examples of what I was doing: (replace_special_char_xpath () is what you need to do)

 function repalce_special_char_xpath($str){ $str = str_replace("//apos//","'",$str); /*add all replacement here */ return $str; } function xml_lang($xml_file,$category,$word,$language){ //path can be relative or absolute $language = str_replace("-","_",$language);// to replace - with _ to be able to use "en-us", ..... $xml = simplexml_load_file($xml_file); $xpath_result = $xml->xpath("${category}/def[en_us = '${word}']/${language}"); $result = $xpath_result[0][0]; return repalce_special_char_xpath($result); } 

text in XML file:

 <def> <en_us>If you don//apos//t know which server, Click here for automatic connection</en_us> <fr_fr>Si vous ne savez pas quelle serveur, Cliquez ici pour une connexion automatique</fr_fr> <ar_sa>إذا لا تعرفوا أي سرفير, إضغطوا هنا من أجل إتصال تلقائي</ar_sa> </def> 

and call in php file (generated html):

 <span><?php echo xml_lang_body("If you don//apos//t know which server, Click here for automatic connection")?> 
0
source share

All Articles