cancel
Showing results for 
Search instead for 
Did you mean: 

PI mapping: concatenating multiple records to just one field

Former Member
0 Kudos

Hi

I am trying to concatenate an unknown number of records in the source message to just one field in the target message.

The structure in the source message is:

<simple>

<record>

<string>first string value</string>

</record>

<record>

<string>second string value</string>

</record>

<record>

<string>third string value</string>

</record>

<concatstring></concatstring>

</simple>

Where the number of records in the source message is unknown.

The target message should look like this:

<simple>

<concatstring>first string value second string value third string value</concatstring>

</simple>

I tried using UDF:

public String concat(String count,String streng,Container container){

int counter = Integer.parseInt(count);

String out = " ";

for(int i=0;i<(counter);i++){

out = out + streng;

}

return out;

}

But that only picks up the value of the field in the first record and repeats that the number of counts.

Can I avoid using a UDF? If not, what is the code?

Looking forward to the answer

Mikael

Accepted Solutions (1)

Accepted Solutions (1)

Former Member

write a UDF of type Queue

set the context of string to simple

UDF should look like

String finalString = "";

int count = streng.length; // input string array count

for(int i=0;i<count;i++)

{

finalString = finalString + streng<i>;

}

result.addValue(finalString);

Former Member
0 Kudos

I do this and it's work.

only to help someone the image os UDF created on Mapping:

Former Member
0 Kudos

Hello,

In case, number of records from source structure to combine is not know, will this UDF work?

IN my case, i have a node in source structure which have occurrence as 0:unbound, and i have to combine its value separated by comma into a single string node of target structure.

Can anyone provide me the way to do it?

Regards,

Neha

Answers (5)

Answers (5)

Former Member

Hi,

TO achive this you need to remove the context for <string> source field and send whole queue as the input parameter to the UDF.

In UDF, concatenate all the queue value and return the resultant string.

Regards,

Shweta.

Former Member
0 Kudos

Hi Mikael,

Try using this XSLT code.

<?xml version='1.0'?>

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

<xsl:template match="/">

<GetCustomerInformationResponse>

<GetCustomerInformationResult>

<xsl:text disable-output-escaping="yes"><![CDATA[<![CDATA[<?xml version="1.0" encoding="UTF-8"?>]]></xsl:text>

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

<xsl:text disable-output-escaping="yes"><![CDATA[]]]]></xsl:text>

<xsl:text disable-output-escaping="yes"><![CDATA[>]]></xsl:text>

</GetCustomerInformationResult>

</GetCustomerInformationResponse>

</xsl:template>

</xsl:stylesheet>

This will solve your problem.

Regards,

Sarvseh Desai

ravi_raman2
Active Contributor
0 Kudos

Sarvesh,

Sorry could not help noticing a copy and paste..atleast clean up and remove the lines highlighted in BOLD.

<?xml version='1.0'?>

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

<xsl:template match="/">

<GetCustomerInformationResponse>

<GetCustomerInformationResult>

<xsl:text disable-output-escaping="yes"><![CDATA[<![CDATAhttp://<?xml version=\"1.0\" encoding=\"UTF-8\"?>]></xsl:text>

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

<xsl:text disable-output-escaping="yes"><!CDATA[]]]></xsl:text>

<xsl:text disable-output-escaping="yes"><![CDATA>]></xsl:text>

</GetCustomerInformationResult>

</GetCustomerInformationResponse>

</xsl:template>

</xsl:stylesheet> \

Regards

Ravi Raman

Former Member
0 Kudos

You need not pass the count to the UDF.

Change the context of string field to higher level then apply the udf(Execution type as context) as below:

public String concat(String a[],Container container){

String out = " ";

for(int i=0;i<a.length;i++){

out = out + a<i>;

}

return out;

}

Former Member
0 Kudos

This message was moderated.

santhosh_kumarv
Active Contributor
0 Kudos

Hi,

Chage the context of the String element from record node to simple node.

you can also use this UDF code... you UDF should be advanced UDF....

String out = "";

for(inti=0;i<streng.length;i++)

{

out = out + streng<i>

}

result.addValue(out);

~SaNv...

Edited by: Santhosh Kumar V on Jul 7, 2009 2:43 PM