Search code examples
concatenationbiztalkbiztalk-mapper

Concatenating values from the repeating nodes under repeating nodes in BizTalk Maps


I have something like this in an input XML

<Root>
<OrderText>
    <item>item1</item>
    <item>item2</item>
  </OrderText>
  <OrderText>
    <item>item3</item>
    <item>item4</item>
    <item>item5</item>
  </OrderText>
</Root>

From this input, the desired output is

<Root>
 <OrderItems>
  <Items>item1#item2</Items>  
 </OrderItems>
  <OrderItems>
  <Items>item3#item4#item5</Items>  
 </OrderItems>
</Root>

I am trying to find a solution here and followed a question asked by myself long back (link How to Concatenate multiple repetitive nodes into a single node - BizTalk) but with that approach I'm getting result like below

<Root>
 <OrderItems>
  <Items>item1#item2#item3#item4#item5</Items>  
 </OrderItems>
  <OrderItems>
  <Items>item1#item2#item3#item4#item5</Items>  
 </OrderItems>
</Root>

which is totally wrong. can somebody help me please.


Solution

  • Have a look at the documentation Cumulative Concatenate Functoid

    That gives you the first clue

    Parameter 2: An optional numeric value that indicates the scope to which the accumulation should be performed. The default value is zero (0), indicating that the accumulation scope is the entire input instance message.

    Try adding the second parameter and setting it to 1. This will result in the below output, which is closer to what you want.

    enter image description here

    <Root>
        <OrderItems>
            <Items>item1#item2#</Items>
            <Items>item3#item4#item5#</Items>
        </OrderItems>
    </Root>
    

    The second clue can be found by going to the Error List, showing Messages and clicking on the "Double-click here to show/hide compiler links". That will cause orange lines to appear on the map surface showing how the map thinks it should loop. See screenshot above that also shows that. Note how it is only looping on the root?

    So the second fix is to draw a line from OrderText to OrderItems, and when prompted select Direct Link, which is telling it you want it to loop there as well.

    enter image description here

    This will give you on output close to your desired output of

    <Root>
        <OrderItems>
            <Items>item1#item2#</Items>
        </OrderItems>
        <OrderItems>
            <Items>item3#item4#item5#</Items>
        </OrderItems>
    </Root>
    

    Removing the extra # at the end could be done either with a number of fuctoids such as string Size, String Left and a Subtraction functoid, or using a Scripting Fuctoid.