Three types of transformation to generate XML from ABAP

I would like to present three very different types of transformations to generate XML from ABAP.

1) the standard transformation ID, generic approach, see the following program:

REPORT zxml_pretty_print_1.

DATA:
  BEGIN OF ls_usr,
    bname TYPE usr02-bname,
    trdat TYPE usr02-trdat,
  END OF ls_usr,
  lt_usr    LIKE TABLE OF ls_usr,
  lv_string TYPE string,
  cx_error  TYPE REF TO cx_transformation_error.

SELECT * UP TO 3 ROWS
  FROM usr02
  INTO CORRESPONDING FIELDS OF TABLE lt_usr.

TRY.
    CALL TRANSFORMATION id
          SOURCE xxx = lt_usr[]
          RESULT XML lv_string.
  CATCH cx_transformation_error INTO cx_error.
    WRITE: / cx_error->if_message~get_text( ).
ENDTRY.

it generates such XML:

<?xml version="1.0" encoding="UTF-16"?>
<asx:abap version="1.0" xmlns:asx="http://www.sap.com/abapxml">
  <asx:values>
    <XXX>
      <item>
        <BNAME>USR11111</BNAME>
        <TRDAT>0000-00-00</TRDAT>
      </item>
      <item>
        <BNAME>USR22222</BNAME>
        <TRDAT>2019-11-05</TRDAT>
      </item>
      <item>
        <BNAME>USR33333</BNAME>
        <TRDAT>2020-03-17</TRDAT>
      </item>
    </XXX>
  </asx:values>
</asx:abap>

2) to use the transformation of type ‘XSLT-Program’ you have to create it through the transaction STRANS and call it with CALL TRANSACTION, here the transformation to generate the same XML:

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

<xsl:strip-space elements="*"/>

<xsl:template match="/">

<xxx>
<xsl:for-each select="//item">
  <item>
    <bname>
      <xsl:value-of select="BNAME"/>
    </bname>
    <trdat>
      <xsl:value-of select="TRDAT"/>
    </trdat>
  </item>
</xsl:for-each>
</xxx>

</xsl:template>

</xsl:transform>

3) and here the transformation of type ‘Simple Transformation’, also this transformation generates the same XML:

<?sap.transform simple?>
<tt:transform xmlns:tt="http://www.sap.com/transformation-templates">

<tt:root name="XXX"/>

<tt:template>

<xxx>
<tt:loop ref=".XXX">
  <item>
    <bname>
      <tt:value ref="BNAME"/>
    </bname>
    <trdat>
      <tt:value ref="TRDAT"/>
    </trdat>
  </item>
</tt:loop>
</xxx>

</tt:template>

</tt:transform>

See other related notes on my website:

Scroll to Top