Technology Blogs by Members
Explore a vibrant mix of technical expertise, industry insights, and tech buzz in member blogs covering SAP products, technology, and events. Get in the mix!
cancel
Showing results for 
Search instead for 
Did you mean: 
former_member186851
Active Contributor

Dear Scn users,

This Document gives the approach of executing XSLT mapping using java class in PI 7.4.

Summary:

The approach of handling parameterized values for a particular field and normal XSLT mapping for other fields. So a combination a parameterized and normal XSLT mapping is handled in this scenario which will be normally applied in most of the business cases.To achieve this a Java class is written which will is used to call the parameters and this java class is called in XSLT mapping.Hence the combination of Java and XSLT mapping is used to acheive the purpose.

In my scenario, I have 4 fields in the input structure

1.FirstName.

2.LastName.

3.Date of joining

4. Serial.

And the fields in the output structure are as below.

1.Name - concatenation of FirstName + LastName.

2. Date of joining-For this field parameterized mapping is used i.e. Date is obtained as parameter from Integration directory.

Step1:

Create Data Types.

One data Type for Input.

One for Output

Step 2:

Create Message Types and Service Interfaces.Just use the data types created and create 2 message types and 2 service interfaces for Input & Output Structures.

Step 3.

Create Java project in NWDS.

1.Open NWDS->New Java project.

2.Then Create a package and class which is used for parameterized mapping for Date field and the export the same as .jar file.

3.Below is the Java code which I used to extract parameters.

package com.po;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Map;
import com.sap.aii.mapping.api.AbstractTransformation;
import com.sap.aii.mapping.api.DynamicConfiguration;
import com.sap.aii.mapping.api.DynamicConfigurationKey;
import com.sap.aii.mapping.api.InputParameters;
import com.sap.aii.mapping.api.StreamTransformationException;
import com.sap.aii.mapping.api.TransformationInput;
import com.sap.aii.mapping.api.TransformationOutput;
public class JavaCode extends  AbstractTransformation{

   public final static String namespace = "http://pi";

   public final static String Date = "Date";

   public  void transform(TransformationInput in, TransformationOutput output) throws StreamTransformationException
   {
    DynamicConfiguration dc = in.getDynamicConfiguration();

    InputParameters parameters = in.getInputParameters();

    // add the seperate values
    getTrace().addInfo("Date " + parameters.getString("Date"));
    DynamicConfigurationKey userKey = DynamicConfigurationKey.create(namespace, Date);
    dc.put(userKey, parameters.getString("Date"));

    //copy the original content
    try{
     InputStream instream = in.getInputPayload().getInputStream();
     OutputStream outstream = output.getOutputPayload().getOutputStream();
     byte[] buf = new byte[1024];
     int numRead=0;
     while((numRead=instream.read(buf)) != -1){
      outstream.write(buf, 0, numRead);

     }
     outstream.flush();
    }catch(IOException ioe){
     throw new StreamTransformationException(ioe.getMessage(),ioe);
    }
   }

   /**
    * Method called from the XSL to return the value of the attribute
    * Remeber to have <xsl:param name="inputparam"/> in the XSL and call with
    * xslsave:getValue($inputparam, 'USERNAME')
    * @Param map the inputparams
    * @Param key the key to lookup
    * @return
    */
   public static String getValue(Map map, String key){
    try{
     DynamicConfiguration dc = (DynamicConfiguration)map.get("DynamicConfiguration");
     DynamicConfigurationKey keyConfig = DynamicConfigurationKey.create(namespace, key);
     return dc.get(keyConfig);
    }catch(Exception e){
     return "Error fetching key "+ key + " because: " +e.getMessage();
    }


   }

}

Step 4:

Create an .XSL file to transform input->output structure as per the requirement.For date field it will a call of java class using Java code.Below is the XSLT code.


<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:ns0="http://nttdata.com/Raghu" xmlns:javamap="java:com.po.JavaCode" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:param name="inputparam"/>
<xsl:template match="/">
<XSLTTestLegacy>
<Name>
xsl:value-of select="concat(concat(ns0:XSLTTestECC/FirstName,' '), ns0:XSLTTestECC/LastName)" />
  </Name>
<Date>
<xsl:value-of select="javamap:getValue($inputparam, 'Date')"/>
</Date>
<Serial>
  <xsl:value-of select="ns0:XSLTTestECC/Serial" />
  </Serial>

</XSLTTestLegacy>
   </xsl:template>
</xsl:stylesheet>

Call the Java class in the XSLT for parameterized mapping.

Step 5:

After completing the code import the JAVA class(.jar file) and XSLT code(.rar file) as imported archive in ESR.

Step 6:

Create the operation mapping by using the service interfaces created and in the mapping program.Select java class at first and XSLT as second as shown below.

And dont forget to select the "use SAP XML Toolkit"option.Now define a parameter and bind it with the java class Date.

Step 7:

After doing all the steps you can test and it can be seen that the Date which is a parameter is passed successfully in to the XSLT class and displayed in the output Structure

And for creating ID objects it is a normal way as we do for interfaces and in the Interfaces Tab of Integrated configuration the value of parameter Date should be declared.

References:

http://wiki.scn.sap.com/wiki/display/Snippets/Java+code+for+Parameterized+XSLT+mappings+in+PI7.1?ori...

http://www.piarchitecture.com/2011/07/using-configure-xsl-with-the-build-in-parameters-in-the-sap-pi...

Labels in this area