I had a problem with the layout of the XML sitemap using a simple PHP xml function that has an almost equal situation, and the tag sitemapdoes not work:
$xml = simplexml_load_string('<?xml version="1.0" encoding="UTF-8"?><sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"></urlset>');
$sitemap = $xml->addChild("sitemap");
$sitemap->addChild("loc", "http://www.example.com/sitemap-1.xml");
Fatal error: call member function addChild () in boolean

This works reliably :
$xml = simplexml_load_string('<?xml version="1.0" encoding="utf-8"?><urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"></urlset>');
$url = $xml->addChild("url");
$url->addChild("loc", "http://www.example.com/sitemap-2.xml");
source
share