Useful XSLT mapping functions in SAP XI/PI
Recently, I have been working with a client where the Client specifically wants to develop XSLT mapping for all the Interfaces. I am writing this document which might be helpful for those who are using XSLT mapping for any type of transformation in SAP XI/PI. This document explains some commonly used mapping functions which can be implemented through XSLT code.
SAP System Properties, Date and Time Function, Padding Template, Copy-of Function, Value Mapping, Dynamic Configuration, ERD, Sender/Receiver Service
1) SAP System Properties
If the requirement is to find out the SAP system properties in mapping, then by using Graphical Mapping a simple UDF can be created for fetching those details. If we are using XSLT, mapping needs to be enhanced with Java Code to invoke those properties.
Write the code, compile it; copy both the files(.java and .class) and paste it in folder according to the package created in the code.
Here, package is com.utils. So both the files (.java and .class) should go under folder Com/utils.
Below is the Java code
1. Class name
2. Calling getSystemProperty()
3. Calling getSAPSystem()
4. Calling getSAPSystemName()
1. Call the java class
First, include the .class file path in stylesheet, syntax is
xmlns: variable name =”file path”
For example in the snapshot xmlns:propertyUtil =“com.utils. PropertyUtil” here propertyUtil is used as
reference name and com.utils.PropertyUtil is file path.
2. Call the method required
For calling the method created in the Java code, syntax is
Reference name: method-name()
For example in the snapshot: propertyUtil:getSAPsystemName(); propertyUtil is reference name and currentdatetime() is Method name in the Java code.
3. Assigning the called method value in the required field
2) Date Time Function
Most common requirement is to get the System date and time. For getting the current date and time in XSLT 1.0, there is no existing standard function. This can be achieved with the help of a Java code which has to be imported into XSLT, for importing the java code into XSLT follow the below steps.
Simple java code to get the current System Date and Time.
– First, include the .class file path in stylesheet, syntax is
xmlns: variable name =”file path”
For example in the snapshot xmlns:com_xi_utils_DateTime=“com.xi.utils.DateTime” here com_xi_utils_DateTime is used
as reference name and com.xi.utils.DateTime is file path.
– Now for calling the method created in the Java code, syntax is
For example in the snapshot: com_xi_utils_DateTime:currentdatetime(); com_xi_utils_DateTime is reference name and
currentdatetime() is Method name in the Java code.
3) Padding Function Template
In some scenarios there might be a requirement of adding spaces or any special characters after or before the value. This is called as Padding. In XSLT, there is no standard function available for padding. This can be achieved by using XSLT template. Here is one template that can be called as many times as required.
This template is for Right Padding.
For Left padding, remove highlighted code and add this
<xsl:with-param name=“padding_Var” select=“concat($padding_Char,$padding_Var)”/>
For calling this template in XSLT code.
Explanation of Code:
In the code, padding template is used for the Job_Level attribute.
One local variable called Position is used to store the value of Designation which is coming from the Input.
<xsl:call-template name=”template name”> is the command to call template.
As, the template name is “padding” syntax will be <xsl:call-template name=“padding”>
padding_Char is used as a parameter which holds the padding character like ‘%’ in this case.
padding_Var is holding the value of local variable Position.
max_len is the maximum length up to which the specified variable has to be padded (including the length of the variable).
4) copy-of Function
If there is any business requirement in which client wants the complete input xml in one field at target side i.e., to return the input payload as an XML into a single field at the target then this can be achieved by either XSLT mapping or Java mapping in SAP XI. If it is SAP PI 7.1 or higher, this can be achieved in Graphical mapping by using ‘Return as Xml’ function. Below is the explanation how to get the Input payload as an XML using copy-of function.
The <xsl:copy-of> element creates a copy of the current node, i.e., if we give the Parent node then child nodes and attributes of the Parent node will be copied automatically.
For copying the complete Input Payload, give “*” in copy-of function and it will copy the complete Input Payload with “Xml Tags”.
To eliminate the Tags from the Payload and copy only the data from the Payload, use <xsl:value-of select=“.”/>
Below snapshot shows how to use it, for the same Input payload which I used in the copy-of function. Only the data inside the tags will come up.
5) Value Mapping
Value Mappings are used to do a dynamic value lookup in PI. The advantage of using Value mapping is, it can be maintained/modified in Production environment instead of using a transport. In this way it reduces the time for change and does not need transport approvals.
If Graphical Mapping is used then value mapping function is available in Graphical Editor itself.
So, there might be a requirement where it is mandatory to use XSLT mapping and need to call Value Mapping. In this case just copy the Value mapping template in the XSLT code.
Value Mapping Group is already maintained in ID, so that configuration part is not included here.
1. This namespace has to be included at the top for invoking the Value Mapping functionality.
2. Calling the Value Mapping template for providing the input and getting the lookup output.
<xsl:with-param name=“SenderParam” select=“‘3A”/>
SenderParam is the input parameter
3. Value Mapping template :
Providing the inputs like Sender Agency, Sender Schema, Input parameter, Receiver Agency, Receiver Schema. It should be in
the same sequence.
<xsl:value-of select=“vm:executeMapping( ‘Sender’, ‘Designation’, $SenderParam, ‘Receiver’, ‘Designation’)”/>
Standard function is :
It can also be directly used in particular field rather than creating template.
6) Dynamic Configuration
The most frequent business requirement is to take the file name or directory path from the payload. Dynamic Configuration is preferably used whenever there is a need to create the target directory and file name dynamically. We need to write a simple XSLT function and import it in Operation Mapping as 1st mapping. Rest of the configuration will remain same.
1. Three namespaces to be included at the top for invoking the Dynamic Configuration functionality.
2. Code for Filename
In first line Filename variable is defined where input has to be given, may be from source structure or hardcoded value.
Here the value is taken from source payload.
<xsl:variable name=“Filename” select=“d:MT_EmployeeData/Employee/FirstName”/>
3. Code for Directory
In first line Directory variable is defined where input has to be given, requirement may be to take a particular value from source
structure and create the directory path or any hardcoded value. Here the value is hardcoded.
<xsl:variable name=“Directory” select=“‘175105′”/>
It is not at all mandatory to use Step 2 and Step 3 code in sequence, Directory Code can be written before the Filename code or
Last line code will copy and pass the whole input xml to the next mapping step.
7) Enhanced Receiver Determination
Enhanced Receiver Determination is used to generate receiver name dynamically. This function is used when we are not sure about the receiver names or want to generate receiver lists at runtime. To achieve this functionality, a mapping needs to be done which can read the values from a Payload or from a table and this mapping will be called in Receiver Determination under ERD section.
It is very simple to use ERD in graphical mapping and so it is in XSLT.
Create DT, MT, SI for sender/Receiver and OM as required.
Here one more OM needs to be created for ERD functionality, where MT “Receivers” and SI “ReceiverDetermination“ provided by SAP inside the Software component SAP BASIS, namespace http://sap.com/xi/XI/System need to be used.
For creating XSLT, export the Source structure from Sender MT and export the structure from Receivers MT (shown above).
1. Source structure
2. Target(Receiver) structure
3. Simple Mapping code
In the target field “Receivers/Receiver/Service”, name of the Receiver Business system/Business component should be placed. Several conditions can be implemented to determine that or the Source field can be mapped.
Here condition is to get all the Employee details in the Receiver Business component BC_ERD_T1 if the source field DOB is blank, otherwise BC_ERD_T2.
In case, more than one receiver is required we have to add multiple Receivers in mapping.
8) Sender and Receiver Service
There might be some business requirement to find out the Sender/Receiver system name to route the message to proper destination. From 7.x onwards, inbuilt functions to call Sender and Receiver are available in graphical mapping. These functions are helpful to find out the sender and receiver service of messages used in mapping.
Both the functions are available in XSLT and we have to call these functions inside the code and use their values.
Now, the requirement is to send the correct BS name in SNDPRN. The correct value of Sender BS will be useful at ECC side to distinguish from which BS the IDoc is coming.
Below is the snapshot of XSLT code.
1. Call “SenderService” Parameter
2. Assigning the value of SenderService parameter into a local Variable(SS)
3. Call “ReceiverService” Parameter
4. Assigning the value of ReceiverService parameter into a local Variable(RS)
5. Mapping of control records(EDI_DC40) of IDoc
SNDPRN – Sender Partner Number – Sender Service Name Mapped
SNDPOR – Sender Port – Hardcoded to ‘SPORTNAME’
RCVPRN – Receiver Partner Number – Receiver Service Name Mapped
RCVPOR– Receiver Port – Hardcoded to ‘RPORTNAME’
Similarly, more runtime properties like MessageId, Interface name, etc. can also be determined in XSLT.
XSLT code for MessageId and SenderInterface
1. Call “MessageId” Parameter
2. Assigning the value of MessageID parameter into a local Variable(MID)
3. Call “Interface” Parameter
4. Assigning the value of Interface parameter into a local Variable(SenderInterface)