Blog > SAP CPI: Technical guide to build a single XSLT mapping for multiple input types

SAP CPI: Technical guide to build a single XSLT mapping for multiple input types

Michal_Rohnka_CPI_XSLT_Int4
Michał Rohnka SAP ABAP and Integration Consultant
icon__calendar 2020-04-21

In this article you will learn:

  • How to create a simple flow to test your XSLT mappings with Postman
  • What are the advantages of using XSLT mapping in CPI
  • How to not repeat almost the same mapping many times and instead be efficient

Introduction

One of the most annoying things in developing integration solutions is to find ourselves in a situation where we need to repeat a similar task many times. For example, you get several similar messages from different source systems and you need to transform them into one common destination structure. Sounds familiar, right?

Creating several almost identical mappings can be a real pain. Not to mention the maintenance overhead when, for instance, the target structure changes – you need to change all the mappings. Nooo!

 

XSLT – flexibility is a key

What seems like a perfect solution is a technology that doesn’t rely on your source message name or its structure but the actual field names or values. This is where XSLT comes into play and reveals its best features at work.  Look at the example below where we have two source systems sending our system the same information but in two different formats:


 

Code 1: XML Input from source system 1

 


 

Code 2: XML Input from source system 2


 

They are different but still, they carry the same information and share most of the structure. It would be nice if you can work with them in one mapping in one flow, rather than create several instances of almost identical flows. You can achieve this in CPI with XSLT mapping. 

 

Simple CPI flow to test your XSLT

To test this you need to build a simple flow:

Simple flow with HTTPS call and XSLT mapping

 

Notice that the above flow is a great tool to test any XSLT mapping using for example Postman. You just send HTTPS POST and receive the result of the XSLT transformation. 

You can upload XSLT transformation from your PC or you can create and edit it directly in the CPI XSLT editor.

Select or create XSLT mapping

 

Pic. 2 Select or create XSLT mapping


XSLT Editor

Pic. 3 XSLT Editor


One mapping to rule them all!

Below you will find a common mapping for the two XMLs from our example. Key to your success in working with multiple input types inside one mapping is the ability of XPATH to try a selection from multiple paths with one statement by using “|” operator. Check variable Lines below, it will contain a sequence of Line or Material depending on the source message. Thanks to that you can then loop over it with one for-each statement no matter what was the source of selection. The same situation when you are selecting a value for <NetPrice> it will be populated with a value from Price or Prc. Nice right? So in simple words, you can ask XPATH to select whatever it will find, from the list you provide. 


 

Code 3. XSLT mapping for two different inputs


It’s time for you to deploy your flow with the above XSLT mapping and put it at work. You can use Postman and send HTTPS POST requests with example XML files to your CPI flow. The result should look like the one below:


 

Code 4. XSLT transformation result in HTTPS response 

Why should you consider this approach?

There are several advantages to this approach:

  • You can easily perform common changes
  • You see differences between source messages processing in one place
  • You can introduce a new source system faster
  • You are working with XSLT on CPI, so you have full potential of XSLT 3.0 and XPATH 3.1 at your disposal

 

I strongly encourage you to explore XSLT as the most powerful tool available on CPI for mapping. Learning can be quite a challenge but it’s for sure worth trying, as I strongly believe it will help you solve most of the complex mapping challenges you will encounter on your projects. 

 

Read also:

1. CPI challenge from SAP Mentor – Eng Swee Yeoh

2. SAP CPI- Combine in Sequence vs Combine Aggregation Algorithm

 

Michał Rohnka SAP ABAP and Integration Consultant
SAP integration and ABAP consultant since 2010. Engaged in many successful international SAP projects. Currently acting as an integration architect and AIF expert. BRFplus evangelist and SAP Press author: Mapping with BRFplus Decision Tables and SAP AIF. Tabletop games enthusiast and climbing apprentice, loves to challenge himself not only at work.