cancel
Showing results for 
Search instead for 
Did you mean: 

Need help to change prefix from ns1 to tns

Former Member
0 Kudos

Hi Experts ,

My scenario is Soap to File ( Sync) . Sender legacy system   is expecting the response message with prefix tns in below format .

<tns:Data xmlns:tns="com.cad.xyz">

            <HUMLast>00393155961240131287</HUMLast>

</tns:Data>

PI is able to send the response message in below format

<ns1:HumLastRequest xmlns:ns1="urn :cad.xyz">

  <HUMLast>00393155965135748871</HUMLast>

  </ns1:HumLastRequest>

How to remove Prefix ns1  and add prefix  tns to match the requirement  .

Expected result :

---------------------

<tns:HumLastRequest xmlns:tns="urn :cad.xyz">

  <HUMLast>00393155965135748871</HUMLast>

  </tns:HumLastRequest>

Please share any code if anyone has already gone through such requirements .

Note : I cannot use XMLAnonymizerBean at SOAP Sender channel as its a Synchronous scenario and response message need to have  Prefix tns .

Accepted Solutions (1)

Accepted Solutions (1)

Former Member

Aziz,

Try this code, working fine.

Paste the code in Attributes and Methods in Functions tab, Message Mapping Editor.

public void transform(TransformationInput in, TransformationOutput out)

throws StreamTransformationException {

try {

  String sourcexml = "";

  String targetxml ="";

  String line ="";

  InputStream ins =    in.getInputPayload().getInputStream();

  BufferedReader br = new BufferedReader( new InputStreamReader(ins));

  while ((line = br.readLine()) != null)   

  sourcexml +=line+"\n";  

  br.close();

  targetxml =sourcexml;

  targetxml = targetxml.replaceAll("ns0", "pfx");

  out.getOutputPayload().getOutputStream().write(targetxml.getBytes());

}

catch (Exception e)

{

    throw new StreamTransformationException(e.getMessage());  

}

}

Thanks,

Dhileep.

former_member184789
Active Contributor
0 Kudos

Hi,

Just one modification in the code:

  targetxml = targetxml.replaceAll("ns0", "pfx");

should be replaced by

  targetxml = targetxml.replaceAll("ns1", "tns");

Former Member
0 Kudos

Hi Dhileep,

Can i use the above java code as a java mapping in PI .?

Former Member
0 Kudos

Aziz,

No ... its a part of Graphical Mapping ,use Message Mapping in Operation mapping .

There won`t be Signature Variables in Attributes and Methods.This conversion will work on xml directly.

Thanks,

Dhileep.

Former Member
0 Kudos

Hi Dhileep,

I tried the same way .

I have 2 mapping . Request message mapping and Response Message mapping .

I have placed the java code as per the above screen shot under

Responsemessagemapping--->Functions tab--->attributes and Methods .

But when i tried to test the message using Test Tab its not giving any results . I think i have done something wrong here ?

regards

Aziz khan

0 Kudos

Thanks Dhileep kumar,

never expected changing ns1/ns0 from SAP PI messages to other like tns would be this easy.

I was able to do this with another Message Mapping after main mapping and adding the code you provided in the Functions Tab.

Answers (3)

Answers (3)

Muniyappan
Active Contributor
0 Kudos

this blog can be helpful.

gagandeep_batra
Active Contributor
0 Kudos

Hi Aziz

You can use  that bean For response message, place the module after the adapter module call.

check below blog for more info

:



Regards

GB


iaki_vila
Active Contributor
0 Kudos

Hi,

You can try with a XSL transformation:


<?xml version="1.0" encoding="UTF-8"?>

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:output method="xml" version="1.0" omit-xml-declaration="no" indent="yes"/>

    <xsl:template match="/">

        <tns:HumLastRequest xmlns:tns="urn :cad.xyz">

            <xsl:copy-of select="./child::*/child::*"/>

        </tns:HumLastRequest>

    </xsl:template>

</xsl:stylesheet>

I think it could be done more generally, but the last XSL example  works too.

Regards.

Former Member
0 Kudos

Hi Inaki,

when i use the above xslt i get the below result

<?xml version="1.0" encoding="UTF-8"?>

<tns:HumResponse xmlns:tns="urn :cad.xyz">

<HUMLast xmlns:ns1="urn :cad.xyz">qwe</HUMLast>

</tns:HumResponse>

I am able to get the prefix  tns at root node HumResponse  but i am getting namespace xmls and ns1 at each node .

do i need to change something in the xslt  code ?

regards,

Aziz khan .