Skip to Content
Technical Articles

XSLT mapping for Batch requests in SAP Cloud Platform Integration ( CPI )

Any integration’s most important part is always the mappings and most of the data we find in integration industry will be XML. Day to day we deal with XML and manipulation of XML. And what better tool to do that other than XSLT.

In the blog post we look at XSLT mapping to create custom batch request for Odata services.

We can create XSLT mapping or groovy scripts to create custom batch request when we are updating multiple entity in single batch request as CPI will not generate the XSD for multiple operation on multiple entities of the same OData API.

Things we can accessing the below XSLT code:

  1. Access the exchange property of message.
  2. Creating  local variables to be used for creating the batch request.
  3. XSLT for each to for looping.

Note:

We can even access headers and set properties, headers from XSLT.

 

Sample XSL to Generate the XML for Batch processing

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
	<xsl:template match="/">
		<!--The Parameter values will be fetched from exchange properties of the 
			Message -->
		<xsl:param name="para1" />
		<xsl:param name="para2" />
		<!--Starting of the batch -->
		<batchParts>
			<xsl:for-each select="root/row/sub-row">
				<!--Creating variable which will be used in many batch parts -->
				<xsl:variable name="var1">
					<xsl:value-of select="@attribute" />
				</xsl:variable>
				<xsl:for-each select="Subelemet/S-child">
					<batchChangeSet>
						<batchChangeSetPart>
							<method>POST</method>
							<!--entity set name and entity set -->
							<entitysetName>
								<entityset>
									<!--Map all source fields to target fields -->
									<tag1>
										<xsl:value-of select="$var1" />
									</tag1>
									<tag2>
										<xsl:value-of select="$para2" />
									</tag2>
									<tag3>
										<xsl:value-of select="@attribute1" />
									</tag3>
								</entityset>
							</entitysetName>
						</batchChangeSetPart>
					</batchChangeSet>
				</xsl:for-each>
				<!--You can add new entity set here of the same ODataAPI -->
			</xsl:for-each>
		</batchParts>
	</xsl:template>
</xsl:stylesheet>

Conclusion

When we have a need for customizing the batch request and need to perform operations on multiple entities. We can use the above generic XSLT mapping skeleton to create needed XSLT code.

This code can be starting point for creating the XSLT mapping for batch requests.

Please refer to link to read more about XSLT 3.0.

 

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