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")?>
Allal mohamed
source share