Processing XML With XSLT Transformation in ABAP
Processing XML With XSLT Transformation in ABAP
XML string in ABAP can be directly converted to internal table or vice-versa using command CALL
TRANSFORMATION. Before you use the command you need to define XSLT transformation. XSLT
transformation defines structure of XML data. CALL TRANSFORMATION then use this XSLT to process the xml
and convert it to ABAP variable. Below sample code which consumes a Web service and process result XML
string using CALL TRANSFORMATION to convert it into ABAP variables/internal table.
Before we continue with ABAP part of it worth looking at webservice. This webservice GetDirection provides
driving direction from one address to another based on parameter 'distanceUnit' and 'expresswayEnabled'
(avoid motorway).
Below is the screen shot of xml result, returned by webservice, which will be processed using CALL
TRANFORMATION. Click on this link to see the XML result.
In short data is organized in tags <drivingdirections> <route> <totalDistance> and <totalTime> . Tag
<route> repeats for each driving instruction so for obvious reasons data in <routes> tag will result in internal
table. XSLT file will have more or less same structure as below.
Below is screen shot thats shows how the final XSLT transformation looks like for above XML string. Below three
lines defines the reference ABAP variables where the data will be transfered
<tt:template>
</tt:template>
Note how <route> tag is defined
<?sap.transform simple?>
<tt:transform xmlns:tt="http://www.sap.com/transformation-templates"
xmlns:ddic="http://www.sap.com/abapxml/types/dictionary"
xmlns:def="http://www.sap.com/abapxml/types/defined">
<tt:template>
<drivingdirections>
<totalDistance>
<tt:value ref="TOTDISTANCE"/>
</totalDistance>
<totalTime>
<tt:value ref="TIME"/>
</totalTime>
</drivingdirections>
</tt:template>
</tt:transform>
REPORT zpw_websrvice_direction.
*&--------------------------------------------------------------------
-*
*& Types and Data
*&--------------------------------------------------------------------
-*
DATA: http_client TYPE REF TO if_http_client ,
http_url TYPE string ,
p_content TYPE string .
*&--------------------------------------------------------------------
-*
*& Start of Selection
*&--------------------------------------------------------------------
-*
START-OF-SELECTION .
IF p_motor = 'X' .
v_motor = 'true' .
ELSE.
v_motor = 'false' .
ENDIF.
CONCATENATE 'http://www.ecubicle.net/driving.asmx'
'/GetDirections?fromAddress=' p_strt
'&toAddress;=' p_end
'&distanceUnit;=' v_unit
'&expresswayEnabled;=' v_motor
INTO http_url .
p_content = http_client->response->get_cdata( ).
REPLACE ALL OCCURRENCES OF '<' IN p_content WITH '<' .
REPLACE ALL OCCURRENCES OF '>' IN p_content WITH '>' .
TRY .
ENDTRY.