Creating transformations for structures and calling them in programs

In this recipe, we will see how we can create transformations that correspond to a nested structure defined within an ABAP program. We will create a transformation, then call the transformation from a program using the CALL TRANSFORMATION statement and pass it to a populated ABAP structure corresponding to the root element of the transformation. We will then see how serialization will generate the XML stream.

How to do it...

We will now see the steps needed to create a transformation corresponding to a structure:

  1. Call the transaction XSLT_TOOL. Then, enter a suitable name for your transformation. We gave it the name ZST6_FOR_STRUCTURE.
  2. Click on the Create button. Then, on the Create Transformation dialog box that appears, choose the option Simple Transformation and proceed further.
  3. Enter the following code within the transformation editor:
    How to do it...
  4. When done, click on the Activate (How to do it...) button.
  5. Next, we will create the program that will call the transformation and pass it to an ABAP structure.
  6. In the program, we will first define the nested structure, as shown in the following screenshot. The name of the structure is struc. It has a field field1 and contains another structure nest1, comprising of the fields field1 and field2.
    How to do it...
  7. Then, populate the structure fields with suitable values.
    How to do it...
  8. Finally, in the ABAP program we will define a string named xml_string and call the transformation using the CALL TRANSFORMATION statement. The result of the CALL TRANSFORMATION statement corresponding to the passed structure is returned in xml_string.
    How to do it...

How it works...

The transformation created is comprised of a structure that will be passed during serialization via the root variable struc. Within the XML literal elements X1, X3, and X4, the current node is set using the reference of the data root struc. For X1, we include the field field1 of struc. For X3, we address the field1 field of the nest1 structure contained within struc.

The ABAP program calls the transformation using the CALL TRANSFORMATION statement. Since it is serialization, we passed a populated ABAP structure struc as the SOURCE structure. The result is read into xml_string. The contents of the string xml_string may be viewed in the ABAP debugger, as shown in the following screenshot:

How it works...
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.116.14.245