What are some best practices for handling namespaces in xpath() queries for XML documents?

When working with XML documents in PHP and using XPath queries, it is important to handle namespaces properly to ensure accurate results. One common issue is when the XML document contains namespaces, which can affect the XPath query results. To solve this, you can register the namespaces with the XPath query using the `registerNamespace()` method in PHP.

$xml = '<root xmlns:ns="http://example.com"><ns:element>Value</ns:element></root>';
$doc = new DOMDocument();
$doc->loadXML($xml);

$xpath = new DOMXPath($doc);
$xpath->registerNamespace('ns', 'http://example.com');

$query = "//ns:element";
$elements = $xpath->query($query);

foreach ($elements as $element) {
    echo $element->nodeValue; // Output: Value
}