Useful XSLT Mapping Functions in SAP XI/PI
Useful XSLT Mapping Functions in SAP XI/PI
Useful XSLT Mapping Functions in SAP XI/PI
Hello,
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
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()
XSLT code
For example in the snapshot xmlns:propertyUtil ="com.utils. PropertyUtil" here propertyUtil is used as
reference name and com.utils.PropertyUtil is file path.
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.
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.
Explanation of Code:
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.
xmlns:vm="com.sap.aii.mapping.value.api.XIVMService"
2. Calling the Value Mapping template for providing the input and getting the lookup output.
<xsl:call-template name="ValueMapping">
<xsl:with-param name="SenderParam" select="'3A"/>
</xsl:call-template>
Standard function is :
<xsl:value-of select="vm:executeMapping(<SenderAgency>,<Sender
Scheme>,InputParameter,<ReceiverAgency>,<ReceiverScheme)"/>
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
st
dynamically. We need to write a simple XSLT function and import it in Operation Mapping as 1 mapping. Rest
of the configuration will remain same.
1. Three namespaces to be included at the top for invoking the Dynamic Configuration functionality.
xmlns:map="java:java.util.Map"
xmlns:dyn="java:com.sap.aii.mapping.api.DynamicConfiguration"
xmlns:key="java:com.sap.aii.mapping.api.DynamicConfigurationKey"
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"/>
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
vice–versa.
Last line code will copy and pass the whole input xml to the next mapping step.
<xsl:copy-of select="."/>
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.
ESR configuration
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 case, more than one receiver is required we have to add multiple Receivers in mapping.
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.
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)