title: Error while adding xmlns:xsi attribute namespace to root element
body: <p>I have a xml called sample.xml</p>
  
  <pre><code>&lt;?xml version="1.0"?&gt;
  &lt;note&gt;
           &lt;to&gt;Tove&lt;/to&gt;
            &lt;from&gt;Jani&lt;/from&gt;
            &lt;heading&gt;Reminder&lt;/heading&gt;
            &lt;body&gt;Don't forget me this weekend!&lt;/body&gt;
  &lt;/note&gt; 
  </code></pre>
  
  <p>and sample.xsd to validate sample.xml</p>
  
  <pre><code>&lt;?xml version="1.0"?&gt;
  &lt;xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
  targetNamespace="http://www.w3schools.com"
  xmlns="http://www.w3schools.com"
  elementFormDefault="qualified"&gt;
  
           &lt;xs:element name="note"&gt;
                    &lt;xs:complexType&gt;
                             &lt;xs:sequence&gt;
                                      &lt;xs:element name="to" type="xs:string"/&gt;
                                      &lt;xs:element name="from" type="xs:string"/&gt;
                                      &lt;xs:element name="heading" type="xs:string"/&gt;
                                      &lt;xs:element name="body" type="xs:string"/&gt;
                             &lt;/xs:sequence&gt;
                    &lt;/xs:complexType&gt;
           &lt;/xs:element&gt;
  
  &lt;/xs:schema&gt; 
  </code></pre>
  
  <p>I'm trying to validate sample.xml through my php code and since there are no namespaces in sample.xml I'm adding the namespace from the php code.</p>
  
  <pre><code>&lt;?php
  
  error_reporting(E_ALL);
  ini_set('display_errors', 'On');
  
  $file = 'sample.xml';
  $schema = 'sample.xsd';
  $doc = new DOMDocument();
  $doc-&gt;load($file);
  
  $doc-&gt;createAttributeNS('http://www.w3schools.com', 'xmlns');
  $doc-&gt;createAttributeNS('http://www.w3.org/2001/XMLSchema-instance', 'xmlns:xsi');
  $doc-&gt;createAttributeNS('http://www.w3schools.com sample.xsd', 'xsi:schemaLocation');
  
  print $doc-&gt;saveXML();
  
  
  if ($doc-&gt;schemaValidate($schema)) {
           print "$file is valid.\n";
  } else {
           print "$file is invalid.\n";
  }
  ?&gt;
  </code></pre>
  
  <p>and when I run the code I get the following error</p>
  
  <blockquote>
    <p>Fatal error: Uncaught exception 'DOMException' with message 'Namespace
    Error' in /var/www/xsd/test_sample.php:20 Stack trace: #0
    /var/www/xsd/test_sample.php(20):
    DOMDocument->createAttributeNS('http://www.w3.o...', 'xmlns:xsi') #1
    {main} thrown in /var/www/xsd/test_sample.php on line 20</p>
  </blockquote>
  
  <p>if I comment out the line <code>$doc-&gt;createAttributeNS('http://www.w3.org/2001/XMLSchema-instance', 'xmlns:xsi');</code> the code runs fine but then I'm not able to validate the xml as it says <code>sample.xml is invalid</code></p>
  
  <p>Can some one help me with this code?</p>