What is the purpose of using XPath in PHP for selecting specific values from XML documents?
XPath in PHP is used to select specific values from XML documents by defining a path expression to navigate through the XML structure. This is useful when you only need to extract certain data from an XML document without having to parse the entire document.
// Load the XML file
$xml = simplexml_load_file('data.xml');
// Use XPath to select specific values
$nodes = $xml->xpath('//book[@category="fiction"]/title');
// Output the selected values
foreach ($nodes as $node) {
echo $node . "<br>";
}