What potential pitfalls should be considered when registering and using namespaces with DOMXPath in PHP?
When registering and using namespaces with DOMXPath in PHP, a potential pitfall to consider is that namespaces must be registered before querying the XML document. Failure to do so can result in incorrect or no results being returned. To avoid this issue, make sure to register the namespaces using the `registerNamespace()` method before executing any XPath queries.
$xml = new DOMDocument();
$xml->load('example.xml');
$xpath = new DOMXPath($xml);
$xpath->registerNamespace('ns', 'http://www.example.com/ns');
$query = '//ns:element';
$elements = $xpath->query($query);
foreach ($elements as $element) {
echo $element->nodeValue . "\n";
}