Alexandre Rezende
Alexandre Rezende

Reputation: 17

Remove ns0 and ns1 prefix and include a new prefix in all tags with XSL

I need help to create a XSLT mapping, for remove all prefix ns (NS0 , NS1, etc) and include a new prefix in all tags.

Below the XML de input and output for examplification.

Input

<ns0:ExecuteMultipleOperations xmlns:ns0="http://www.example.com">
    <ns0:Operations>
        <ns0:Operation>
            <ns0:Action>Create</ns0:Action>
            <ns0:Object>
                <ns1:SOUser xmlns:ns1="http://www.example.com">
                    <ns1:FullName>My Full Name</ns1:FullName>
                    <ns1:EmailAddress>[email protected]</ns1:EmailAddress>
                    <ns1:Active>1</ns1:Active>
                </ns1:SOUser>
            </ns0:Object>
        </ns0:Operation>
    </ns0:Operations>
    <ns0:OneTransaction>true</ns0:OneTransaction>
    <ns0:ContinueOnError>true</ns0:ContinueOnError>
</ns0:ExecuteMultipleOperations>

Output

<clic:ExecuteMultipleOperations xmlns:clic="http://www.example.com">
    <clic:Operations>
        <clic:Operation>
            <clic:Action>Create</clic:Action>
            <clic:Object>
                <clic:SOUser>           
                    <clic:FullName>My Full Name</clic:FullName>
                    <clic:EmailAddress>[email protected]</clic:EmailAddress>
                    <clic:Active>1</clic:Active>
                </clic:SOUser>
            </clic:Object>
        </clic:Operation>
    </clic:Operations>
    <clic:OneTransaction>true</clic:OneTransaction>
    <clic:ContinueOnError>true</clic:ContinueOnError>
</clic:ExecuteMultipleOperations>

Any sugestion ?

Upvotes: 1

Views: 1672

Answers (1)

Aniket V
Aniket V

Reputation: 3247

Please try the below XSLT that matches all the elements having prefix ns0 and ns1 and replaces their names with clic prefix.

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:clic="http://www.example.com" 
    xmlns:ns0="http://www.example.com"
    xmlns:ns1="http://www.example.com">
    <xsl:output method="xml" />
    <xsl:strip-space elements="*" />

     <xsl:template match="ns0:* | ns1:*">
        <xsl:element name="clic:{local-name()}">
            <xsl:apply-templates select="@* | node()" />
        </xsl:element>
    </xsl:template>
</xsl:stylesheet>

Output

<clic:ExecuteMultipleOperations xmlns:clic="http://www.example.com">
    <clic:Operations>
        <clic:Operation>
            <clic:Action>Create</clic:Action>
            <clic:Object>
                <clic:SOUser>
                    <clic:FullName>My Full Name</clic:FullName>
                    <clic:EmailAddress>[email protected]</clic:EmailAddress>
                    <clic:Active>1</clic:Active>
                </clic:SOUser>
            </clic:Object>
        </clic:Operation>
    </clic:Operations>
    <clic:OneTransaction>true</clic:OneTransaction>
    <clic:ContinueOnError>true</clic:ContinueOnError>
</clic:ExecuteMultipleOperations>

Upvotes: 3

Related Questions