cancel
Showing results for 
Search instead for 
Did you mean: 

Map values based on first occurence

Former Member
0 Kudos

Hi,

We have a source and target structure of same format like below.

<item>

<key></key>

<Value><Value>

</item>

But while mapping to target. The Value field after first occurrence of key ZZ. Should be sent as X. Help to advice in achieving this.

Ex: Source Structure

<item>

<key>10</key>

<Value>2<Value>

</item>

<item>

<key>10</key>

<Value>2<Value>

</item>

<item>

<key>ZZ</key>

<Value>2<Value>

</item>

<item>

<key>10</key>

<Value>2<Value>

</item>

<key>10</key>

<Value>2<Value>

</item>

Target Structure

<item>

<key>10</key>

<Value>2<Value>

</item>

<item>

<key>10</key>

<Value>2<Value>

</item>

<item>

<key>ZZ</key>

<Value>2<Value>

</item>

<item>

<key>10</key>

<Value>X<Value>

</item>

<key>10</key>

<Value>X<Value>

</item>

Accepted Solutions (1)

Accepted Solutions (1)

pvishnuvardan_reddy
Active Contributor
0 Kudos

Hi Praveen,

Try the below udf and check the outcome. Make sure you use remove context for both before passing the inputs key and value to the udf.

Code Snippet:

int flag=0;

for(int i=0,j=0;i<key.length;i++)

{

       if((flag==1) && (j==1))

      result.addValue("X");

      else

     result.addValue(value[i]);

  

     if(key[i].equals("ZZ"))

     {

        flag=1;

        j++;

     }

}

Answers (2)

Answers (2)

vinaymittal
Contributor
0 Kudos

Hi
Praveen,

do a one to one mapping for key field.

1)argument 1: key

2)argument 2: value

3) result of type ResultList

execution type: all values of a context

use remove context for both the arguments before passing them to target udf

use the below udf

boolean flag = false;

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

{

     if(key[i].equalsIgnoreCase("ZZ") && flag == false)

     {

          result.addValue(value[i]);

          flag = true;

     }

     else if(flag == true)

          result.addValue("X");

     else

          result.addValue(value[i]);

}

//it handles cases when there are more than one ZZ

source

10

2

ZZ

2

10

2

ZZ

3

10

4

target

10 2, ZZ 2, 10 X, ZZ X, 10 X

all the ZZ after the first ZZ too will have X

Harish
Active Contributor
0 Kudos

Hi Praveen,

Did you tried sort and sortbykey functions?

regards,

Harish

Former Member
0 Kudos

This message was moderated.