solvit.io

Add a namespace to an XML element with SimpleXML and PHP

Solution
$node = new SimpleXMLElement('<node/>');
$node->addAttribute('xmlns', 'http://www.example.org/...');
Example
$urlSetNode = new SimpleXMLElement('<urlset/>');
$urlSetNode->addAttribute('xmlns', 'http://www.sitemaps.org/schemas/sitemap/0.9');
// ...

// The generated XML document will contain:
// <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
//   ...
// </urlset>
Find more solutions about: , , ,