cancel
Showing results for 
Search instead for 
Did you mean: 

Groovy or XSLT to remove XML Tag(not values) in sap cpi payload

nidhi_srivastava22
Active Contributor
0 Kudos
832

Hi Experts,

I have a XML payload from which I need to remove only the XML tag and the values should pass to the parent tag. Sample payload mentioned below -

nidhi_srivastava22_0-1727005601585.png

I tried few groovy functions mentioned on the community but that is replacing the entire <AdditionalComments/> tag to blank which is not expected.

Please help me if we can take this with groovy or I need to go with XSLT?

Thanks.

Best Regards,

Nidhi Srivastava

View Entire Topic
nageshrao
Product and Topic Expert
Product and Topic Expert

Hi Nidhi, 

Below XSLT will achieve the result you want

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
    
    <!-- Identity transform using xsl:mode -->
    <xsl:mode on-no-match="shallow-copy"/>
    
    <!-- Special handling for AdditionalComments -->
    <xsl:template match="AdditionalComments">
        <xsl:copy>
            <xsl:value-of select="div"/>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

 

nidhi_srivastava22
Active Contributor
0 Kudos

Hi Nagesh,

Thanks for your help. This code works fine, but it fails when there is assigned to field sent.

<AssignedTo>ABC XYZ <ABC.XYZ@gmail.com></AssignedTo>

 

 
Error Details
net.sf.saxon.trans.XPathException: Error reported by XML parser: Unexpected character '@' (code 64) expected space, or '>' or "/>" at [row,col {unknown-source}]: [6,49}
 
Can we format the XSLT like to check only in AdditionalComments field if that is present or ignore the AssignedTo field for the check?
 
Thanks,
Nidhi Srivastava
nageshrao
Product and Topic Expert
Product and Topic Expert
0 Kudos

Hello Nidhi,
The error occurs because the email address in the AssignedTo field contains the '@' character, which is not valid in XML content outside of attributes.
Assuming you will get this '@' only in the field - "AssignedTo" we can update the XSLT to ignore that as below 

 

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
    
    <!-- Identity transform using xsl:mode -->
    <xsl:mode on-no-match="shallow-copy"/>
    
    <!-- Special handling for AdditionalComments -->
    <xsl:template match="AdditionalComments">
        <xsl:copy>
            <xsl:value-of select="div"/>
        </xsl:copy>
    </xsl:template>

    <!-- Explicitly copy AssignedTo without changes -->
    <xsl:template match="AssignedTo">
        <xsl:copy-of select="."/>
    </xsl:template>

</xsl:stylesheet>

 




nidhi_srivastava22
Active Contributor
0 Kudos

Hi Nagesh,

I tried this but still it is not working. I also tried to keep the explicit copy in order but still it is showing the same error.

nidhi_srivastava22_0-1727173644246.png

Thanks for your advise.

Best Regards,

Nidhi Srivastava

nageshrao
Product and Topic Expert
Product and Topic Expert
0 Kudos
Hi Nidhi,Can you please paste the complete sample source XML ?
nidhi_srivastava22
Active Contributor
0 Kudos
Hi Nagesh,
 
PFB the source sample payload.
 
<?xml version='1.0' encoding='UTF-8'?>
<Root1>
    <Root2>
        <ADONumber>12345</ADONumber>
        <State></State>
        <AssignedTo>SRIVASTAVA Nidhi (External) <nidhi.srivastava.ext@XXXXX.eu> </AssignedTo>
            <Priority></Priority>
            <AdditionalComments></AdditionalComments>
        </Root2>
    </Root1>
 
Just discussed on the issue & was suggested with a workaround that instead of sending the full details I can only send the email id. 
<AssignedTo>nidhi.srivastava.ext@XXXXX.eu</AssignedTo> this can be added in the XSLT or can i add a groovy step before to remove the same.
 
Thanks a lot for your advice.
 
Regards,
Nidhi
Ryan-Crosby
Active Contributor
0 Kudos
@nidhi_srivastava22 That is not valid XML.