In my PHP script, I\'m using XPATH to search nodes for text. Everything works swimmingly -except - when I search for a word with an apostrophe.
basically my code lo
(which I'm forced to use since this is PHP)
perhaps http://basex.org/api might be worth a look/try. It allows you to use XQuery/XPath and communicates either via REST or sockets. Apart from that I'd recommend salathes solutions.
As Google shared with you, you cannot escape an apostrophe in XPath. The simplest workaround is to use a different quote character around the string parts of the query.
$nodes = $xml->xpath('//line[contains(translate(text(),"'.$upper.'","'.$lower.'"),"'.$search.'")]');
Of course, the above is only useful if you don't want to allow double-quotes in the search value. If that might be necessary, then you could move the searching/comparison into PHP-land using the method that Gordon pointed out in your previous question.
Well i was in the same quest, and after a moment i found that's there is no support in xpath for this, quiet disappointing! But well we can always work around it!
I wanted something simple and straight froward. What i come with is to set your own replacement for the apostrophe, kind of unique code (something you will not encounter in your xml text) , I chose //apos// for example. now you put that in both your xml text and your xpath query . (in case of xml you didn't write always we can replace with replace function of any editor). And now how we do? we search normally with this, retrieve the result, and replace back the //apos// to '.
bellow some samples from what i was doing:
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);
}
the 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 the call in the php file (generated html):
<span><?php echo xml_lang_body("If you don//apos//t know which server, Click here for automatic connection")?>