title: PHP xmllib XSD validation namespace error: Did not expect X, expected X
body: <p>I've been trying to get xmllib to cooperate and validate a xmldsig <code>&lt;Signature&gt;</code> element via <code>DOMDocument</code>, yet it keeps producing spurious errors no matter how I throw the element at it. Apparently having the <code>&lt;Signature&gt;</code> be the <a href="https://stackoverflow.com/questions/26594261/how-to-set-namespace-xmlns-declaration-at-root-tag-with-pure-dom">root</a> is a no-go due to <code>DOMDocument</code>. I can't manage to set the <code>xmlns</code> attribute on the root properly:  Trying to validate results in:</p>
  <pre class="lang-none prettyprint-override"><code>Element 'Signature': No matching global declaration available for the validation root. 
  </code></pre>
  <p>I think It's maybe because the schema is intended to be 'included', you would want to sign 'something'  after all, not just have signature by itself. So, by creating this:</p>
  <pre><code>&lt;?xml version=&quot;1.0&quot;?&gt;
  &lt;xs:schema xmlns:xs=&quot;http://www.w3.org/2001/XMLSchema&quot;
      xmlns:ds=&quot;http://www.w3.org/2000/09/xmldsig#&quot;
      version=&quot;1.0&quot;
      elementFormDefault=&quot;qualified&quot;&gt;
  &lt;xs:import namespace=&quot;http://www.w3.org/2000/09/xmldsig#&quot; schemaLocation=&quot;xmldsig-core-schema.xsd&quot; /&gt;
  &lt;xs:element name=&quot;root&quot;&gt;
      &lt;xs:complexType&gt;
          &lt;xs:sequence&gt;
              &lt;xs:element ref=&quot;ds:Signature&quot; /&gt;
          &lt;/xs:sequence&gt;
      &lt;/xs:complexType&gt;
  &lt;/xs:element&gt;
  &lt;/xs:schema&gt;
  </code></pre>
  <p>I can now have an included signature for a dummy element.</p>
  <p>I realize PHP XML processing is rather bad, none of the tools really work all that well. However, once it actually hands things off to xmllint properly, there should be no issue doing this. Other answers suggest adding in XPath as well, but I'd like to keep things KISS; use as little PHP libraries as possible.</p>
  <p>With a couple hundred lines of code finagling I got it to cooperate and wrap just the <code>&lt;Signature&gt;</code> part of the xml file into a plain test file, inside a <code>&lt;root&gt;</code> element.</p>
  <p>I think it's doing something to corrupt the namespaces in the document. Calling <code>$myDocument-&gt;saveXML();</code> after it fails <code>$myDocument-&gt;schemaValidate();</code> returns a peculiar result:</p>
  <pre class="lang-none prettyprint-override"><code>Element 'Signature': This element is not expected. Expected is ( {http://www.w3.org/2000/09/xmldsig#}Signature ).
  </code></pre>
  <p>When the xml looks like this (I've added the formatting; the real thing is unformatted; though the whitespace should not be significant):</p>
  <pre class="lang-xml prettyprint-override"><code>&lt;?xml version=&quot;1.0&quot;?&gt;
  &lt;root&gt;
      &lt;Signature xmlns=&quot;http://www.w3.org/2000/09/xmldsig#&quot;&gt;
           (... omitted crypto ...)
      &lt;/Signature&gt;
  &lt;/root&gt;
  </code></pre>
  <p>From the reading I've done on it, the error should mean that the namespace is wrong on the <code>&lt;Signature&gt; </code> element. But here it's clearly explicitly designated with the <code>xmlns</code> to be exactly what the error message is expecting it to be. So, what's wrong here?</p>
  <p>Addendum: Code to prepare the signature for verification (in an arbitrary document) would look something like this. This is the complicated version where I've added a 'root' element instead of using the schema directly, which, see my answer below, turned out to be unnecessary, although a good test, as in more complicated scenarios where you'd want to validate a signed thing in one call to libxml it's a valid thing to want to do:</p>
  <pre class="lang-php prettyprint-override"><code>function verifySchema(\DOMDocument $doc) {        
      libxml_use_internal_errors(true);
      $new = new \DOMDocument; 
      $newRoot = $new-&gt;createElement(&quot;root&quot;);
      $root = $doc-&gt;documentElement;     
      foreach($root-&gt;childNodes as $node) {   
          if($node-&gt;nodeName == &quot;Signature&quot;) {
              $signature = $new-&gt;createElement(&quot;Signature&quot;);
              $signature-&gt;setAttributeNS('http://www.w3.org/2000/xmlns/','xmlns','http://www.w3.org/2000/09/xmldsig#');
              foreach($node-&gt;childNodes as $subnode) {
                  $signature-&gt;appendChild($new-&gt;importNode($subnode, true));
              }
              $newRoot-&gt;appendChild($signature);
              break;
          }
      }           
      $new-&gt;appendChild($newRoot);
      if(!$signature) {
          throw new \Exception(&quot;Document is not signed: No signature node found.&quot;);
      }
      if(!$new-&gt;schemaValidate(&quot;xmldsig-test.xsd&quot;)) {
         // Error handling code here.
     } else {
         return true;
     }
  }
  </code></pre>