Skip to Content
Technical Articles
Author's profile photo Sahar Massoud Far

SAP CPI – Parsing XML Tag from SAP Marketing Cloud API Response

Introduction

Welcome to the blog post of the Expert Services Marketing Practice.

We are happy to share our experience with you around Marketing Integration, Analytics, and Business Technology.

You want to see more blog posts from Expert Services? Click here

 

Overview

In this blog post, I will demonstrate how you can use SAP Cloud Platform Integration’s XSLT Mapping process to parse the XML properties tag that was received from SAP Marketing Cloud API response.

Then, I will use a Groovy script to read the value of a specific tag and pass that value throughout the CPI iFlow.

 

API response

Using the open-source Postman application, I run the Campaign API with the details shown in the table below, so you can see what the response data from SAP Marketing Cloud looks like.

Then in the Response section, you can see the tags below inside the <m:properties> tag as:

<d:TargetGroup> , <d:TargetGroupUUID> , and <d:CampaignUUID>.

What we want to achieve is to parse the tags and remove the “d:” or any other related characters or alphabets and only keep the actual tag names. Please see the next steps in this document.

 

Request

GET

https://{{InsertYourHostURL}}/sap/opu/odata/sap/API_MKT_CAMPAIGN_SRV/Campaigns(guid’fa163e88-95f5-1eea-83a9-755f929c5e8e’)/CampaignAssignedTargetGroups

x-csrf-token: Fetch

Authorization: Basic {{Enter your username and password}}

Response
<feed xmlns=”http://www.w3.org/2005/Atom” xmlns:m=”http://schemas.microsoft.com/ado/2007/08/dataservices/metadata” xmlns:d=”http://schemas.microsoft.com/ado/2007/08/dataservices” xml:base=”https://my302303-api.s4hana.ondemand.com/sap/opu/odata/sap/API_MKT_CAMPAIGN_SRV/”>
    <id>https://myxxxxx-api.s4hana.ondemand.com/sap/opu/odata/sap/API_MKT_CAMPAIGN_SRV/Campaigns(guid%27fa163e88-95f5-1eea-83a9-755f929c5e8c%27)/CampaignAssignedTargetGroups</id>
    <title type=”text”>AssignedTargetGroups</title>
    <updated>2019-12-05T20:02:45Z</updated>
    <author>
        <name/>
    </author>
    <link href=”Campaigns(guid%27fa163e88-95f5-1eea-83a9-755f929c5e8c%27)/CampaignAssignedTargetGroups” rel=”self” title=”AssignedTargetGroups”/>
    <entry>
        <id>https://myxxxx-api.s4hana.ondemand.com/sap/opu/odata/sap/API_MKT_CAMPAIGN_SRV/AssignedTargetGroups(TargetGroupUUID=guid’fa163e88-95f5-1eea-83a9-6fb993561e87′,CampaignUUID=guid’fa163e88-95f5-1eea-83a9-755f929c5e8c’)</id>
        <title type=”text”>AssignedTargetGroups(TargetGroupUUID=guid’fa163e88-95f5-1eea-83a9-6fb993561e87′,CampaignUUID=guid’fa163e88-95f5-1eea-83a9-755f929c5e8c’)</title>
        <updated>2019-12-05T20:02:45Z</updated>
        <category term=”API_MKT_CAMPAIGN_SRV.AssignedTargetGroupEntityType” scheme=”http://schemas.microsoft.com/ado/2007/08/dataservices/scheme”/>
        <link href=”AssignedTargetGroups(TargetGroupUUID=guid’fa163e88-95f5-1eea-83a9-6fb993561e87′,CampaignUUID=guid’fa163e88-95f5-1eea-83a9-755f929c5e8c’)” rel=”self” title=”AssignedTargetGroupEntityType”/>
        <content type=”application/xml”>
            <m:properties xmlns:m=”http://schemas.microsoft.com/ado/2007/08/dataservices/metadata” xmlns:d=”http://schemas.microsoft.com/ado/2007/08/dataservices”>
                <d:TargetGroup>134</d:TargetGroup>
                <d:TargetGroupUUID>fa163e88-95f5-1eea-83a9-6fb993561e87</d:TargetGroupUUID>
                <d:CampaignUUID>fa163e88-95f5-1eea-83a9-755f929c5e8c</d:CampaignUUID>
            </m:properties>
        </content>
    </entry>
</feed>

 

SAP CPI

In the SAP CPI iFlow, after the Request Reply call to the Campaign API, I will add the XSLT Mapping, and follow the next steps.

 

  1. Add the process called ‘XSLT Mapping’
  2. Click on the plus button to create a new mapping
  3. Paste the script below in the .xsl resource, then click on OK
1.	<?xml version="1.0"?>
2.	<xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
3.	    <xsl:output indent="yes" method="xml" encoding="utf-8" omit-xml-declaration="yes"/>
4.	    <!-- Stylesheet to remove all namespaces from a document -->
5.	    <!-- NOTE: this will lead to attribute name clash, if an element contains
6.	       two attributes with same local name but different namespace prefix -->
7.	    <!-- Nodes that cannot have a namespace are copied as such -->
8.	    <!-- template to copy elements -->
9.	    <xsl:template match="*">
10.	        <xsl:element name="{local-name()}">
11.	            <xsl:apply-templates select="@* | node()"/>
12.	        </xsl:element>
13.	    </xsl:template>
14.	    <!-- template to copy attributes -->
15.	    <xsl:template match="@*">
16.	        <xsl:attribute name="{local-name()}">
17.	            <xsl:value-of select="."/>
18.	        </xsl:attribute>
19.	    </xsl:template>
20.	    <!-- template to copy the rest of the nodes -->
21.	    <xsl:template match="comment() | text() | processing-instruction()">
22.	        <xsl:copy/>
23.	    </xsl:template>
24.	</xsl:stylesheet>

 

The screenshot below shows the above the code is properly pasted and we are ready to go to the next steps.

  1. Create a Groovy Script
  2. Past the script below in the Groovy File, then click OK.

The line “${parseXML.entry.content.properties.TargetGroup.text()}”; will grab the value inside of the specific XML tag (in this case TargetGroup).

Please note that depending on your API response the path to the specific tag might be different than what’s mentioned here.

For example, if you looked at the table in the ‘API response’ section of the document, in order to get to the TargetGroup tag, you would follow the path from <entry> <content> <properties> and finally the <TargetGroup> tag, and that’s where you need to insert the path in the “${parseXML.entry.content.properties.TargetGroup.text()}”

The line message.setProperty(“TargetGroup”, TargetGroup); assigns the value of the TargetGroup into your defined variable, in order to access it throughout the iFlow.

1.	import com.sap.gateway.ip.core.customdev.util.Message;
2.	import java.util.HashMap;
3.	import java.text.SimpleDateFormat;
4.	 
5.	def Message processData(Message message) {
6.	   
7.	    def map = message.getProperties();
8.	    def body = message.getBody(String.class);
9.	    def parseXML = new XmlParser().parseText(body);
10.	 
11.	 def TargetGroup = "${parseXML.entry.content.properties.TargetGroup.text()}";
12.	 def TargetGroupUUID = "${parseXML.entry.content.properties.TargetGroupUUID.text()}";
13.	 
14.	    message.setProperty("TargetGroup", TargetGroup);
15.	    message.setProperty("TargetGroupUUID", TargetGroupUUID);
16.	   
17.	    return message;
18.	 
19.	}

 

Result

Here’s the payload in SAP CPI which does not include any of the alphabets or characters withing the XML tags:

2.	<feed base="https://myxxxxx -api.s4hana.ondemand.com/sap/opu/odata/sap/API_MKT_CAMPAIGN_SRV/">
3.	<id>https://myxxxxx -api.s4hana.ondemand.com/sap/opu/odata/sap/API_MKT_CAMPAIGN_SRV/Campaigns(guid'fa163e88-95f5-1eea-83a9-755f929c5e8c')/CampaignAssignedTargetGroups</id>
4.	<title type="text">AssignedTargetGroups</title>
5.	<updated>2019-12-05T20:46:15Z</updated>
6.	<author>
7.	<name/>
8.	</author>
9.	<link href="Campaigns(guid'fa163e88-95f5-1eea-83a9-755f929c5e8c')/CampaignAssignedTargetGroups"
10.	rel="self"
11.	title="AssignedTargetGroups"/>
12.	<entry>
13.	<id>https://myxxxxx -api.s4hana.ondemand.com/sap/opu/odata/sap/API_MKT_CAMPAIGN_SRV/AssignedTargetGroups(TargetGroupUUID=guid'fa163e88-95f5-1eea-83a9-6fb993561e87',CampaignUUID=guid'fa163e88-95f5-1eea-83a9-755f929c5e8c')</id>
14.	<title type="text">AssignedTargetGroups(TargetGroupUUID=guid'fa163e88-95f5-1eea-83a9-6fb993561e87',CampaignUUID=guid'fa163e88-95f5-1eea-83a9-755f929c5e8c')</title>
15.	<updated>2019-12-05T20:46:15Z</updated>
16.	<category term="API_MKT_CAMPAIGN_SRV.AssignedTargetGroupEntityType"
17.	scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme"/>
18.	<link href="AssignedTargetGroups(TargetGroupUUID=guid'fa163e88-95f5-1eea-83a9-6fb993561e87',CampaignUUID=guid'fa163e88-95f5-1eea-83a9-755f929c5e8c')"
19.	rel="self"
20.	title="AssignedTargetGroupEntityType"/>
21.	<content type="application/xml">
22.	<properties>
23.	<TargetGroup>134</TargetGroup>
24.	<TargetGroupUUID>fa163e88-95f5-1eea-83a9-6fb993561e87</TargetGroupUUID>
25.	<CampaignUUID>fa163e88-95f5-1eea-83a9-755f929c5e8c</CampaignUUID>
26.	</properties>
27.	</content>
28.	</entry>
29.	</feed>



 

The screenshot below shows the SAP CPI, where you can set the log to ‘Trace’ and verify your payload.

 

Conclusion

In conclusion, in SAP Cloud Platform Integration (CPI) you can use XSLT Mapping process to beautify the API response from SAP Marketing Cloud and easily access the value of an XML tag, by writing a simple Groovy script.

This process allows you to access and pass the data easily within CPI in case you need to do any logic based on the data received from the API.

 

Assigned Tags

      Be the first to leave a comment
      You must be Logged on to comment or reply to a post.