Previous post useOneAsMany explained in easy example used an example to convert tree structure into flat structure by using useOneAsMany. This post will do the reverse, convert flat structure back to tree structure.
The source message type is flat structure. The target message type is tree structure. This mean Category, CatID, CatName need to be grouped based on same value. Book, BookID and BookName need to placed under corresponding Category.
formatByExample_MM
The objective of the mapping is to convert below test source data (flat data) and target data (tree data):
formatByExample_before_after

Firstly, map the first element in target message type, which is “Category” field. Below transformed queue with 6 values and 6 contexts, into queue with 3 values and single context.
formatByExample_queue_Category_explain
 
For field CatID and CatName, follow above similar way to map, and add additional SplitByValue [Each value].
formatByExample_queue_CatID_CatName

 
For field Book, use formatByExample to get correct context changes for each category. Example: values B1,B2,B3 under single context will group and place these 3 books under first category C1.
formatByExample_queue_Book_explain
For field BookID and BookName, use directly one-to-one mapping only. This is because source fields have a context change, and for each target fields under Book group also always have a context change.
formatByExample_queue_BookID      formatByExample_queue_BookName
Done. We completed the mapping!
The technique and tips in these 2 posts to convert tree structure to flat structure and vice-versa, should be useful to tackle actual mapping development that involved different hierarchy levels. Enjoy!

collapseContext and formatByExample to Convert Flat Structure to Tree Structure

16 thoughts on “collapseContext and formatByExample to Convert Flat Structure to Tree Structure

  • January 20, 2015 at 5:39 pm
    Permalink

    Your post helped me a lot in getting clear understanding of tricky node functions.
    Thanks.
    Regards,
    Ajit

    Reply
  • January 23, 2015 at 12:40 pm
    Permalink

    Thank you very much Clearly explained.Keep Posting

    Reply
  • January 31, 2015 at 5:54 pm
    Permalink

    Hi Ajit, Pavan,
    Thanks. Once we understand those tricky node functions, it could be very useful. 🙂
    Regards,
    Yee Loon

    Reply
    • June 13, 2015 at 1:31 am
      Permalink

      Exactly, once you get it right. you can achieve the complex mapping easily. Thank you for making it easy.

      Reply
  • October 21, 2015 at 7:14 pm
    Permalink

    Great post, info clear and concise. SAP training should be more like this.

    Reply
    • October 22, 2015 at 10:36 am
      Permalink

      Hi Younesse,
      Thanks. Glad to hear that 🙂
      Regards,
      Yee Loon

      Reply
  • July 11, 2017 at 3:43 pm
    Permalink

    Very nice explanation, it clarifies my doubts and provides clear picture.
    My suggestion is after explaining the functionality , it would be great to give some real time requirements as example.

    Reply
  • July 27, 2017 at 9:19 pm
    Permalink

    Excellent post sir..it is very clear

    Reply
  • October 18, 2017 at 3:29 pm
    Permalink

    Well explained.. Thank you.

    Reply
  • September 25, 2018 at 11:36 am
    Permalink

    If Row1 CatID = C1 , CatName = Parenting
    Row2 CatID = C1 , CatName = Parentingggggg
    Row3 CatID = C2 , CatName = Computers
    then CatName in Target will shift like
    Row1 CatID = C1 , CatName = Parenting
    Row2 CatID = C2 , CatName = Parentingggggg
    Any suggestion to solve this.
    and I know that source should collect it.
    but we know that worst case can be happen

    Reply
  • July 25, 2020 at 8:43 am
    Permalink

    hello, nice explanation.

    May I know how to achieve this but when the target structure has no records to filter but only the Recordset?

    Source: (CSV pipe delimited)
    MT_XML
    Recordset
    ShipNum
    OrderNum

    Target:
    XML.XML
    –> FILING 1..UNBOUNDED
    ->HEADER 1.1
    ShipNum 0.1
    OrderNum 0.1
    ->Details 1.1
    DetailSet 1.unbounded

    I don’t know which fields I can use to generate the structure, also I’m required to,
    I need to have one one Filing/Header segment per shipment with multiple tags nested underneath, shipment are not equal as the OrderNum and the values are not matching.

    any help is very much appreciated!.

    Reply
  • August 10, 2020 at 5:03 pm
    Permalink

    This is a great tutorial, and the screenshots really help.

    I’ve tried to use this to set up something similar where we take a flat file, and create CONF_ACT1001 IDocs. It works if the source data is sorted by the key I am using, but if it’s not in order, the data moves to the wrong IDocs. I’ve posted a query on answers.sap.com and that has more screenshots, if anyone knows what I’m missing, please let me know! Thanks

    Link: https://answers.sap.com/questions/13110260/sap-po-mapcollate-multiple-records-in-a-file-to-si.html

    Reply
  • June 28, 2021 at 11:20 pm
    Permalink

    Hello Yee Loon.
    Thanks for your post.
    I have a problem with convert tree structure if I use attributes in the target structure.
    For example,

    MT_BOOK_TREE
    Category Element
    CatID Attribute xsd:string
    CatName Attribute xsd:string
    Book Element
    BookID Attribute xsd:string
    BookName Attribute xsd:string

    Result:
    Book (element) value = BookID (attribute) value

    B1
    B2
    B3

    How can I fix it?

    Reply
  • June 28, 2021 at 11:23 pm
    Permalink

    Hello Yee Loon.
    Thanks for your post.
    I have a problem with convert tree structure if I use attributes in the target structure.
    For example,

    MT_BOOK_TREE
    Category Element
    CatID Attribute xsd:string
    CatName Attribute xsd:string
    Book Element
    BookID Attribute xsd:string
    BookName Attribute xsd:string

    Result:
    Book (element) value = BookID (attribute) value

    B1
    B2
    B3

    How can I fix it?

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *