2013-02-21 42 views
2
子元素

XML:卸下使用SimpleXML與命名空間

<?xml version="1.0"?> 
<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"> 
    <saml:Issuer Format="urn:oasis:names:tc:SAML:2.0:nameid-format:entity">https://ssl.perquisite.net/memberweb/federation</saml:Issuer> 
    <Signature xmlns="http://www.w3.org/2000/09/xmldsig#"> 
    <SignedInfo> 
     <CanonicalizationMethod Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/> 
     <SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1"/> 
     <Reference URI="#_9069a2d7-491f-49ca-a13f-96c5bad39b14"> 
     <Transforms> 
      <Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature"/> 
      <Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"> 
      <InclusiveNamespaces xmlns="http://www.w3.org/2001/10/xml-exc-c14n#" PrefixList="#default saml ds xs xsi"/> 
      </Transform> 
     </Transforms> 
     <DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"/> 
     <DigestValue>QpoCI6jZFhQi+PuYM6UaFBL9KEU=</DigestValue> 
     </Reference> 
    </SignedInfo> 
    <SignatureValue>X8X6qSUDKa3h3+/girXCeOVxo8=</SignatureValue> 
    <KeyInfo> 
     <X509Data> 
     <X509Certificate>MIIB2...CieOpEo35g==</X509Certificate> 
     </X509Data> 
    </KeyInfo> 
    </Signature> 
    <saml:Subject> 
    <saml:NameID Format="urn:oasis:names:tc:SAML:2.0:nameid-format:transient">654d2065-6eed-401f-bf6d-a181117a5fb8</saml:NameID> 
    </saml:Subject> 
    <saml:Conditions NotBefore="2010-04-06T16:27:04Z" NotOnOrAfter="2010-04-06T16:28:04Z"/> 
    <saml:AuthnStatement AuthnInstant="2010-04-06T16:27:34Z"> 
    <saml:AuthnContext> 
     <saml:AuthnContextClassRef>urn:oasis:names:tc:SAML:2.0:ac:classes:X509</saml:AuthnContextClassRef> 
    </saml:AuthnContext> 
    </saml:AuthnStatement> 
</saml:Assertion> 

代碼:

$xml被設定爲上面的XML。

$sxml = new SimpleXMLElement($xml); 
$sxml->registerXPathNamespace('saml', 'urn:oasis:names:tc:SAML:2.0:assertion'); 
$sxml->registerXPathNamespace('sig', 'http://www.w3.org/2000/09/xmldsig#'); 

$signodes = $sxml->xpath('/saml:Assertion/sig:Signature'); 

在這一點上我想刪除SIG:簽名節點,並從 XML的所有兒童。我已經試過:

$node = dom_import_simplexml($signodes[0]); 
$node->parentNode->removeChild($node); 

和:

foreach($signodes as $item) { 
    $node = dom_import_simplexml($item); 
    $node->parentNode->removeChild($node); 
} 

和:

unset($signodes); 

和:

foreach($signodes as $node) { 
    unset($node); 
} 

,然後又回到了XML具有:

$Canonical = $sxml->asXML(); 

以上所有內容對結果XML 似乎都沒有影響(但不要給出錯誤)。

任何人有任何線索爲什麼?

+0

它看起來對我很正確。我將嘗試在本地複製它。問題會得到滿意的答覆,因爲您確實發佈了您已經嘗試過的內容,並提出了一個明確的問題,這個問題在這裏很少見。 – mkaatman 2013-02-21 16:38:21

+2

你的例子你在哪裏通過dom_import_simplexml和removechild在這裏工作:http://codepad.org/b4emqwCA – mkaatman 2013-02-21 17:11:26

+0

謝謝mkaatman,我簡化了代碼的可讀性,所以我認爲這是與我的實際代碼以及導致它「恢復」的變量的範圍。 – 2013-02-21 17:24:45

回答

0

正如你看到了什麼mkaatman說, PHP有一個方法removeChild。 (http://php.net/manual/en/domnode.removechild.php

您可以通過示例刪除小孩: $ node-> parentNode-> removeChild($ node);

+0

我已經知道這一點,我問是否有可能將它與名稱空間一起使用。答案是「是的」,我的代碼出錯了。 – 2013-02-25 11:48:54