Skip to Content
Technical Articles

How does xslt consume instantial Java method

As a convenient way to do mapping in SAP PI/PO, XSLT is always the first choice way to make message structure conversion in SAP PI/PO. However with the limitation of the language, it sometimes needs to be enhanced by using Java functions.

SAP PI/PO developers must be very familar with the way, dynamic configuration has been get/set inside XSLT mapping by consuming a set of Java functions. Another frequently used case that Java function has been consumed is value mapping.

Most of the Java functions consumed are static functions. However sometimes the program might require consume an instantial Java function. For example, the mapping is to get both surface area and volume of a cube, when the side length has been given. From the semantics perspective, it is better to create a cube instance with side length, and get the surface area, volume respectively.

In the following text, I will introduce a way to consume an instential Java function inside XSLT 1.0.

Let’s take the cube class as an example

Package au.com.test
public class Cube{
  private int _sideLength;
  // Constructor Method
  public Cube(int sideLength){
    _sideLength = sideLength;
  }

  public String GetSurfaceArea(){
    int surfaceArea = _sideLength * _sideLength * 6;
    return String.valueOf(surfaceArea);
  }

  public String GetVolume(){
    int volume = _sideLength * _sideLength * _sideLength;
    return String.valueOf(volume);
  }
}

export the jar file and import it to an imported archive whose SWCV is the same as your xslt mapping.

In order to consume the instantial method, the xslt will be as below

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
                              xmlns:cube="au.com.test.Cube">
  <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
  <xsl:strip-space elements="*"/>

  <xsl:variable name="SIDE_LENGTH" select="3"/>
  <xsl:template match="/">
    <xsl:variable name="instance" select="cube:new($SIDE_LENGTH)"/> 
    <xsl:element name="CubeInfo">
      <xsl:element name="SurfaceArea">
        <xsl:value-of select=“cube:GetSurfaceArea($instance)"/>
      </xsl:element>
      <xsl:element name="Volume">
        <xsl:value-of select=“cube:GetVolume($instance)"/>
      </xsl:element>
    </xsl:element>
  </xsl:template>
</xsl:stylesheet>

 

By this means, the class Cube has been instantialised at first. Then two instantial methods have been called inside XSLT mapping.

 

btw, while writing the blog, I just have a notepad on hand and the code has not been checked by the compiler. There might be small syntax error in the code. Please check it first. The logic is for your reference. 🙂

 

With you find this way useful in the mapping development.

Be the first to leave a comment
You must be Logged on to comment or reply to a post.