'How to remove wsse:Security tag using XSLT?

I have following xml which I want to remove the two lines, i.e. 'wsu:Created' and 'wsse:Nonce':

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
   <SOAP-ENV:Header>
      <wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" SOAP-ENV:mustUnderstand="1">
         <wsse:UsernameToken>
            <wsse:Username>test</wsse:Username>
            <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">test</wsse:Password>
            <wsu:Created
                    xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">2022-05-03T16:05:18.952Z
            </wsu:Created>
            <wsse:Nonce>+2Z+Xw8qQSBhES2ESxZoPQ==</wsse:Nonce>
         </wsse:UsernameToken>
      </wsse:Security>
      <ns:version xmlns:ns="http://example.com/m2m xmlns:ns0=http://schemas.xmlsoap.org/soap/envelope/" xmlns:xs="http://www.w3.org/2001/XMLSchema xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance">2.0.0</ns:version>
   </SOAP-ENV:Header>
   <SOAP-ENV:Body>

I tried with following XSLT (to remove the Nonce first) but it won't work:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:m2m="http://example.com.com/m2m" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" version="1.0">
   <xsl:output omit-xml-declaration="yes" />
   <!-- Complete copy of the request -->
   <xsl:template match="@* | node()">
      <xsl:copy>
         <xsl:apply-templates select="@* | node()" />
      </xsl:copy>
   </xsl:template>
   <xsl:template match="Envelope/Header/Security/UsernameToken/Nonce"/>
</xsl:stylesheet>

or

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:m2m="http://example.com.com/m2m" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" version="1.0">
   <xsl:output omit-xml-declaration="yes" />
   <!-- Complete copy of the request -->
   <xsl:template match="@* | node()">
      <xsl:copy>
         <xsl:apply-templates select="@* | node()" />
      </xsl:copy>
   </xsl:template>
   <xsl:template match="wsse:Nonce"/>
</xsl:stylesheet>

Appreciate anyone familiar with XSLT to help on this, thank you so much! 🙏🏼🙏🏼🙏🏼



Solution 1:[1]

You have declare the namespaces and the use them like this:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
  xmlns:m2m="http://example.com.com/m2m" 
  xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" 
  xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"
  xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"
  version="1.0">
  <xsl:strip-space elements="*"/>
  <xsl:output omit-xml-declaration="yes"  indent="yes"/>
  <!-- Complete copy of the request -->
  <xsl:template match="@* | node()">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()" />
    </xsl:copy>
  </xsl:template>
  
  <xsl:template match="wsse:Nonce|wsu:Created"/>
</xsl:stylesheet>

Applying the above xslt on this source xml:

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
  <SOAP-ENV:Header>
    <wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" SOAP-ENV:mustUnderstand="1">
      <wsse:UsernameToken>
        <wsse:Username>test</wsse:Username>
        <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">test</wsse:Password>
        <wsu:Created
          xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">2022-05-03T16:05:18.952Z
        </wsu:Created>
        <wsse:Nonce>+2Z+Xw8qQSBhES2ESxZoPQ==</wsse:Nonce>
      </wsse:UsernameToken>
    </wsse:Security>
    <ns:version xmlns:ns="http://example.com/m2m xmlns:ns0=http://schemas.xmlsoap.org/soap/envelope/" xmlns:xs="http://www.w3.org/2001/XMLSchema xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance">2.0.0</ns:version>
  </SOAP-ENV:Header>
  <SOAP-ENV:Body/>
</SOAP-ENV:Envelope>

wil give this expected result:

<?xml-stylesheet type="text/xsl" href="xsl.xsl"?><SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
   <SOAP-ENV:Header>
      <wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"
                     SOAP-ENV:mustUnderstand="1">
         <wsse:UsernameToken>
            <wsse:Username>test</wsse:Username>
            <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">test</wsse:Password>
         </wsse:UsernameToken>
      </wsse:Security>
      <ns:version xmlns:ns="http://example.com/m2m xmlns:ns0=http://schemas.xmlsoap.org/soap/envelope/"
                  xmlns:xs="http://www.w3.org/2001/XMLSchema xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance">2.0.0</ns:version>
   </SOAP-ENV:Header>
   <SOAP-ENV:Body/>
</SOAP-ENV:Envelope>

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1