Skip to Content
author's profile photo Former Member
Former Member

XI Mapping Question

I have a simple XML structure as follows:

<A>

<row><item>1</item></row>

<row><item>1</item></row>

<row><item>2</item></row>

</A>

I need to consolidate all the <row> records into <group> records

whenever <item> changes, for example:

<A>

<group>

<row><item>1</item></row>

<row><item>1</item></row>

</group>

<group>

<row><item>2</item></row>

</group>

</A>

I've tried the SplitByValue(), collapseContents() removeContents() node

functions but cannot come up with a workable solution. Can anyone provide a suggestion?

Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

6 Answers

  • Best Answer
    author's profile photo Former Member
    Former Member
    Posted on Oct 12, 2005 at 11:11 PM

    Suresh,

    It sounds like you are on the right track but it still did not give desired results.

    <A>

    <row><item>1</item></row>

    <row><item>2</item></row>

    </A>

    produced:

    <A>

    <row><item>1</item></row>

    </A>

    It completely skipped the second row. Do you have any suggestion to fine tune this?

    Brian

    Add a comment
    10|10000 characters needed characters exceeded

    • Former Member Former Member

      Brian,

      Here you go:

      if(a.length > 0) {
        result.addValue("newgroup");
      }
      for(int i=0;i<a.length; i++){
        if(a<i>.equals(ResultList.CC)){
           result.addValue("newgroup");
        }
      }

      Make sure you check the 'Cache Entire Queue' checkbox in the user-defined function. (the function is an advanced user-defined function and has 1 argument)

      Also while mapping item -> group and item -> row, the context of item is 'A' and not 'row'.

      Regards,

      Suresh.

  • Posted on Oct 11, 2005 at 03:53 AM

    Hi Brian,

    Try this

    <A> ---> <A>

    <row> ---> <group>

    <row> ---> splitbyvalue -


    > <row>

    <item> ---> <item>

    Hope this will be helpful.

    Regards

    Suraj

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Oct 11, 2005 at 08:56 AM

    Hi Brian

    In case you want to do the same using XSLT, you can check my code sample, which speaks of how to handle ur requirement.

    https://www.sdn.sap.com/irj/servlet/prt/portal/prtroot/com.sap.km.cm.docs/library/xi/xi-code-samples/generic xslt mapping in sap xi, part i.pdf

    cheers

    Sameer

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Oct 11, 2005 at 09:12 AM

    Hi Brian,

    I'd follow Suraj suggestions, with slide modifications:

    <A> ---> <A>

    <row> -> splitbyvalue(<b>value changed</b>) -> <b>collapseContexts</b> -> <group>

    <row> ---> splitbyvalue(<b>value changed</b>) -


    > <row>

    <item> ---> <item>

    Best regards

    Joachim

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Oct 11, 2005 at 01:09 PM

    J.R. & Joachim,

    Thanks for the input. Both soutions using SplitByValue produced similar results, but the grouping was not performed. I received the following output:

    <A>

    <group><row><item>1</item></row>

    <group><row><item>1</item></row>

    <group><row><item>2</item></row>

    </A>

    This was the same approach I tried yesterday but could never achieve the desired results. I was close when I tried:

    <A> --> <A>

    <item (ctx = A)> ->SplitByValue(Value Changed)-> <group>

    <item (ctx = A)> ->SplitByValue(Value Changed)-> <row>

    <item> -> <item>

    But this failed when I added a 4th input row.

    Add a comment
    10|10000 characters needed characters exceeded

    • Former Member

      Brian,

      Your approach does not produce desired results because

      when you do a splitbyvalue on item and map it to group,

      the number of groups will be equal to the number of

      items with '1' (the first value) and not the number of

      context changes.

      In order to do what you want, one of the options

      is to create an advanced user-defined function 'contextChangeList' with 1 argument

      (enter argument count as 1 while creating the function) like:

      if(a.length > 0) {
        result.addValue("newgroup");
      }
      for(int i=0;i<a.length; i++){
        if(a.equals(ResultList.CC)){
           result.addValue("newgroup");
        }
      }

      As a result, your mapping will look like

      <A> --> <A>

      <item (ctx = A)> ->SplitByValue(Value Changed)-> contextChangeList -> <group>

      <item (ctx = A)> ->SplitByValue(Value Changed)-> <row>

      <item> -> <item>

      Let me know if that works.

      Regards,

      Suresh.

  • author's profile photo Former Member
    Former Member
    Posted on Oct 13, 2005 at 08:38 AM

    Try the following

    1. Map the item to Group and change the context of item to A (root node)Write an Advanced function for creating the required no of Groups

    //write your code here

    result.addValue("0");

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

    {

    if(!a<i>.equals(a[i+1])){

    result.addValue(a<i>);

    }

    }

    2. Map the item to row and change the context of item to A (root node,)Write another function for creating required no of rows in each group

    //write your code here

    result.addValue(a[0]);

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

    {

    if (!a<i>.equals(a[i+1]))

    result.addContextChange();

    result.addValue(a[i+1]);

    }

    3. Map Item to Item, leave the context as it is.

    This should solve your problem.

    Thanks

    Abinash

    Add a comment
    10|10000 characters needed characters exceeded

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.