Skip to Content

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

   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()

    /wp-content/uploads/2013/09/1a_283950.jpg

XSLT code

/wp-content/uploads/2013/09/1b_283951.jpg

         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.

/wp-content/uploads/2013/09/2a_283959.jpg

Simple java code to get the current System Date and Time.

/wp-content/uploads/2013/09/1__283960.jpg

 

    –     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

  Reference name:Method-name()

  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.

/wp-content/uploads/2013/09/2_283967.jpg

   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.

/wp-content/uploads/2013/09/3_283968.jpg

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.

/wp-content/uploads/2013/09/4_283969.jpg

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”.

/wp-content/uploads/2013/09/5_283988.jpg

 

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.

/wp-content/uploads/2013/09/6_283989.jpg

 

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.

/wp-content/uploads/2013/09/7_284018.jpg

     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>

       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:template name=“ValueMapping”>

            <xsl:param name=“SenderParam”/>

<xsl:value-of select=“vm:executeMapping( ‘Sender’, ‘Designation’, $SenderParam, ‘Receiver’, ‘Designation’)”/>

          </xsl: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 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.

/wp-content/uploads/2013/09/8_284019.jpg

     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”

     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

        vice–versa.

          Last line code will copy and pass the whole input xml to the next mapping step.

         

          <xsl:copy-of select=“.”/>

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.

ESR configuration

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.

/wp-content/uploads/2013/09/9a_284045.jpg

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.

/wp-content/uploads/2013/09/10_284046.jpg

/wp-content/uploads/2013/09/9_284047.jpg

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

    

       /wp-content/uploads/2013/09/11_284066.jpg

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)

     /wp-content/uploads/2013/09/10a_284067.jpg

    

To report this post you need to login first.

13 Comments

You must be Logged on to comment or reply to a post.

  1. Roberto Cantero

    Hi,

         Why XSLT mappings and no graphical mapping? Is there any hide reason to do it that way? I guess should take longer do it that way, even if you have some XSLT experience. Anyway is something good to learn.

    cheers,

    Rober.

    (0) 
    1. Aman Gupta Post author

      Hello,

      Yes there was strict instructions from client to use only XSLT mapping and no graphical mapping. yes i do agree with your point to use graphical mapping however decision was purely on client’s requirement and instructions.

      Thanks,

      Aman

      (0) 
    2. Peter Wallner

      Hello,

      In my opinion you are more flexible with XSLT. If you need to sort data or relocate XML elements XSLT is the way to go. Also if you have a lot of “if-else” in a logic you can make that well-arranged with XSLT instead of multiple “if-else” constructs in a graphical mapping.

      It is worth knowing XSLT at least, it makes certain tasks easier.

      Best regards,

      Peter

      (0) 
      1. Aman Gupta Post author

        Hi Peter,

        I completely agree with you and appreciate your opinion.

        Moreover the context handling is damm easy in XSLT unlike graphical. But, if we are dealing with large data and complex transformation using XSLT then performance issue always be there.

        Thanks,

        Aman

        (0) 

Leave a Reply