0% found this document useful (0 votes)
181 views

Processing XML With XSLT Transformation in ABAP

This document discusses processing XML data with XSLT transformations in ABAP. It explains that XML strings can be converted to internal tables and vice versa using the CALL TRANSFORMATION command along with a defined XSLT. An example is provided that calls a web service, processes the XML response using CALL TRANSFORMATION and an XSLT file to extract the data into ABAP variables and internal tables for further processing. The example XSLT structure and ABAP code are shown.

Uploaded by

viceawork01
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
181 views

Processing XML With XSLT Transformation in ABAP

This document discusses processing XML data with XSLT transformations in ABAP. It explains that XML strings can be converted to internal tables and vice versa using the CALL TRANSFORMATION command along with a defined XSLT. An example is provided that calls a web service, processes the XML response using CALL TRANSFORMATION and an XSLT file to extract the data into ABAP variables and internal tables for further processing. The example XSLT structure and ABAP code are shown.

Uploaded by

viceawork01
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

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.

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


<drivingdirections>
<route id="XX" distanceToTravel="XXXX" finalStep="XXXX">XXXX</route>
<totalDistance>XXX</totalDistance>
<totalTime>XXX</totalTime>
</drivingdirections>
To create XSLT file open transaction SE80, hit 'Edit Object' under 'More' tab enter name next to
'Transformation' and press 'New' button.

Enter description and choose 'Simple Transformation' press Ok.

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:root name="T_ROUTE" type="?"/>


<tt:root name="TOTDISTANCE" type="?"/>
<tt:root name="TIME" type="?"/>
The structure of XML data is defined between tags

<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:root name="T_ROUTE" type="?"/>


<tt:root name="TOTDISTANCE" type="?"/>
<tt:root name="TIME" type="?"/>

<tt:template>
<drivingdirections>

<tt:loop name="Route" ref="T_ROUTE">


<route>
<tt:attribute name="id" value-ref="$Route.ID"/>
<tt:attribute name="distanceToTravel" value-
ref="$Route.DISTANCE"/>
<tt:attribute name="finalStep" value-ref="$Route.FINAL"/>
<tt:value ref="$Route.ROUTETXT"/>
</route>
</tt:loop>

<totalDistance>
<tt:value ref="TOTDISTANCE"/>
</totalDistance>

<totalTime>
<tt:value ref="TIME"/>
</totalTime>

</drivingdirections>

</tt:template>

</tt:transform>

And here is the sample code ...

REPORT zpw_websrvice_direction.

*&--------------------------------------------------------------------
-*
*& Types and Data
*&--------------------------------------------------------------------
-*
DATA: http_client TYPE REF TO if_http_client ,
http_url TYPE string ,
p_content TYPE string .

DATA : v_unit TYPE string ,


v_motor TYPE string .
*&--------------------------------------------------------------------
-*
*& Selection Screen
*&--------------------------------------------------------------------
-*
SELECTION-SCREEN BEGIN OF BLOCK m1 WITH FRAME .
PARAMETERS : p_strt TYPE char7 ,
p_end TYPE char7 .
SELECTION-SCREEN SKIP 1 .
PARAMETERS : p_mile TYPE char01 RADIOBUTTON GROUP r1 ,
p_km TYPE char01 RADIOBUTTON GROUP r1 .
SELECTION-SCREEN SKIP 1 .
PARAMETERS : p_motor TYPE char01 AS CHECKBOX DEFAULT 'X' .
SELECTION-SCREEN END OF BLOCK m1.

*&--------------------------------------------------------------------
-*
*& Start of Selection
*&--------------------------------------------------------------------
-*
START-OF-SELECTION .

* Build the url string based on input


IF p_mile = 'X' .
v_unit = 'mile' .
ELSE.
v_unit = 'km' .
ENDIF.

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 .

* Creation of new IF_HTTP_Client object


CALL METHOD cl_http_client=>create_by_url
EXPORTING
url = http_url
IMPORTING
client = http_client
EXCEPTIONS
argument_not_found = 1
plugin_not_active = 2
internal_error = 3
OTHERS = 4.

http_client->request->set_header_field( name = '~request_method'


value = 'GET' ).
* Send the request
http_client->send( ).

* Reterive the result


CALL METHOD http_client->receive
EXCEPTIONS
http_communication_failure = 1
http_invalid_state = 2
http_processing_failed = 3
OTHERS = 4.

p_content = http_client->response->get_cdata( ).
REPLACE ALL OCCURRENCES OF '<' IN p_content WITH '<' .
REPLACE ALL OCCURRENCES OF '>' IN p_content WITH '>' .

TYPES : BEGIN OF ty_route ,


id TYPE string ,
distance TYPE string ,
final TYPE string ,
routetxt TYPE string ,
END OF ty_route .

DATA : l_route TYPE TABLE OF ty_route ,


l_totdistance TYPE string ,
l_time TYPE string .

TRY .

CALL TRANSFORMATION zpw_direct


SOURCE XML p_content
RESULT t_route = l_route
totdistance = l_totdistance
time = l_time .
CATCH cx_st_error.

ENDTRY.

DATA : ls_route TYPE ty_route .

LOOP AT l_route INTO ls_route .


IF ls_route-final <> 'true' .
WRITE : /1 ls_route-id ,
10 ls_route-distance ,
20 ls_route-routetxt .
ENDIF.
ENDLOOP.

WRITE : / 'Total Distance : ' , l_totdistance ,


/ 'Time : ' , l_time .

You might also like