Eng Swee posted a post about why you should be using Groovy for your mappings in SAP CP, and challenge me to write about XSLT. I agree Groovy has some really good options in regards to handling some mappings and parsing XML/JSON and create documents from it. XSLT has some really good sides also and if it is not a part of your tool pack then you will not make the ideal mappings.
XSLT can also be used on SAP PI/PO, I think there is support for XSLT 2.0 but you may need to install third-party processors for it. You can also use 1.0 but the allowed functions are not as many.
He writes about message mapping that is is pretty difficult to grasp and not a lot of people fully understanding it. I agree I have seen many really bad mappings. It was created in 2003-4 with SAP XI 3.0. You are able to quickly make some complex mappings, but at some point, you needed to have a high complexity if you needed to get data from multiple sources and compare it. I have just created some small messages mappings in CPI where it made sense. I did find it a little difficult to navigate the mapping editor and add the structure in it. I don’t think I’ll be able to do any IDOC-EDI mappings in it at the current state. I guess it may also be here the Integration Content Advisor comes into the picture.
I have found that XSLT has been pretty useful in a lot of the projects that I have been a part of. I do like it because:
- You can enter complex rules with XPath into the mapping to select the correct area.
- You can run it on any platform, if you decide to move from SAP CPI to another platfrom you can reuse your mappings, that is not an option with Message Mappings
- You can run it on any PC/Mac. I have been editing it in Eclipse which enables you to edit and run the mapping offline. Unless you want to use some special extensions like accessing headers. There are some debugging options but I have not got it to work, probably you need XMLSpy or a similar tool to process it.
- You have multiple different options to handle mappings on. I’ll explain some of them in this post.
- With XSLT 3.0 which is supported by SAP CPI you can call advanced functions to ie convert message to Json
- It is a lot easier to get developers that know XSLT than message mapping.
- XML can be used to make structural mappings of your content.
- It gives a good option to sort documents.
Run in Eclipse
It is pretty simple to run an XSLT in eclipse. Just create any project and place your files there. Once you create your XSLT with one of the templates there.
Once you have created the XSLT you can create an XML sample in any way you like.
Then you select the XSLT
Then select the XML sample that you want to run with.
You will then see the result of the conversion.
If you want to rerun the same query it can sometimes be a little tricky, but otherwise, you have the short cuts in the Run As.
Different examples of XSLT
I’m by no means an expert in XSLT, and you can find better and longer examples around the internet. My goal is here to show some of the XSLT that I have made on projects as a way to show how it can make your mappings better.
XML to Text
I have some requirements to map from a SOAP XML structure to a CSV for a SalesForce Project. Yes, I know that CSV was the best thing for integration. You can do a normal message mapping and then convert it to CSV. I did find it easier to just map directly to CSV. because then I could control headers.
The thing that made this most difficult was about finding which column matched together when there were more than 20 columns, but then a message mapping could also have been a problem. Next time I may define a script that creates the XSLT based on some XML content. The way we ended up solving it was like the following example. In output mode text you have to option to set escape characters like line breaks.
You can see the full source code here. SalesforceUsermap.xsl
Map with target rules
You can often just take an example XML document that you want to map to. Then for each element, you can fill in the values. It is pretty easy for simple structures and you have the option to add quite complicated rules.
It is also possible to loop over elements like done with <xsl:for-each select=”mm:Messages/mm:Message1/EmpJob”>. Then it will take all EmpJob elements and loop over them.
And you can see the full example here. SFtoCanonicalFormat.xsl
In XSLT you also have the option to use templates more advanced. In a template, you can place processing information about the current node. There is the Apply-templates select lower instances and copy those structures.
You can also set up rules that if something element is found with a template to do something for it.
You can see the example here. SortEmployee.xsl
You can also end up with more complicated structures like, where where I want to not process other than the first to messages of a multi mapping <xsl:template match=”mm:Message2|mm:Message3|mm:Message4|mm:Message5|mm:Message6|mm:Message7|mm:Message8|mm:Message9″></xsl:template>
It is also possible to create functions that you are commonly using. It could be date functions that you want to modify, in the same way, multiple times. On can then call the function from other locations in the XSLT and call them with parameters that can then be used. There is also an option to use import of XSLT so you can have a shared XSLT function so you can use the same curated functions in all your iFlows, without having to copy them in.
<xsl:function name="add-months" as="xs:date?"> <xsl:param name="date" as="xs:anyAtomicType?"/> <xsl:param name="months" as="xs:integer"/> <xsl:sequence select=" xs:date($date) + yearMonthDuration(0,$months) "/> </xsl:function>
I hope you got some initial understanding of what XSLT can do for your SAP CPI mappings and start using them where it makes sense. I will encourage you to spend some time in understanding what XSLT can do for your mappings and how it can speed up the development cycle.
You will need to learn how to use XPath to define your rules and it is pretty easy to learn. XPath contains a lot of function that you can use directly, and since CPI supports XSLT 3.0 it has most of the build in functions.
Cross-posted on CPIcourse.