Processing XML With XSLT Transformation in ABAP

Embed Size (px)

DESCRIPTION

XML

Citation preview

Processing xml with XSLT transformation in ABAPXML string in ABAP can be directly converted to internal table or vice-versa using commandCALL 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. Thiswebservice GetDirectionprovides 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 onthis linkto see the XML result.

In short data is organized in tags and . Tag repeats for each driving instruction so for obvious reasons data in tag will result in internal table. XSLT file will have more or less same structure as below.?123456

XXXXXXXXXX

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?123

The structure of XML data is defined between tags?12

Note howtag is defined

?123456789101112131415161718192021222324252627282930313233

And here is the sample code ...?123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112REPORT 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 inputIF 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_motorINTO http_url .* Creation of new IF_HTTP_Client objectCALL METHOD cl_http_client=>create_by_urlEXPORTINGurl = http_urlIMPORTINGclient = http_clientEXCEPTIONSargument_not_found = 1plugin_not_active = 2internal_error = 3OTHERS = 4.http_client->request->set_header_field( name = '~request_method'value = 'GET' ).* Send the requesthttp_client->send( ).* Reterive the resultCALL METHOD http_client->receiveEXCEPTIONShttp_communication_failure = 1http_invalid_state = 2http_processing_failed = 3OTHERS = 4.p_content = http_client->response->get_cdata( ).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_directSOURCE XML p_contentRESULT t_route = l_routetotdistance = l_totdistancetime = 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 .