Skip to Content
0
Jul 26, 2019 at 09:52 AM

Simple Transformation of xfa-like XML with optional Elements and two Tables in ABAP-Structure

485 Views

I try to transform the XML of Adobe Print File into an ABAP-Structure.

The target is to transfer the pdf-xml into labelpoint2 (printer language for thermo printer - Datamax, Microplex-Printer). My transformation class from ABAP-structure to Labelpoint2 works fine.

My problem is, that I have to deal with <field> and <draw> elements in mixed order.
Sometimes only one type of the tags is included in the XML-file. For example only <field>-Tags.

If the <field> and <draw> elements are sorted I don't have any problems to transfer them into my ABAP-Structure. But if they are not sorted my transformation didn't work. The unsorted order is unfortunately the norm.

Result for this shown XML-Example: The unsorted elements after 3 times draw and 2 times field are ignored.

How I have to change the simple transformation to make it run?

I don't have any idea.

Could someone help me to fix this problem, please?

Many thanks in advance.

In a simplistic form, the xml-file looks like the content of the variable lf_xml_file.

Here is the complete transformation report:

*&---------------------------------------------------------------------*
*& Report z_xml_test_st01
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT z_xml_test_st01. 
TYPES: BEGIN OF ts_draw, 
         text TYPE char10,
       END OF ts_draw. 
TYPES: tt_draw TYPE STANDARD TABLE OF ts_draw WITH KEY text. 
TYPES: BEGIN OF ts_field,
         text TYPE char10,
       END OF ts_field. 
TYPES: tt_field TYPE STANDARD TABLE OF ts_field WITH KEY text. 
TYPES: BEGIN OF ts_xml_data,
         t_draw  TYPE tt_draw,
         t_field TYPE tt_field,
       END OF ts_xml_data.
DATA:  lf_xml_file TYPE string. 
DATA: ls_xml_data     TYPE ts_xml_data,
      lt_xml_content  TYPE filetable,
      lf_anz_dat      TYPE i,
      lo_xml_document TYPE REF TO cl_xml_document,
      lf_filename     TYPE localfile,
      lf_subrc        TYPE sy-subrc,
      lf_xml_string   TYPE xstring,
      lf_size         TYPE sy-tabix. 
*cl_gui_frontend_services=>file_open_dialog(

*  EXPORTING*  window_title  = 'Select an XML file '
*  default_filename  = 'c:\users\XXXXXXXXX\Desktop\zwn_test_st01.xml'
*  initial_directory = 'C:\'
*  CHANGING
*      file_table = lt_xml_content
*      rc = lf_anz_dat*  EXCEPTIONS*  OTHERS = 5 ).
**     lf_filename = lt_xml_content[ 1 ].<br>
** lo_xml_document = NEW #(  ).*<br>
* lo_xml_document->import_from_file(
*     EXPORTING
*         filename = lf_filename
*     RECEIVING
*         retcode = lf_subrc ).
*
* lo_xml_document->render_2_xstring(
*     IMPORTING
*         retcode = lf_subrc
*         stream  = lf_xml_string
*         size  = lf_size  ).

* XML file to transfer
lf_xml_file = |<template>| &
|  <draw>| &
|      <value>| &
|          <text>1</text>| &
|      </value>| &
|  </draw>| &
|  <draw>| &
|      <value>| &
|          <text>2</text>| &
|      </value>| &
|  </draw>| &
|  <draw>| &
|      <value>| &
|          <text>3</text>| &
|      </value>| &
|  </draw>| &
|  <field>| &
|      <value>| &
|          <text>1</text>| &
|      </value>| &
|  </field>| &
|  <field>| &
|      <value>| &
|          <text>2</text>| &
|      </value>| &
|  </field>| &
*|  <draw>| &
*|      <value>| &
*|          <text>4</text>| &
*|      </value>| &
*|  </draw>| &
*|  <field>| &
*|      <value>| &
*|          <text>3</text>| &
*|      </value>| &
*|  </field>| &
*|  <draw>| &
*|      <value>| &
*|          <text>5</text>| &
*|      </value>| &
*|  </draw>| &
|</template>|.

TRY.    
    CALL TRANSFORMATION zwn_test_st01
*     SOURCE XML lf_xml_string       
      SOURCE XML lf_xml_file      
      RESULT root = ls_xml_data.  
    CATCH cx_st_error INTO DATA(l_error).
ENDTRY.
 
BREAK-POINT.

The simple transformation file looks like:

The real transformation, of course, looks a lot more complicated, with plenty of optional tags and optional attributes.

But to show my problem this simple version is good enough:

<?sap.transform simple?>
<tt:transform xmlns:tt="http://www.sap.com/transformation-templates"         
              xmlns:ddic="http://www.sap.com/abapxml/types/dictionary" 
              xmlns:def="http://www.sap.com/abapxml/types/defined">
 <tt:root name="ROOT" type="?"/>
  <tt:template>
  <template>
    <tt:group>
        <tt:cond frq="?">
            <tt:loop name="d" ref="ROOT.t_draw">
                <draw>
                    <value>
                        <text>
                            <tt:value ref="$d.text"/>
                        </text>
                    </value>
                </draw>
           </tt:loop>
           <tt:loop name="f" ref="ROOT.t_field">
               <field>
                   <value>
                       <text>
                           <tt:value ref="$f.text"/>
                       </text>
                   </value>
               </field>
          </tt:loop>
        </tt:cond>
   </tt:group>
 </template>
 </tt:template>
</tt:transform>