Additional Blogs by Members
cancel
Showing results for 
Search instead for 
Did you mean: 
former_member181955
Contributor
0 Kudos

Description:

In my project I have a requirement where I need to call a instance method from ABAP class into  xslt mapping. It is very clear to call static methods (class methods) into xslt mapping.

But to invoke instance method we need an object, but that object should be created prior to calling the instance method.

In xslt we can’t create an object of ABAP Class. Because of this I created one class method, in that I created the object of this class and i used this boject to call instance method.So prior to calling the static method i called static method to get the object,by using this object i was able to call the instance method.

Example: 

1. Source File

<?xml version="1.0" encoding="UTF-8">

<Order>

   <MHDHB>3</MHDHB>

   <IPRKZ>2</IPRKZ>

</Order>

2.Target

<?xml version="1.0" encoding="utf-8"?>

<Message>

<FIELD>

  <ID>MHDHB</ID>

  <VALUE>3</VALUE>

</FIELD>

<FIELD>

  <ID>IPRKZ</ID>

  <VALUE>2</VALUE>

</FIELD>

<FIELD>

  <ID>PERIOD</ID>           

  <VALUE>0000000090</VALUE>

</FIELD>

</Message>

NOTE:The field PERIOD of the target consists of no of days, it should be calculated in instance method.This method requires two import parameters MHDHB,IPRKZ to calculate PERIOD.

3.Instance Method

method ZPERIOD. 

DATA v_mhdhb TYPE i. 

v_mhdhb = im_mhdhb. 

IF im_iprkz EQ 2.              " 2 = Month  

ex_mhdhb = v_mhdhb * 30 .

ELSEIF im_iprkz EQ 1.          " 1 = Week  

ex_mhdhb = v_mhdhb * 7.

ELSEIF im_iprkz EQ 3.          " 3 = Year  

ex_mhdhb = v_mhdhb * 365.

ELSE.                          "   = Day 

  ex_mhdhb = v_mhdhb.

ENDIF. 

CONDENSE ex_mhdhb.

endmethod.

4.Class Method( Static Method )

 

method SIMP.

CREATE OBJECT EX_OBJ.

endmethod.

5.Logic:

1.For calling the instance method object is required. I have one static function in that I created one object.

2.In xslt mapping I called this class method and get the object.

3.By using the above object I have called the instance method.

6.XSLT Mapping

<xsl:transform version="1.0"

xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

xmlns:sap="http://www.sap.com/sapxsl">

<xsl:strip-space elements="*"/>

<xsl:param name="IM_MHDHB" select="expression"/>

<xsl:param name="IM_IPRKZ" select="expression"/>

<xsl:variable name="EX_MHDHB" select="expression"/>

<xsl:variable name="EX_OBJ" select="expression"/>

<xsl:template match="/">

<sap:call-external class="ZCAL" method="SIMP">    "calling static method

  <sap:callvariable param="EX_OBJ" name="plant"/>  " Hold the objec

</sap:call-external>

<sap:call-external name="plant" method="ZPERIOD"> "calling instacne method

  <sap:callvalue param="IM_MHDHB"  select="string(Order/MHDHB)"/>

  <sap:callvalue param="IM_IPRKZ"  select="string(Order/IPRKZ)"/>

  <sap:callvariable param="EX_MHDHB" name="period"/>

</sap:call-external>

<Message>

<FIELD>

<xsl:element name="ID">

  <xsl:value-of select="'MHDHB'"/>

</xsl:element>

<xsl:element name="VALUE">

  <xsl:value-of select="Order/MHDHB"/>

</xsl:element>

</FIELD>

<FIELD>

<xsl:element name="ID">

  <xsl:value-of select="'IPRKZ'"/>

</xsl:element>

<xsl:element name="VALUE">

<xsl:value-of select="Order/IPRKZ"/>

</xsl:element>

</FIELD>

<FIELD>

<xsl:element name="ID">

  <xsl:value-of select="'PERIOD'"/>

</xsl:element>

<xsl:element name="VALUE">

  <xsl:value-of select="($period)"/>

</xsl:element>

</FIELD>

</Message>

</xsl:transform>