title: PHP DOMDocument - how to add Namespace declaration?
body: <p>I’m creating a dynamic sitemap.xml based on the following code</p>
  
  <pre><code> &lt;?php
  
   $dom = new DOMDocument();
  
   $dom-&gt;encoding = 'utf-8';
   $dom-&gt;xmlVersion = '1.0';
   $dom-&gt;formatOutput = true;
  
   $xml_file_name = 'SM_listings'.$mid.'.xml';
  
   $root = $dom-&gt;createElement('urlset');
  
  
       while(!$listings-&gt;atEnd()) {
           $url_node = $dom-&gt;createElement('url');
           $child_node_loc = $dom-&gt;createElement('loc', urlTarget.$listings-&gt;getColumnVal('invId'));
      $url_node-&gt;appendChild($child_node_loc);
  
      $child_node_date = $dom-&gt;createElement('lastmod', $listings-&gt;getColumnVal('Submit_date'));
      $url_node-&gt;appendChild($child_node_date);
    $root-&gt;appendChild($url_node);
  
  
   $listings-&gt;moveNext();
                              }
   $listings-&gt;moveFirst(); //return RS to first record
  
  
   $dom-&gt;appendChild($root);
   $dom-&gt;save($xml_file_name);
  
   echo "$xml_file_name has been successfully created";
  
   ?&gt;
  </code></pre>
  
  <p>This works but Google is not happy there is not a Namespace declaration in the 'urlset' node.
  Google error:  “Your Sitemap or Sitemap index file doesn't declare the expected namespace: <a href="http://www.sitemaps.org/schemas/sitemap/0.9" rel="nofollow noreferrer">http://www.sitemaps.org/schemas/sitemap/0.9</a>"</p>
  
  <p>If I change the code to :</p>
  
  <pre><code>$root = $dom-&gt;createElement('urlset 
  xmlns=“http://www.sitemaps.org/schemas/sitemap/0.9"');
  </code></pre>
  
  <p>it fails to generate the xml file and references the following:</p>
  
  <pre><code>Fatal error: Uncaught exception 'DOMException' with message 'Invalid Character Error' in E:\Domain.com\siteMap-generator1.php:54 Stack trace: #0 E:\Domain.com\siteMap-generator1.php(54): DOMDocument-&gt;createElement('urlset xmlns=\\"...') #1 {main} thrown in X:\Domain.com\siteMap-generator1.php on line 54
  </code></pre>
  
  <p>In testing I see that the node requires a very specific name/string:</p>
  
  <pre><code>$root = $dom-&gt;createElement(‘urlset 123’);  FAILS
  $root = $dom-&gt;createElement(‘urlset-123');  WORKS
  </code></pre>
  
  <p>BUT the closing node also balances such as:</p>
  
  <pre><code>&lt;url&gt;
      &lt;urlset-123&gt;
          &lt;loc&gt;some value&lt;/loc&gt;
      &lt;/urlset-123&gt;
  &lt;url&gt;
  </code></pre>
  
  <p>QUESTION: How to properly add the required Namespace and also not have it as part of the closing node element?</p>
  
  <p>I tried the following to append the value but that also failed:</p>
  
  <pre><code>$dom-&gt;nameSpace = ' http://www.sitemaps.org/schemas/sitemap/0.9';
  
  $root = $dom-&gt;createElement('urlset + nameSpace');
  </code></pre>