How can XPATH be used to efficiently retrieve specific values from XML data in PHP?

To efficiently retrieve specific values from XML data in PHP using XPATH, you can use the SimpleXMLElement class along with the xpath() method to navigate through the XML structure and extract the desired data. This allows you to target specific elements or attributes within the XML document without having to manually parse the entire file.

$xml = simplexml_load_file('data.xml');
$result = $xml->xpath('//book[@id="123"]/title');

foreach ($result as $title) {
    echo $title . "\n";
}