cancel
Showing results for 
Search instead for 
Did you mean: 

Mapping for split a field to several subelements

Former Member
0 Kudos

Could I do this mapping? We have a complex remark field, it includes 2 or more data. And we want to separate it to several subelements. The subelements are separated by semicolon.

<b>Source:</b>

<header>
<order>A12345</order>
<customer>XXX</customer>
<remark>TESTA/1;TESTB/2;TESTC/3</remark>
</header>

<b>Target:</b>

<header>
<order>A12345</order>
<customer>XXX</customer>
  <detail>
    <field1>TESTA</field1>
    <field2>1</field2>
  </detail>
  <detail>
    <field1>TESTB</field1>
    <field2>2</field2>
  </detail>
  <detail>
    <field1>TESTC</field1>
    <field2>3</field2>
  </detail>
</header>

I try to study SplitByValue and useOneAsMany, but it seems not for it.

Accepted Solutions (1)

Accepted Solutions (1)

bhavesh_kantilal
Active Contributor
0 Kudos

Hi,

This would actually be possible even without using a Global Variable.

Use an Advanced User Defined Function. Adavnced user Defined Functions can return Multiple values.

<u><b>UDF1:</b></u>

1.Takes renmark as input.

2.Does the split on the basis of ;

3. Splits each split of step 2 on the basis of /

4. Add the output of step 3 to the resultlist with a context change.

u]<b>UDF2:</b></u>

1.Takes renmark as input.

2.Does the split on the basis of ;

3. Splits each split of step 2 on the basis of /

4. Add the output of step 3 to the resultlist with a context change.

For Advanced UDF's,

http://help.sap.com/saphelp_nw04/helpdata/en/f8/2857cbc374da48993c8eb7d3c8c87a/content.htm

Regards,

Bhavesh

Former Member
0 Kudos

I use result.addValue("XXX"); and I can add multi values now like this

<header>
<order>A12345</order>
<customer>XXX</customer>
<detail>TESTA<detail>
<detail>TESTB<detail>
<detail>TESTC<detail>
</header>

But I want some subelements, so, I try another test, this is my source code:

 result.addValue("TESTA");
 result.addValue("1");
 result.addContextChange();
 result.addValue("TESTB");
 result.addValue("2");
 result.addContextChange();
 result.addValue("TESTC");
 result.addValue("3");

The result is

<detail>
  <field1>TESTA</field1>
  <field2>TESTA</field2>
</detail>
<detail>
  <field1>TESTB</field1>
  <field2>TESTB</field2>
</detail>
<detail>
  <field1>TESTC</field1>
  <field2>TESTC</field2>
</detail>

I think the result is not correct, I checked the methods of ResultList objects, but I only found addValue(), addContextChange() and addSuppress()

http://help.sap.com/saphelp_nw04/helpdata/en/b1/83a09f668320419dbe00a741e0fe6a/content.htm

Former Member
0 Kudos

Hi Dennys,

You can tru this piece of code in your mapping...

You need 3 UDFs each for <detail>, <field1>,<field2>

<b>UDF1: For <detail></b>

String arr[] = a[0].split(";");

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

result.addValue(arr<i>);

<b>UDF2: For <field1></b>

String str = a[0];

String str1 = null;

String sarr1[] = str.split(";");

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

{

str1 = sarr1<i>;

String sarr2[] = str1.split("/");

for (int j=0; j<sarr2.length;j++){

result.addValue(sarr2[j]);

j++;

}

result.addContextChange();

}

<b>UDF2: For <field2></b>

String str = a[0];

String str1 = null;

String sarr1[] = str.split(";");

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

{

str1 = sarr1<i>;

String sarr2[] = str1.split("/");

for (int j=0; j<sarr2.length;j++){

j++;

result.addValue(sarr2[j]);

}

result.addContextChange();

}

All these functions requires one input <remark>

Regards,

Jai Shankar.

bhavesh_kantilal
Active Contributor
0 Kudos

Hi,

Can you try this,

result.addValue("TESTA");
 result.addValue("1");
 <b>result.addValue(ResultList.CC);</b>
 result.addValue("TESTB");
 result.addValue(ResultList.CC);
 result.addContextChange();
 result.addValue("TESTC");
 result.addValue("3");

Regards,

Bhavesh

Former Member
0 Kudos

It works, I just modify some syntax and the 2nd loop is not necessary.

Great thanks.

// main

String arr[] = remark[0].split(";");

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

result.addValue(arr<i>);

// 1st field

String str = remark[0];

String str1 = null;

String sarr1[] = str.split(";");

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

{

str1 = sarr1<i>;

String sarr2[] = str1.split("/");

result.addValue(sarr2[0]);

result.addContextChange();

}

// 2nd field

String str = remark[0];

String str1 = null;

String sarr1[] = str.split(";");

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

{

str1 = sarr1<i>;

String sarr2[] = str1.split("/");

result.addValue(sarr2[1]);

result.addContextChange();

}

Answers (2)

Answers (2)

bhavesh_kantilal
Active Contributor
0 Kudos

Hi,

Possible using UDF's.

You would need 2 UDF's + Global variables of Java Section to achieve this.

<u><b>Logic :</b></u>

Declare 2 Global variables in the Java section and inititliaze them to 1 in the Initlization section.

Now, write an UDF, that will read take REMARK as its input. Check the value if tghe Global variable and extract only that portion from the String and then increment the global variable by 1.

Regards,

Bhavesh

Former Member
0 Kudos

Dennys,

If the <remark> node ahs fixed length contents for each field say field1- length5 and field2 - length 1, then you can use the <b>substring</b> function to do this easily...

Regards,

Jai Shankar.

former_member206604
Active Contributor
0 Kudos

Hi,

You can also try with a simple UDF which would split remark by<b> ;</b> and then split the result by<b> /</b>.

Regards,

Prakash

Former Member
0 Kudos

> If the <remark> node ahs fixed length contents for

> each field say field1- length5 and field2 - length

> 1, then you can use the <b>substring</b> function to

> do this easily...

Sorry, not fixed length, they are separated by semicolon and slash.

And the item count is not fixed too, maybe 3 or 6 or 9 items.

Former Member
0 Kudos

I checked some documents for UDF, most of them generate "only one" target element,

how to generate a new subelement in UDF? Is there any sample ?

btw, should I write a UDF between source's <remark> and target's <detail> ?

Thanks all of you, I ever wrote Java before, but I never write UDF, I'll try it.