cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Replace namespace prefix in XML using XSLT mapping

former_member60331
Participant
0 Kudos
3,208

Hi Experts,

Can you please help me with the XSLT code to replace the below xml to the one I need. There are three prefixes ns0,ns1 and ns2 which are to be replaced with tem,ep4 and ep41.

Input Structure

<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns0="http://tempuri.org/" xmlns:ns1="http://schemas.datacontract.org/2004/07/EP4.Integration.Entities.Common" xmlns:ns2="http://schemas.datacontract.org/2004/07/EP4.Integration.Entities">
   <soapenv:Header />
   <soapenv:Body>
      <ns0:PrintCertificateByContractNumber>
         <ns0:ContractNumber>300004</ns0:ContractNumber>
         <ns0:Credentials>
            <ns1:Password>a2fP2te254b0d0d74bDe</ns1:Password>
            <ns1:ResponseStatusList>
               <ns2:ResponseStatus>
                  <ns2:ErrorMessage />
                  <ns2:ResponseType>Error</ns2:ResponseType>
               </ns2:ResponseStatus>
            </ns1:ResponseStatusList>
            <ns1:UserName>e.eppcointegration</ns1:UserName>
            <ns1:UserReferenceNumber />
         </ns0:Credentials>
      </ns0:PrintCertificateByContractNumber>
   </soapenv:Body>
</soapenv:Envelope>

Output Structure

<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ep4="http://schemas.datacontract.org/2004/07/EP4.Integration.Entities.Common" xmlns:ep41="http://schemas.datacontract.org/2004/07/EP4.Integration.Entities" xmlns:tem="http://tempuri.org/">
   <soapenv:Header />
   <soapenv:Body>
      <tem:PrintCertificateByContractNumber>
         <tem:ContractNumber>300001</tem:ContractNumber>
         <tem:Credentials>
            <ep4:Password>a2fP2te254b0d0d74bDe</ep4:Password>
            <ep4:ResponseStatusList>
               <ep41:ResponseStatus>
                  <ep41:ErrorMessage />
                  <ep41:ResponseType>Error</ep41:ResponseType>
               </ep41:ResponseStatus>
            </ep4:ResponseStatusList>
            <ep4:UserName>e.eppcointegration</ep4:UserName>
            <ep4:UserReferenceNumber>1234567890</ep4:UserReferenceNumber>
         </tem:Credentials>
      </tem:PrintCertificateByContractNumber>
   </soapenv:Body>
</soapenv:Envelope>

Accepted Solutions (0)

Answers (3)

Answers (3)

Hi Akash,

it seems that a similar issue has been already resolved in below thread:

https://answers.sap.com/questions/12365647/xslt-mapping-to-replace-namespace-prefix.html

Regards.

Michal

former_member60331
Participant
0 Kudos

Hi Michal,

Thanks for your response.

When I use the xslt in the above thread, I get the output as below:

XSLT:



<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://sample.com/s" xmlns:ns0="http://tempuri.org/" xmlns:ns1="http://schemas.datacontract.org/2004/07/EP4.Integration.Entities.Common" xmlns:ns2="http://schemas.datacontract.org/2004/07/EP4.Integration.Entities" 
xmlns:tem="http://tempuri.org/" xmlns:ep4="http://schemas.datacontract.org/2004/07/EP4.Integration.Entities.Common" xmlns:ep41="http://schemas.datacontract.org/2004/07/EP4.Integration.Entities" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="ns0 ns1 ns2">


<xsl:output method="xml" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/">
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/" xmlns:ep4="http://schemas.datacontract.org/2004/07/EP4.Integration.Entities.Common" xmlns:ep41="http://schemas.datacontract.org/2004/07/EP4.Integration.Entities">

<soapenv:Header/>

<soapenv:Body>

<xsl:copy>


<xsl:apply-templates select="@* | node()"/>


</xsl:copy>


</soapenv:Body>


</soapenv:Envelope>


</xsl:template>


<xsl:template match="ns0:*">


<xsl:element name="tem:{local-name()}">


<xsl:apply-templates select="@* | node()"/>


</xsl:element>


</xsl:template>


<xsl:template match="ns1:*">


<xsl:element name="ep4:{local-name()}">


<xsl:apply-templates select="@* | node()"/>


</xsl:element>


</xsl:template>
<xsl:template match="ns2:*">


<xsl:element name="ep41:{local-name()}">


<xsl:apply-templates select="@* | node()"/>


</xsl:element>


</xsl:template>


</xsl:stylesheet>


Output:

<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
                  xmlns="http://sample.com/s"
                  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                  xmlns:xs="http://www.w3.org/2001/XMLSchema">
   <soapenv:Header/>
   <soapenv:Body>
      <tem:PrintCertificateByContractNumber xmlns:tem="http://tempuri.org/">
         <tem:ContractNumber>300004</tem:ContractNumber>
         <tem:Credentials>
            <ep4:Password xmlns:ep4="http://schemas.datacontract.org/2004/07/EP4.Integration.Entities.Common">a2fP2te254b0d0d74bDe</ep4:Password>
            <ep4:ResponseStatusList xmlns:ep4="http://schemas.datacontract.org/2004/07/EP4.Integration.Entities.Common">
               <ep41:ResponseStatus xmlns:ep41="http://schemas.datacontract.org/2004/07/EP4.Integration.Entities">
                  <ep41:ErrorMessage/>
                  <ep41:ResponseType>Error</ep41:ResponseType>
               </ep41:ResponseStatus>
            </ep4:ResponseStatusList>
            <ep4:UserName xmlns:ep4="http://schemas.datacontract.org/2004/07/EP4.Integration.Entities.Common">e.eppcointegration</ep4:UserName>
            <ep4:UserReferenceNumber xmlns:ep4="http://schemas.datacontract.org/2004/07/EP4.Integration.Entities.Common"/>
         </tem:Credentials>
      </tem:PrintCertificateByContractNumber>
   </soapenv:Body>
</soapenv:Envelope>

It inserts the namespaces inside the tags.
deviprasad_pothireddy
Active Participant
0 Kudos

<?xml version='1.0' ?>

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns0="http://tempuri.org/" xmlns:ns1="http://schemas.datacontract.org/2004/07/EP4.Integration.Entities.Common" xmlns:ns2="http://schemas.datacontract.org/2004/07/EP4.Integration.Entities">

<xsl:template match="/">

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ep4="http://schemas.datacontract.org/2004/07/EP4.Integration.Entities.Common" xmlns:ep41="http://schemas.datacontract.org/2004/07/EP4.Integration.Entities" xmlns:tem="http://tempuri.org/">

<soapenv:Body>

<tem:PrintCertificateByContractNumber>

<tem:ContractNumber>

<xsl:value-of select="soapenv:Envelope/soapenv:Body/ns0:PrintCertificateByContractNumber/ns0:ContractNumber"/>

</tem:ContractNumber>

<tem:Credentials>

<ep4:Password>

<xsl:value-of select="soapenv:Envelope/soapenv:Body/ns0:PrintCertificateByContractNumber/ns0:Credentials/ns1:Password"/>

</ep4:Password>

<ep4:ResponseStatusList>

<ep41:ResponseStatus>

<ep41:ErrorMessage>

<xsl:value-of select="soapenv:Envelope/soapenv:Body/ns0:PrintCertificateByContractNumber/ns0:Credentials/ns1:ResponseStatusList/ns2:ResponseStatus/ns2:ErrorMessage"/>

</ep41:ErrorMessage>

<ep41:ResponseType>

<xsl:value-of select="soapenv:Envelope/soapenv:Body/ns0:PrintCertificateByContractNumber/ns0:Credentials/ns1:ResponseStatusList/ns2:ResponseStatus/ns2:ResponseType"/>

</ep41:ResponseType>

</ep41:ResponseStatus>

</ep4:ResponseStatusList>

<ep4:UserName>

<xsl:value-of select="soapenv:Envelope/soapenv:Body/ns0:PrintCertificateByContractNumber/ns0:Credentials/ns1:UserName"/>

</ep4:UserName>

<ep4:UserReferenceNumber>

<xsl:value-of select="soapenv:Envelope/soapenv:Body/ns0:PrintCertificateByContractNumber/ns0:Credentials/ns1:UserReferenceNumber"/>

</ep4:UserReferenceNumber>

</tem:Credentials>

</tem:PrintCertificateByContractNumber>

</soapenv:Body>

</soapenv:Envelope>

</xsl:template>

</xsl:stylesheet>

ask me , similarly type of requirements to provide solutions.

former_member190293
Active Contributor
0 Kudos

Hi!

<?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                    xmlns:xs="http://www.w3.org/2001/XMLSchema"
                    xmlns:tem="http://tempuri.org/"
                    xmlns:ep4="http://schemas.datacontract.org/2004/07/EP4.Integration.Entities.Common"
                    xmlns:ep41="http://schemas.datacontract.org/2004/07/EP4.Integration.Entities"
                    version="1.0">
        <xsl:output method="xml" encoding="UTF-8" indent="yes"/>
        <xsl:strip-space elements="*"/>
        <xsl:template match="/">
            <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
                              xmlns:tem="http://tempuri.org/"
                              xmlns:ep4="http://schemas.datacontract.org/2004/07/EP4.Integration.Entities.Common"
                              xmlns:ep41="http://schemas.datacontract.org/2004/07/EP4.Integration.Entities">
                <soapenv:Header/>
                <soapenv:Body>
                    <xsl:apply-templates select="@* | node()"/>
                </soapenv:Body>
            </soapenv:Envelope>
        </xsl:template>
        <xsl:template match="ns0:*" xmlns:ns0="http://tempuri.org/">
            <xsl:element name="tem:{local-name()}">
                <xsl:apply-templates select="@* | node()"/>
            </xsl:element>
        </xsl:template>
        <xsl:template match="ns1:*" xmlns:ns1="http://schemas.datacontract.org/2004/07/EP4.Integration.Entities.Common">
            <xsl:element name="ep4:{local-name()}" namespace="http://schemas.datacontract.org/2004/07/EP4.Integration.Entities.Common">
                <xsl:apply-templates select="@* | node()"/>
            </xsl:element>
        </xsl:template>
        <xsl:template match="ns2:*" xmlns:ns2="http://schemas.datacontract.org/2004/07/EP4.Integration.Entities">
            <xsl:element name="ep41:{local-name()}" namespace="http://schemas.datacontract.org/2004/07/EP4.Integration.Entities">
                <xsl:apply-templates select="@* | node()"/>
            </xsl:element>
        </xsl:template>
    </xsl:stylesheet>

Regards, Evgeniy.