cancel
Showing results for 
Search instead for 
Did you mean: 

Groovy Script: Add new node in existing XML

sabana1705
Explorer
0 Kudos
4,471

Hi Experts,
Can you please suggest how to write groovy script to add new element in the existing XML payload

my input would be

I need to get below like this

Any suggestion Pls.

Regards,

Jas

Accepted Solutions (0)

Answers (3)

Answers (3)

MortenWittrock
SAP Mentor
SAP Mentor

Hi Sabana

As ryan.crosby2 points out, this problem is a very good match for XSLT. This short stylesheet inserts the extra elements:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="3.0">
<xsl:mode on-no-match="shallow-copy"/>
<xsl:template match="GroupValue">
<xsl:copy>
<xsl:apply-templates/>
<CommercialParameters>
<ProductComponent>Dummy</ProductComponent>
</CommercialParameters>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>

Regards,

Morten

saikrishna7029
Explorer
0 Kudos

Hi Mortten,

which tool do you use to play around XSLT mapping and do you have any specific go to knowledge base for learning more about XSLT mapping.

Regards,

Sai Krishna V

LinkedIn

MortenWittrock
SAP Mentor
SAP Mentor
0 Kudos

Hi taciturn

Having some sort of XML IDE definitely helps. You can do without, but your life will be easier with an IDE. XSLT does have a fairly steep learning curve, so it might make sense to find a book or a course to get you started.

Regards,

Morten

PriyankaChak
SAP Champion
SAP Champion

Hi Sabana,

You can use appendNode method.

GroupValue.appendNode {
  ProductComponent('Dummy')
}
Varanganti
Explorer

My preferred approach is XSLT only but we can try for few cases

${bodyAs(String).replace('</GroupValue>','<Notification>Dummy</Notification></GroupValue>')}

Input

Output