How to remove wsu:id and namespace declaration by xslt 1.0

Viewed 68

I would like to remove the attribute Id and the namespace in the tag soap:Body by using XSLT.

My sample soap response to transform is:

<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope">
    <soap:Header/>
    <soap:Body wsu:Id="Body-a3aedbad-1ef4-44a9-9982-bfa5cf9fe25f" xmlns:wsu="http://docs.oasis- 
    open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
        <soap:Fault>         
        </soap:Fault>
    </soap:Body>
</soap:Envelope>

My already implemented xslt is:

<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
    xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"
    exclude-result-prefixes="wsu">
    <xsl:output method="xml" omit-xml-declaration="yes"
        indent="yes" />
        
    <!-- remove attribute Id with prefix wsu -->
    <xsl:template match="@wsu:Id" />

    <!-- removed unused namespace declarations in soap:Body, especially xlmns:wsu -->
    <xsl:template match="*[local-name()='Body']">
        <xsl:element name="{name()}">
            <xsl:apply-templates select="@* | node()" />
        </xsl:element>
    </xsl:template>
    

    <!-- copy all other elements -->
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()" />
        </xsl:copy>
    </xsl:template>

</xsl:stylesheet>

The output: is

<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope">
    <soap:Header/>
    <soap:Body>
        <soap:Fault xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">      
            
        </soap:Fault>
    </soap:Body>
</soap:Envelope>

The ouput should be

<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope">
    <soap:Header/>
    <soap:Body>
        <soap:Fault>         
            
        </soap:Fault>
    </soap:Body>
</soap:Envelope>

The attribute and the namespace is removed. The problem is that the namespace declaration is inherited to the child objects. Can anyone tell me a solution for my problem?

1 Answers

The second template, instead of matching <xsl:template match="*[local-name()='Body']">, needs to match <xsl:template match="soap:Body | soap:Body//*">. In addition, the namespaces in the XSLT, for reason I don't understand, differ from the ones in the XML, so you need to adapt the namespace declarations in the XSLT to say

xmlns:soap="http://www.w3.org/2003/05/soap-envelope"
xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"

(this assumes the line break in the wsu namespace in the posted sample is a formatting error).

Related