使用PHP访问XML中元素的值

I am trying to access an xml file with the following content

<?xml version="1.0" encoding="UTF-8"?>
<descriptions> 
  <image> 
    <name>1.jpg</name>  
    <text>Investor Return</text> 
  </image> 
</descriptions>

How do I retrieve the value of "text" for "name" = 1.jpg? I have tried to use $desc = $xml->xpath('image[name="'.$file.'"]/text'); but it gives an array with contains a single simplexmlelement (no value). Can someone suggest a solution?

Thanks

Sachin

Try to use //image instead of just image (although, your original XPath also worked for me, see the demo here):

$raw = <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<descriptions> 
  <image> 
    <name>1.jpg</name>  
    <text>Investor Return</text> 
  </image> 
</descriptions>
XML;
$file = "1.jpg";
$xml = simplexml_load_string($raw);
$result = $xml->xpath('//image[name="'.$file.'"]/text');
echo $result[0];

eval.in demo

output :

Investor Return