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
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:
If you don//apos//t know which server, Click here for automatic connection Si vous ne savez pas quelle serveur, Cliquez ici pour une connexion automatique إذا لا تعرفوا أي سرفير, إضغطوا هنا من أجل إتصال تلقائي
and the call in the php file (generated html):