PHP remove parant tag from XML

46 Views Asked by At

I have the following XML document, and I want to remove the parent element <trust:RequestedSecurityToken>.

<?xml version="1.0"?>
<trust:RequestedSecurityToken xmlns:trust="http://docs.oasis-open.org/ws-sx/ws-trust/200512" xmlns:xenc="http://www.w3.org/2001/04/xmlenc#" xmlns:default="http://www.w3.org/2000/09/xmldsig#" xmlns:o="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
  <xenc:EncryptedData xmlns:xenc="http://www.w3.org/2001/04/xmlenc#" Type="http://www.w3.org/2001/04/xmlenc#Element">
    <xenc:EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#aes256-cbc"/>
    <default:KeyInfo xmlns="http://www.w3.org/2000/09/xmldsig#">
      <xenc:EncryptedKey xmlns:e="http://www.w3.org/2001/04/xmlenc#">
        <xenc:EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#rsa-oaep-mgf1p">
          <default:DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"/>
        </xenc:EncryptionMethod>
        <default:KeyInfo>
          <o:SecurityTokenReference xmlns:o="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
            <default:X509Data>
              <default:X509IssuerSerial>
                <default:X509IssuerName>ss</default:X509IssuerName>
                <default:X509SerialNumber>ss</default:X509SerialNumber>
              </default:X509IssuerSerial>
            </default:X509Data>
          </o:SecurityTokenReference>
        </default:KeyInfo>
        <xenc:CipherData>
          <xenc:CipherValue>e/+5KDiAWPJK6Hc3iKQ2cZ/+nQV9h7/+/jC6c0XF8UUNZ+++8wu3i+cqxZOF5213q/5RYxsMmMcKUJaHBQZA/ucC2IZwjWZ7hv2b4W9LBYODRWDUWG4ABIeHph9r/mM+XGLnKM796DEOB8hLzvPieV54pHDhLSOpWQ==</xenc:CipherValue>
        </xenc:CipherData>
      </xenc:EncryptedKey>
    </default:KeyInfo>
    <xenc:CipherData>
      <xenc:CipherValue>KiS3+2V71jp5vlq3ND5MtwcfKJmIWrmu5WT+//+Hhyh2ugF7jNT2r4gOUSOe72ryRSvoPyZf0wmN1juImZohRCsWIdyiXGicSxIdWQLmJQJogF3Uz/E85zLdIzslSD4cRg17VbfQl859ZDBcAGd
      </xenc:CipherValue>
    </xenc:CipherData>
  </xenc:EncryptedData>
</trust:RequestedSecurityToken>

I have tried following code but it prints the same XML.

$xml = new SimpleXMLElement($xmlString);

$trustRequestedSecurityToken = $xml->children('trust', true)
    ->RequestedSecurityToken;
unset($trustRequestedSecurityToken[0]);
   
echo $xml->asXML();
1

There are 1 best solutions below

1
talha2k On

Namespaces are essential in XML to avoid naming conflicts. SimpleXMLElement needs to be made aware of them for proper manipulation.

$xml = new SimpleXMLElement($xmlString);

$xml->registerXPathNamespace('trust', 'http://docs.oasis-open.org/ws-sx/ws-trust/200512'); 

$trustRequestedSecurityToken = $xml->xpath('//trust:RequestedSecurityToken')[0];

unset($trustRequestedSecurityToken[0]); 

echo $xml->asXML();

registerXPathNamespace is used to associate the prefix 'trust' with the correct namespace URI. This is needed for targeting the element correctly.

The xpath method now includes the 'trust:' prefix to locate the RequestedSecurityToken element within the specified namespace.

unset() removes the referenced element.