title: Removing a child element using SimpleXML with namespaces
body: <p>XML:</p>
  
  <pre><code>&lt;?xml version="1.0"?&gt;
  &lt;saml:Assertion xmlns:saml="urn:oasis:names:tc:SAML:2.0:assertion" ID="_9069a2d7-491f-49ca-a13f-96c5bad39b14" IssueInstant="2010-04-06T16:27:34Z" Version="2.0"&gt;
    &lt;saml:Issuer Format="urn:oasis:names:tc:SAML:2.0:nameid-format:entity"&gt;https://ssl.perquisite.net/memberweb/federation&lt;/saml:Issuer&gt;
    &lt;Signature xmlns="http://www.w3.org/2000/09/xmldsig#"&gt;
      &lt;SignedInfo&gt;
        &lt;CanonicalizationMethod Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/&gt;
        &lt;SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1"/&gt;
        &lt;Reference URI="#_9069a2d7-491f-49ca-a13f-96c5bad39b14"&gt;
          &lt;Transforms&gt;
            &lt;Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature"/&gt;
            &lt;Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"&gt;
              &lt;InclusiveNamespaces xmlns="http://www.w3.org/2001/10/xml-exc-c14n#" PrefixList="#default saml ds xs xsi"/&gt;
            &lt;/Transform&gt;
          &lt;/Transforms&gt;
          &lt;DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"/&gt;
          &lt;DigestValue&gt;QpoCI6jZFhQi+PuYM6UaFBL9KEU=&lt;/DigestValue&gt;
        &lt;/Reference&gt;
      &lt;/SignedInfo&gt;
      &lt;SignatureValue&gt;X8X6qSUDKa3h3+/girXCeOVxo8=&lt;/SignatureValue&gt;
      &lt;KeyInfo&gt;
        &lt;X509Data&gt;
          &lt;X509Certificate&gt;MIIB2...CieOpEo35g==&lt;/X509Certificate&gt;
        &lt;/X509Data&gt;
      &lt;/KeyInfo&gt;
    &lt;/Signature&gt;
    &lt;saml:Subject&gt;
      &lt;saml:NameID Format="urn:oasis:names:tc:SAML:2.0:nameid-format:transient"&gt;654d2065-6eed-401f-bf6d-a181117a5fb8&lt;/saml:NameID&gt;
    &lt;/saml:Subject&gt;
    &lt;saml:Conditions NotBefore="2010-04-06T16:27:04Z" NotOnOrAfter="2010-04-06T16:28:04Z"/&gt;
    &lt;saml:AuthnStatement AuthnInstant="2010-04-06T16:27:34Z"&gt;
      &lt;saml:AuthnContext&gt;
        &lt;saml:AuthnContextClassRef&gt;urn:oasis:names:tc:SAML:2.0:ac:classes:X509&lt;/saml:AuthnContextClassRef&gt;
      &lt;/saml:AuthnContext&gt;
    &lt;/saml:AuthnStatement&gt;
  &lt;/saml:Assertion&gt;
  </code></pre>
  
  <p>code:</p>
  
  <p><code>$xml</code> is set to the above xml.</p>
  
  <pre><code>$sxml = new SimpleXMLElement($xml);
  $sxml-&gt;registerXPathNamespace('saml', 'urn:oasis:names:tc:SAML:2.0:assertion');
  $sxml-&gt;registerXPathNamespace('sig', 'http://www.w3.org/2000/09/xmldsig#');
  
  $signodes = $sxml-&gt;xpath('/saml:Assertion/sig:Signature');
  </code></pre>
  
  <p>At this point I want to remove the sig:Signature node and all children from the
  XML. I've tried:</p>
  
  <pre><code>$node = dom_import_simplexml($signodes[0]);
  $node-&gt;parentNode-&gt;removeChild($node);
  </code></pre>
  
  <p>and:</p>
  
  <pre><code>foreach($signodes as $item) {
      $node = dom_import_simplexml($item);
      $node-&gt;parentNode-&gt;removeChild($node);
  }
  </code></pre>
  
  <p>and:</p>
  
  <pre><code>unset($signodes);
  </code></pre>
  
  <p>and:</p>
  
  <pre><code>foreach($signodes as $node) {
     unset($node);
  }
  </code></pre>
  
  <p>and then getting back the XML with:</p>
  
  <pre><code>$Canonical = $sxml-&gt;asXML();
  </code></pre>
  
  <p>All of the above seem to have no effect on the resultant XML
  (but do not give errors either).</p>
  
  <p>Anyone have any clues as to why?</p>