What are some potential pitfalls when using SimpleXML to parse XHTML documents in PHP?

One potential pitfall when using SimpleXML to parse XHTML documents in PHP is that it may not handle namespaces properly, resulting in difficulty accessing elements with namespaces. To solve this issue, you can register the namespaces with SimpleXML before parsing the document.

$xml = <<<XML
<?xml version='1.0'?>
<root xmlns:foo="http://example.com">
    <foo:bar>Test</foo:bar>
</root>
XML;

$doc = simplexml_load_string($xml);

$doc->registerXPathNamespace('f', 'http://example.com');
$nodes = $doc->xpath('//f:bar');

foreach ($nodes as $node) {
    echo (string) $node;
}