How can Xpath be used to access parent elements from child elements in PHP?

To access parent elements from child elements using XPath in PHP, you can use the "parent::" axis in your XPath expression. This allows you to navigate up the XML tree and select the parent element of a given child element. By specifying "parent::" followed by the parent element's tag name, you can retrieve the parent element based on the context of the child element.

// Load the XML file
$xml = simplexml_load_file('example.xml');

// Find all child elements named 'child'
$children = $xml->xpath('//child');

// Loop through each child element and access its parent
foreach ($children as $child) {
    $parent = $child->xpath('parent::*');
    // Do something with the parent element
}