cancel
Showing results for 
Search instead for 
Did you mean: 

How to reorder contexts

0 Kudos

Hi all,

I have a complicated requirement. I need to reorder contexts/ values.

Example:

<parent>

<child>2</child>

</parent>

<parent>

<child>51</child>

</parent>

<parent>

<child>14</child>

</parent>

<parent>

<child>32</child>

</parent>

Output of this input will be:

2

51

14

32

I need it to be:

32

51

2

14

I cannot use sort or sortbykey since there is no ascending or descending order.

Thanks in advance.

Regards,

SAPEnthusiast

Accepted Solutions (1)

Accepted Solutions (1)

former_member191435
Contributor
0 Kudos

Hi,

Can you please provide occurences of the source and target structure.

Thanks,

Enivass

0 Kudos

I'm really sorry to those who replied. There were not enough details.

Ok here's a clear example:

<parent>

<child>X</child>

</parent>

<parent>

<child>A</child>

</parent>

<parent>

<child>B</child>

</parent>

<parent>

<child>Y</child>

</parent>

there's a rule that X=1, A=2, B=3, Y=4

In my current mapping, output is:

1,2,3,4

I need it to be

3,4,2,1

The order of the source is not fixed. It can be A,B,X,Y or X,B,A,Y.. etc.

The order of the target is fixed. B,Y,A,X (3,4,2,1).

former_member854360
Active Contributor
0 Kudos

Hi,

If the values are fixed then map with constant dont look at source.

If you are going to receive only fixed four values in different order A,B,X,Y

former_member854360
Active Contributor
0 Kudos

public void Rearrange(String[] input, ResultList result, Container container) throws StreamTransformationException

{

result.addValue("B");

result.addValue("Y"]);

result.addValue("A");

result.addValue("X");

}

0 Kudos

Ok I'll just follow your suggestion a while ago. Thanks for that

But I have a new problem now Sorry I noticed just now..

The other fields should be sorted according to the rule I stated before.

Example:

<parent>

<child>X</child>

<detail1>dog</detail1>

<detail2>car</detail2>

<detail3>wood</detail3>

</parent>

<parent>

<child>A</child>

<detail1>cat</detail1>

<detail2>bus</detail2>

<detail3>metal</detail3>

</parent>

<parent>

<child>B</child>

<detail1>frog</detail1>

<detail2>van</detail2>

<detail3>cotton</detail3>

</parent>

<parent>

<child>Y</child>

<detail1>rabbit</detail1>

<detail2>train</detail2>

<detail3>plastic</detail3>

</parent>

Aside from:

3

4

2

1

It should also be:

3 frog van cotton

4 rabbit train plastic

2 cat bus metal

1 dog car wood

<targetnode>

<targetfield>3</targetfield>

<targetfield2>frog</targetfield>

<targetfield3>van</targetfield>

<targetfield4>cotton</targetfield>

</targetnode>

<targetnode>

<targetfield>4</targetfield>

<targetfield2>rabbit</targetfield>

<targetfield3>train</targetfield>

<targetfield4>plastic</targetfield>

</targetnode>

<targetnode>

<targetfield>2</targetfield>

<targetfield2>cat</targetfield>

<targetfield3>bus</targetfield>

<targetfield4>metal</targetfield>

</targetnode>

<targetnode>

<targetfield>1</targetfield>

<targetfield2>dog</targetfield>

<targetfield3>car</targetfield>

<targetfield4>wood</targetfield>

</targetnode>

Right now, the output is

3 dog car wood

4 cat bus metal

2 frog van cotton

1 rabbit train plastic

former_member191435
Contributor
0 Kudos

Hi,

Can you please follow what ever the debashish said in his earlier post.

then use sortbykey node fuction with respect to child field.

Thanks,

Enivass

former_member854360
Active Contributor
0 Kudos

Hi,

Do this way ..........most SIMPLE ONE

child1---FixValueTable-------removecontext------->
                                                 -sortBykey------->SplitByValue------->targetfield2
DETAIL11-------removecontext----------------------->

FIXVALUE TABLE

B=1

Y=2

A=3

X=4

Do same for all the fields

Former Member
0 Kudos

UDF1: i have taken this code from Debashish's reply


public void Rearrange(ResultList result, Container container) throws StreamTransformationException
{

result.addValue("B");
result.addValue("Y"]);
result.addValue("A");
result.addValue("X");
}

UDF2: will have 3 inputs of type string

Execution type: all values of a context.


int a=var1.length;
int b= var2.length;
for(int i=0;i<a;i++)
{
for(int j=0;j<b;j++)
{
if(var1<i>.equals(var2[j]))
{
result.addValue(var3[j]);
break;
}
}
}

Mapping:

UDF1----


child---remove context


UDF2splitby value(each value)--targetfield2

detail1---remocecontext--

UDF1----


child---remove context


UDF2splitby value(each value)--targetfield3

detail2---remocecontext--

in the same way proceed for the rest of the fileds

former_member191435
Contributor
0 Kudos

Hi Debhashish,

It working fine.

Thanks,

Enivass

Answers (5)

Answers (5)

Former Member
0 Kudos

Hi

Use below UserDefined Function

public void Rearrange(String[] input, ResultList result, Container container) throws StreamTransformationException

{

result.addValue(input[3]);

result.addValue(input[4]);

result.addValue(input[1]);

result.addValue(input[2]);

}

Thanks

Mary Durai

Former Member
0 Kudos

Hi

Use below Userdefined Function.

public void Rearrange(String[] input, ResultList result, Container container) throws StreamTransformationException

{

result.addValue(input[3]);

result.addValue(input[1]);

result.addValue(input[0]);

result.addValue(input[2]);

}

Thanks

Mary Durai

Former Member
0 Kudos

if the sequence is fixed then u can go for CopyValue function

Former Member
0 Kudos

Hi,

You have not mentioned the logic based on which you are re ordering the contexts.

However, if its a fixed order, you can handle this in a UDF (queue) function. The input argument would child field.

In the UDF, you can search for the values in order and output them to the resultList.

Regards

former_member854360
Active Contributor
0 Kudos

Hi,

Use this UDF

public void Rearrange(String[] input, ResultList result, Container container) throws StreamTransformationException
{

result.addValue(input[3]);
result.addValue(input[1]);
result.addValue(input[0]);
result.addValue(input[2]);
}