<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>Question Re: CPI: How to map multiple fields multiple times to target node in Technology Q&amp;A</title>
    <link>https://community.sap.com/t5/technology-q-a/cpi-how-to-map-multiple-fields-multiple-times-to-target-node/qaa-p/13790597#M4841005</link>
    <description>HI Ryad,Can you try this code import com.sap.gateway.ip.core.customdev.util.Message; import groovy.xml.*; def Message processData(Message message) { // Get the XML body as a string def xmlBody = message.getBody(String.class); // Parse the XML using XmlParser to work with mutable XML structure def parser = new XmlParser() def xml = parser.parseText(xmlBody) // Create a new root element def newRoot = new Node(null, 'Root') new Node(newRoot, 'id', '123') // Initialize itemId for targetSubitem int targetItemId = 1 // Iterate over the original XML's items xml.items.each { item -&amp;gt; item.subItem.each { subItem -&amp;gt; // Create a new targetSubitem element def targetSubitem = new Node(newRoot, 'targetSubitem') // Add the generated itemId element new Node(targetSubitem, 'itemId', targetItemId.toString()) // Increment the itemId for the next targetSubitem targetItemId++ // Iterate over the references and add them to the targetSubitem item.references.each { ref -&amp;gt; def reference = new Node(targetSubitem, 'references') new Node(reference, 'code', ref.code.text()) new Node(reference, 'id', ref.id.text()) } } } // Convert the transformed XML back to a string def writer = new StringWriter() def xmlNodePrinter = new XmlNodePrinter(new PrintWriter(writer)) xmlNodePrinter.preserveWhitespace = true xmlNodePrinter.print(newRoot) def transformedXml = writer.toString() // Set the transformed XML as the new message body message.setBody(transformedXml) return message; }</description>
    <pubDate>Sun, 11 Aug 2024 15:21:03 GMT</pubDate>
    <dc:creator>sivaramaraju_danthuluri</dc:creator>
    <dc:date>2024-08-11T15:21:03Z</dc:date>
    <item>
      <title>CPI: How to map multiple fields multiple times to target node</title>
      <link>https://community.sap.com/t5/technology-q-a/cpi-how-to-map-multiple-fields-multiple-times-to-target-node/qaq-p/13790120</link>
      <description>&lt;P&gt;I have a requirement to map a list of fields, and each value multiple times to target node.&lt;/P&gt;&lt;P&gt;Its similar like use Many as many function - compare with the standard function use one as many.&lt;/P&gt;&lt;P&gt;My source xml is like below:&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;&amp;lt;Root&amp;gt;
    &amp;lt;id&amp;gt;123&amp;lt;/id&amp;gt;
    &amp;lt;items&amp;gt;
        &amp;lt;itemId&amp;gt;1&amp;lt;/itemId&amp;gt;
        &amp;lt;references&amp;gt;
            &amp;lt;code&amp;gt;c01&amp;lt;/code&amp;gt;
            &amp;lt;id&amp;gt;1&amp;lt;/id&amp;gt;
        &amp;lt;/references&amp;gt;
        &amp;lt;references&amp;gt;
            &amp;lt;code&amp;gt;c01&amp;lt;/code&amp;gt;
            &amp;lt;id&amp;gt;2&amp;lt;/id&amp;gt;
        &amp;lt;/references&amp;gt;
        &amp;lt;subItem&amp;gt;
            &amp;lt;subItemId&amp;gt;001
            &amp;lt;/subItemId&amp;gt;
        &amp;lt;/subItem&amp;gt;
        &amp;lt;subItem&amp;gt;
            &amp;lt;subItemId&amp;gt;002
            &amp;lt;/subItemId&amp;gt;
        &amp;lt;/subItem&amp;gt;
        &amp;lt;subItem&amp;gt;
            &amp;lt;subItemId&amp;gt;003
            &amp;lt;/subItemId&amp;gt;
        &amp;lt;/subItem&amp;gt;
    &amp;lt;/items&amp;gt;
    &amp;lt;items&amp;gt;
        &amp;lt;itemId&amp;gt;2&amp;lt;/itemId&amp;gt;
        &amp;lt;references&amp;gt;
            &amp;lt;code&amp;gt;c01&amp;lt;/code&amp;gt;
            &amp;lt;id&amp;gt;3&amp;lt;/id&amp;gt;
        &amp;lt;/references&amp;gt;
        &amp;lt;references&amp;gt;
            &amp;lt;code&amp;gt;c01&amp;lt;/code&amp;gt;
            &amp;lt;id&amp;gt;4&amp;lt;/id&amp;gt;
        &amp;lt;/references&amp;gt;
        &amp;lt;references&amp;gt;
            &amp;lt;code&amp;gt;c01&amp;lt;/code&amp;gt;
            &amp;lt;id&amp;gt;5&amp;lt;/id&amp;gt;
        &amp;lt;/references&amp;gt;
        &amp;lt;subItem&amp;gt;
            &amp;lt;subItemId&amp;gt;001
            &amp;lt;/subItemId&amp;gt;
        &amp;lt;/subItem&amp;gt;
        &amp;lt;subItem&amp;gt;
            &amp;lt;subItemId&amp;gt;002
            &amp;lt;/subItemId&amp;gt;
        &amp;lt;/subItem&amp;gt;
    &amp;lt;/items&amp;gt;
&amp;lt;/Root&amp;gt;&lt;/LI-CODE&gt;&lt;P&gt;and I want to map the each of the&amp;nbsp; 'reference' node multuple times, the times equals the size of the subitem in source node.&lt;/P&gt;&lt;P&gt;Below are my expected target structure:&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;&amp;lt;Root&amp;gt;
    &amp;lt;id&amp;gt;123&amp;lt;/id&amp;gt;
    &amp;lt;targetSubitem&amp;gt;
        &amp;lt;itemId&amp;gt;1&amp;lt;/itemId&amp;gt;
        &amp;lt;references&amp;gt;
            &amp;lt;code&amp;gt;c01&amp;lt;/code&amp;gt;
            &amp;lt;id&amp;gt;1&amp;lt;/id&amp;gt;
        &amp;lt;/references&amp;gt;
        &amp;lt;references&amp;gt;
            &amp;lt;code&amp;gt;c01&amp;lt;/code&amp;gt;
            &amp;lt;id&amp;gt;2&amp;lt;/id&amp;gt;
        &amp;lt;/references&amp;gt;
    &amp;lt;/targetSubitem&amp;gt;
    &amp;lt;targetSubitem&amp;gt;
        &amp;lt;itemId&amp;gt;2&amp;lt;/itemId&amp;gt;
        &amp;lt;references&amp;gt;
            &amp;lt;code&amp;gt;c01&amp;lt;/code&amp;gt;
            &amp;lt;id&amp;gt;1&amp;lt;/id&amp;gt;
        &amp;lt;/references&amp;gt;
        &amp;lt;references&amp;gt;
            &amp;lt;code&amp;gt;c01&amp;lt;/code&amp;gt;
            &amp;lt;id&amp;gt;2&amp;lt;/id&amp;gt;
        &amp;lt;/references&amp;gt;
    &amp;lt;/targetSubitem&amp;gt;
    &amp;lt;targetSubitem&amp;gt;
        &amp;lt;itemId&amp;gt;3&amp;lt;/itemId&amp;gt;
        &amp;lt;references&amp;gt;
            &amp;lt;code&amp;gt;c01&amp;lt;/code&amp;gt;
            &amp;lt;id&amp;gt;1&amp;lt;/id&amp;gt;
        &amp;lt;/references&amp;gt;
        &amp;lt;references&amp;gt;
            &amp;lt;code&amp;gt;c01&amp;lt;/code&amp;gt;
            &amp;lt;id&amp;gt;2&amp;lt;/id&amp;gt;
        &amp;lt;/references
    &amp;lt;/targetSubitem&amp;gt;
    &amp;lt;targetSubitem&amp;gt;
        &amp;lt;itemId&amp;gt;4&amp;lt;/itemId&amp;gt;
        &amp;lt;references&amp;gt;
            &amp;lt;code&amp;gt;c01&amp;lt;/code&amp;gt;
            &amp;lt;id&amp;gt;3&amp;lt;/id&amp;gt;
        &amp;lt;/references&amp;gt;
        &amp;lt;references&amp;gt;
            &amp;lt;code&amp;gt;c01&amp;lt;/code&amp;gt;
            &amp;lt;id&amp;gt;4&amp;lt;/id&amp;gt;
        &amp;lt;/references&amp;gt;
        &amp;lt;references&amp;gt;
            &amp;lt;code&amp;gt;c01&amp;lt;/code&amp;gt;
            &amp;lt;id&amp;gt;5&amp;lt;/id&amp;gt;
        &amp;lt;/references&amp;gt;
    &amp;lt;/targetSubitem&amp;gt;
    &amp;lt;targetSubitem&amp;gt;
        &amp;lt;itemId&amp;gt;5&amp;lt;/itemId&amp;gt;
        &amp;lt;references&amp;gt;
            &amp;lt;code&amp;gt;c01&amp;lt;/code&amp;gt;
            &amp;lt;id&amp;gt;3&amp;lt;/id&amp;gt;
        &amp;lt;/references&amp;gt;
        &amp;lt;references&amp;gt;
            &amp;lt;code&amp;gt;c01&amp;lt;/code&amp;gt;
            &amp;lt;id&amp;gt;4&amp;lt;/id&amp;gt;
        &amp;lt;/references&amp;gt;
        &amp;lt;references&amp;gt;
            &amp;lt;code&amp;gt;c01&amp;lt;/code&amp;gt;
            &amp;lt;id&amp;gt;5&amp;lt;/id&amp;gt;
        &amp;lt;/references&amp;gt;
    &amp;lt;/targetSubitem&amp;gt;
&amp;lt;/Root&amp;gt;&lt;/LI-CODE&gt;&lt;P&gt;Is there a way to do it with standard mapping funcion? Or must write&amp;nbsp; UDF?&lt;/P&gt;&lt;P&gt;Thanks a lot in advance&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 10 Aug 2024 10:44:14 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/cpi-how-to-map-multiple-fields-multiple-times-to-target-node/qaq-p/13790120</guid>
      <dc:creator>rayd</dc:creator>
      <dc:date>2024-08-10T10:44:14Z</dc:date>
    </item>
    <item>
      <title>Re: CPI: How to map multiple fields multiple times to target node</title>
      <link>https://community.sap.com/t5/technology-q-a/cpi-how-to-map-multiple-fields-multiple-times-to-target-node/qaa-p/13790597#M4841005</link>
      <description>HI Ryad,Can you try this code import com.sap.gateway.ip.core.customdev.util.Message; import groovy.xml.*; def Message processData(Message message) { // Get the XML body as a string def xmlBody = message.getBody(String.class); // Parse the XML using XmlParser to work with mutable XML structure def parser = new XmlParser() def xml = parser.parseText(xmlBody) // Create a new root element def newRoot = new Node(null, 'Root') new Node(newRoot, 'id', '123') // Initialize itemId for targetSubitem int targetItemId = 1 // Iterate over the original XML's items xml.items.each { item -&amp;gt; item.subItem.each { subItem -&amp;gt; // Create a new targetSubitem element def targetSubitem = new Node(newRoot, 'targetSubitem') // Add the generated itemId element new Node(targetSubitem, 'itemId', targetItemId.toString()) // Increment the itemId for the next targetSubitem targetItemId++ // Iterate over the references and add them to the targetSubitem item.references.each { ref -&amp;gt; def reference = new Node(targetSubitem, 'references') new Node(reference, 'code', ref.code.text()) new Node(reference, 'id', ref.id.text()) } } } // Convert the transformed XML back to a string def writer = new StringWriter() def xmlNodePrinter = new XmlNodePrinter(new PrintWriter(writer)) xmlNodePrinter.preserveWhitespace = true xmlNodePrinter.print(newRoot) def transformedXml = writer.toString() // Set the transformed XML as the new message body message.setBody(transformedXml) return message; }</description>
      <pubDate>Sun, 11 Aug 2024 15:21:03 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/cpi-how-to-map-multiple-fields-multiple-times-to-target-node/qaa-p/13790597#M4841005</guid>
      <dc:creator>sivaramaraju_danthuluri</dc:creator>
      <dc:date>2024-08-11T15:21:03Z</dc:date>
    </item>
    <item>
      <title>Re: CPI: How to map multiple fields multiple times to target node</title>
      <link>https://community.sap.com/t5/technology-q-a/cpi-how-to-map-multiple-fields-multiple-times-to-target-node/qaa-p/13790686#M4841024</link>
      <description>&lt;P&gt;Given the source/target structures I think this would be pretty easy using IA.&lt;/P&gt;</description>
      <pubDate>Sun, 11 Aug 2024 23:09:27 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/cpi-how-to-map-multiple-fields-multiple-times-to-target-node/qaa-p/13790686#M4841024</guid>
      <dc:creator>Ryan-Crosby</dc:creator>
      <dc:date>2024-08-11T23:09:27Z</dc:date>
    </item>
    <item>
      <title>Re: CPI: How to map multiple fields multiple times to target node</title>
      <link>https://community.sap.com/t5/technology-q-a/cpi-how-to-map-multiple-fields-multiple-times-to-target-node/qaa-p/13790710#M4841031</link>
      <description>Hi Ryan, Could you tell me what exactally does 'IA' mean, I am somehow new to CPI, Thanks!</description>
      <pubDate>Mon, 12 Aug 2024 01:06:42 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/cpi-how-to-map-multiple-fields-multiple-times-to-target-node/qaa-p/13790710#M4841031</guid>
      <dc:creator>rayd</dc:creator>
      <dc:date>2024-08-12T01:06:42Z</dc:date>
    </item>
    <item>
      <title>Re: CPI: How to map multiple fields multiple times to target node</title>
      <link>https://community.sap.com/t5/technology-q-a/cpi-how-to-map-multiple-fields-multiple-times-to-target-node/qaa-p/13790713#M4841033</link>
      <description>Integration Advisor</description>
      <pubDate>Mon, 12 Aug 2024 01:12:39 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/cpi-how-to-map-multiple-fields-multiple-times-to-target-node/qaa-p/13790713#M4841033</guid>
      <dc:creator>Ryan-Crosby</dc:creator>
      <dc:date>2024-08-12T01:12:39Z</dc:date>
    </item>
  </channel>
</rss>

