Вы находитесь на странице: 1из 6

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.

1 2 3 4 5 6

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

1 2 3
?

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

2
Note how <route> tag is defined

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26

<?sap.transform simple?> <tt:transform xmlns:tt="http://www.sap.com/transformation-templates" xmlns:ddic="htt 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>

27 28 29 30 31 32 33

</tt:template> </tt:transform>

And here is the sample code ...

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39

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

40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89

'&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 = http_invalid_state = http_processing_failed = OTHERS =

1 2 3 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' .

90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112

WRITE : /1 ls_route-id , 10 ls_route-distance , 20 ls_route-routetxt . ENDIF. ENDLOOP. WRITE : / 'Total Distance : ' , l_totdistance , / 'Time : ' , l_time .

Вам также может понравиться