cancel
Showing results for 
Search instead for 
Did you mean: 

Mapping problem.

Former Member
0 Kudos

Hi all.

Have the following situation.

The source message can look something like this.

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

<ns0:sourcerecord xmlns:ns0="http:">
   <s_record>
      <ponum>1</ponum>
      <polinenum>1</polinenum>
   </s_record>
   <s_record>
      <ponum>2</ponum>
      <polinenum>1</polinenum>
   </s_record>
   <s_record>
      <ponum>1</ponum>
      <polinenum>4</polinenum>
   </s_record>
   <s_record>
      <ponum>1</ponum>
      <polinenum>3</polinenum>
   </s_record>
   <s_record>
      <ponum>2</ponum>
      <polinenum>3</polinenum>
   </s_record>
   <s_record>
      <ponum>2</ponum>
      <polinenum>2</polinenum>
   </s_record>
</ns0:sourcerecord>

And i want the following result at the target.

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

<ns0:targetrecord xmlns:ns0="http:">
   <t_record>
      <ponum>1</ponum>
      <linerec>
         <polinenum>1</polinenum>
      <linerec>
      </linerec>
         <polinenum>3</polinenum>
      <linerec>
      </linerec>
         <polinenum>4</polinenum>
      </linerec>
   </t_record>
   <t_record>
      <ponum>2</ponum>
      <linerec>
         <polinenum>1</polinenum>
      <linerec>
      </linerec>
         <polinenum>2</polinenum>
      <linerec>
      </linerec>
         <polinenum>3</polinenum>
      </linerec>
   </t_record>
</ns0:targetrecord>

As you see the fields coming in a node with the same ponum should be collected in ansubnode to for the ponum and sorted. The sort are no problem but the big poblem I have are to get all the rigth fields in the same node.

I hope somebody can help me.

BR

Andreas

Accepted Solutions (1)

Accepted Solutions (1)

justin_santhanam
Active Contributor
0 Kudos
Former Member
0 Kudos

That was exactly the solution I was looking for.

Thanks Raj.

Points on the way.

Will also award points for the xslt solution.

Answers (5)

Answers (5)

Former Member
0 Kudos

Hi Andreas,

here you are tring to group the records by ponum, so you nedd to use the below udf in your graphical mapping.

Give the name of the udf as "groupingByPonum" with the cache type queue and two agguments ponum,polinenum:

	public groupingByPonum(String[] ponum,String[] polinenum,ResultList result,Container container) {
		int i,j=0,k=0,i1,j1;
		Vector V[]=new Vector[ponum.length];
		String[] pos= new String[ponum.length];	String t=new String();		
		for(i=0;i<ponum.length;i++) {
			for(j=0;j<k;j++) {
				if(pos[j].equals(ponum<i>))	break;
			}
			if(j==k) {
				pos[k]=new String(ponum<i>);	k++;
				V[j]=new Vector();				
			}
			V[j].addElement(polinenum<i>);
		}
		Vector temp=new Vector();
		for(i=0;i<=j;i++) {
			for(i1=1;i1<V<i>.size();i1++) {
				for(j1=0;j1<i1;j1++) {
					if(Integer.parseInt(V<i>.elementAt(j1).toString())>Integer.parseInt(V<i>.elementAt(i1).toString())) {
						t=(String)V<i>.elementAt(i1);	V<i>.removeElementAt(i1);	V<i>.insertElementAt(V<i>.elementAt(j1), i1);	V<i>.removeElementAt(j1);	V<i>.insertElementAt(t, j1);
					}
				}
			}
		}
		for(i=1;i<=j;i++) {
			for(k=0;k<i;k++) {
				if(Integer.parseInt(pos[k])>Integer.parseInt(pos<i>)) {
					temp=V<i>;	V<i>=V[k];	V[k]=temp;
				}
			}
		}
		for(i=0;i<=j;i++) 
			for(k=0;k<V<i>.size();k++) 
				result.addValue(V<i>.elementAt(k));

	}	

Now do this graphical mapping:

ponum>removeContexts>sort>splitByValue\[value changed\]>collapseContexts-->t_record

ponum>removeContexts>sort>splitByValue\[value changed\]>collapseContexts>splitByValue\[each value\]>ponum

ponum>removeContexts>sort>splitByValue\[value changed\]>linerec

ponum>removeContexts>groupingByPonum>splitByValue\[each value\]>polinenum

polinenum>removeContexts

(in the udf give two inputs

1: ponum-->removeContexts

2: polinenum-->removeContexts)

that's all......

you can use this udf for any kind of grouping operation in message mapping.

--Sankar Choudhury

Former Member
0 Kudos

Hi,

if you want you can try this XSLT mapping:


<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"  version="1.0">
  <xsl:output method="xml"/>
  <xsl:key name="po" match="s_record" use="ponum"/>
  <xsl:template match="/">
    <ns0:targetrecord xmlns:ns0="http://test.it">
      <xsl:for-each select="//s_record[generate-id(.)=generate-id(key('po',ponum))]">
        <xsl:sort select="ponum" order="ascending"/>
        <t_records>
          <ponum>
            <xsl:value-of select="ponum"/>
          </ponum>
          <xsl:for-each select="key('po',ponum)">
            <xsl:sort select="polinenum"/>
            <linerec>
              <polineum>
                <xsl:value-of select="polinenum"/>
              </polineum>
            </linerec>
          </xsl:for-each>
        </t_records>
      </xsl:for-each>
    </ns0:targetrecord>
  </xsl:template>
</xsl:stylesheet>

Copy it in a file with ex. xslt and import it in mapping.

Be careful to put a valid namespace in the source xml.I used for istance xmlns:ns0="http://test.it", otherwise you cannot test it.

Regards,

Emiliano

Former Member
0 Kudos

Ok, with the UDF I mentioned above instead it should be mapped as:

ponum -> ponum

ponum, ponum, polinenum -> doMap -> linerec

(change the context of the second "ponum" and "polinenum" to "sourcerecord" - right-click->context->...)

ponum, ponum, polinenum -> doMap -> splitByValue -> polinenum

(change the context of the second "ponum" and "polinenum" to "sourcerecord" - right-click->context->...)

Regards,

Yaghya

Former Member
0 Kudos

How do you map t_record?

What occurences do you use for the nodes and fields?

//Andreas

Former Member
0 Kudos

ponum -> removeContexts -> t_record

Can you please clarify your question regarding occurences of fields and nodes? Do you still need to define the data types?

Edited by: Yaghya Nana on Jun 4, 2008 2:35 PM

Former Member
0 Kudos

No I have the data types but I think you are not using the same occurenses as I do.

Former Member
0 Kudos

The occurences are correct and are handled by context changes.

Former Member
0 Kudos

hi

check with standard function FORMAT BY EXAMPLE

check the below blog which illustrates the above function

/people/stefan.grube/blog/2005/12/29/new-functions-in-the-graphical-mapping-tool-xi-30-sp13

regards

chandrakanth

Former Member
0 Kudos

Create a User Defined function "doMap" with type "context" that takes three parameters a, b, and c.

String currPonum = a[0];
for (int i=0; i< b.length; i++){
if (b<i> == currPonum)
	result.addValue(c<i>);
}

Now map as follows:

ponum -> ponum

ponum, ponum, polinenum -> doMap -> polinenum

(change the context of the second "ponum" and "polinenum" to "sourcerecord" - right-click->context->...)

Regards,

Yaghya

Edited by: Yaghya Nana on Jun 4, 2008 2:04 PM

Former Member
0 Kudos

Did you read my question before I edited the reply i wanted in the system?

Just to be sure because about 2 min after I posted I changed the spec on what the solution needed to cover.

BR

Andreas