How to transform ABAP table into XML using XSLT?

In this step by step SAP tutorial, you will learn how to transform the ABAP table into XML using XSLT? If you don’t have a clue what XSLT is, I recommend you read the following article before starting the tutorial Introduction to XSLT in ABAP.

Step 1. Go transaction XSLT_TOOL (Transport Organizer). Put xslt_toolin the command field and hit Enter:

Step 2. Put new transformation name i.e. “ZSAP_XSLT_EXAMPLE01” in Transformation field:

Step 3. Click on Create button:

Step 4. Put meaningful description in Short Description field i.e. “ABAP to XML Example“:

Step 5. In popup window choose “XSLT Program” from Transformation Type list:

Step 6. Click continue button:

Step 7. Change tab by clicking on Source Code:

Step 8. Remove everything from the source area and put following XSLT code:

<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

  <xsl:output encoding="iso-8859-1" indent="yes" method="xml" version="1.0"/>
  <xsl:strip-space elements="*"/>

  <xsl:template match="/">
    <CATALOG>
      <xsl:apply-templates select="//IBOOK/item"/>
    </CATALOG>
  </xsl:template>

  <xsl:template match="IBOOK/item">
    <BOOK>
      <author>
        <xsl:value-of select="AUTHOR"/>
      </author>
      <title>
        <xsl:value-of select="TITLE"/>
      </title>
      <genere>
        <xsl:value-of select="GENRE"/>
      </genere>
      <price>
        <xsl:value-of select="PRICE"/>
      </price>
      <description>
        <xsl:value-of select="DESCRIPTION"/>
      </description>
    </BOOK>
  </xsl:template>
</xsl:transform>

Step 9. Active everything by clicking on Activate button:

Step 10. Now go to transaction SE80 (Object Navigator). Put /n se80in the command field and hit Enter:

Step 11. Make sure that object type is set to Program and if not choose program from object list:

Step 12. Put new program name in Program name field i.e. “ZSAP_XSLT_EXAMPLE01“:

Step 13. Click on Yes button in Create Program popup:

Step 14. Click on Yes button in Create Program popup:

Step 15. Click on Save button:

Step 16. Choose package where you want to store development object i.e. “ZSAP_PACKAGE_EXAMPLE“. If want to store object locally ignore this step and go streigh to Step 18:

Step 17. Click on Save button:

Step 18.  To store development artefact locally click on Local Object button:

Step 19. Double click on program name:

Step 20. Click on Display<-> Change button:

Step 21. Remove everything from code area and past there following ABAP program code:

REPORT zsap_xslt_example01.
CONSTANTS lv_file TYPE string VALUE 'C:\temp\book.xml'.

TYPES: BEGIN OF ty_book,
         author      TYPE c LENGTH 30,
         title       TYPE c LENGTH 50,
         genre       TYPE c LENGTH 10,
         price       TYPE dmbtr,
         description TYPE c LENGTH 512,
       END OF ty_book.

FIELD-SYMBOLS: <fs_book> TYPE ty_book.

DATA: lt_output TYPE STANDARD TABLE OF ty_book,
      lt_input  TYPE STANDARD TABLE OF char2048.

DATA: lt_result_xml TYPE STANDARD TABLE OF abap_trans_resbind,
      ls_result_xml TYPE abap_trans_resbind.

* Try-catch variables
DATA: ls_rif_ex   TYPE REF TO cx_root, ls_var_text TYPE string.
APPEND INITIAL LINE TO lt_output ASSIGNING <fs_book>.
IF sy-subrc EQ 0.
  <fs_book>-description = 'Some description'.
  <fs_book>-author = 'King, Stephen'.
  <fs_book>-genre = 'Thriller'.
  <fs_book>-price = 46.
  <fs_book>-title = 'Misery'.
ENDIF.

APPEND INITIAL LINE TO lt_output ASSIGNING <fs_book>.
IF sy-subrc EQ 0.
  <fs_book>-description = 'Some description'.
  <fs_book>-author = 'King, Stephen'.
  <fs_book>-genre = 'Thriller'.
  <fs_book>-price = 26.
  <fs_book>-title = 'Carrie'.
ENDIF.

GET REFERENCE OF lt_output INTO ls_result_xml-value.
ls_result_xml-name = 'IBOOK'.
APPEND ls_result_xml TO lt_result_xml.

* Perform the XSLT stylesheet
TRY.
    CALL TRANSFORMATION zsap_xslt_example01
    SOURCE (lt_result_xml)
    RESULT XML lt_input.

  CATCH cx_root INTO ls_rif_ex.
    ls_var_text = ls_rif_ex->get_text( ).
    MESSAGE ls_var_text TYPE 'E'.
ENDTRY.

CALL METHOD cl_gui_frontend_services=>gui_download
  EXPORTING
    codepage = '4102'
    filename = lv_file
  CHANGING
    data_tab = lt_input.

 

Step 22. Active everything by clicking on Activate button:

 

Step 23. Run your program by clicking on Direct Processing button:

 

Step 24. Popup will appear where you will be asked to grant write access. It is required to export the XML to a local disk. Click on Allow button:

 

Step 25. Go to My Computer | System (C:) | temp. You should see a new file there “book.xml“. Double click to open file:

 

Step 26. the file will open in programs that are associated with  XML extension on your local PC (i.e. Notepad++):

This simple example should help you understand how XSLT works. Below you will also find a list of other tutorials related to this topic.

 

Written by 

Leave a Reply

Your email address will not be published. Required fields are marked *