Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Simple Transformation / initial_components not working as expected

alejandro_bindi
Active Contributor
0 Likes
3,248

I have created a simple transformation to transform from DDIC structures to XML. The structures mimic the XML hierarchy so the conversion is almost 1 to 1.

The problem I'm having is that although some fields have no value, an element is created for them, even when using the initial_components = 'SUPPRESS' option.

This is a simplified version of the real code that has the same behaviour: In this case, DDIC structure YSTESTS has 2 fields: CHARS type CHAR10, and STR type STRING.

ABAP:


REPORT  y_tests2.

DATA:
      gv_initcomp TYPE string,
      xstr    TYPE xstring,
      struct TYPE ystests.


PARAMETERS:
  p_blanks    TYPE c AS CHECKBOX DEFAULT 'X'.

START-OF-SELECTION.

  IF p_blanks = 'X'.
    gv_initcomp = 'INCLUDE'.
  ELSE.
    gv_initcomp = 'SUPPRESS'.
  ENDIF.

* ABAP > XML

  TRY.
      CALL TRANSFORMATION y_tests
        SOURCE data = struct
        RESULT XML xstr
        OPTIONS initial_components = gv_initcomp.
    CATCH cx_transformation_error.
      MESSAGE 'Error en transformación' TYPE 'A'.
  ENDTRY.

  CALL FUNCTION 'SCOL_TRACE_SHOW_XML'
    EXPORTING
      xdoc = xstr.

ST:


<?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="DATA" type="ddic:YSTESTS"></tt:root>

  <tt:template>
    <data tt:ref="DATA">
        <chars tt:value-ref="CHARS"></chars>
        <str tt:value-ref="STR"></str>
    </data>
  </tt:template>

</tt:transform>

The XML output is like this, even when deselecting the P_BLANKS checkbox:


<?xml version="1.0" encoding="utf-8" ?> 
<data>
  <chars></chars> 
  <str></str> 
</data>

The only solution I've found so far is to post-process the resulting XML using the iXML interfaces (DOM processing) to iterate over all the nodes and remove the empty elements, but I sure hope there is a simpler and cleaner way to achieve this.

Am I right?

Many thanks

1 ACCEPTED SOLUTION
Read only

JackGraus
Active Contributor
0 Likes
1,762

Hi Alejandro, try this ST:

<tt:cond s-check="not-initial(ref('CHARS'))">
  <chars tt:value-ref="CHARS"/>
</tt:cond>
<tt:cond s-check="not-initial(ref('STR'))">
  <str tt:value-ref="STR"/>
</tt:cond>

Regards Jack

I have created a simple transformation to transform from DDIC structures to XML. The structures mimic the XML hierarchy so the conversion is almost 1 to 1.

The problem I'm having is that although some fields have no value, an element is created for them, even when using the initial_components = 'SUPPRESS' option.

This is a simplified version of the real code that has the same behaviour: In this case, DDIC structure YSTESTS has 2 fields: CHARS type CHAR10, and STR type STRING.

ABAP:


REPORT  y_tests2.

DATA:
      gv_initcomp TYPE string,
      xstr    TYPE xstring,
      struct TYPE ystests.


PARAMETERS:
  p_blanks    TYPE c AS CHECKBOX DEFAULT 'X'.

START-OF-SELECTION.

  IF p_blanks = 'X'.
    gv_initcomp = 'INCLUDE'.
  ELSE.
    gv_initcomp = 'SUPPRESS'.
  ENDIF.

* ABAP > XML

  TRY.
      CALL TRANSFORMATION y_tests
        SOURCE data = struct
        RESULT XML xstr
        OPTIONS initial_components = gv_initcomp.
    CATCH cx_transformation_error.
      MESSAGE 'Error en transformación' TYPE 'A'.
  ENDTRY.

  CALL FUNCTION 'SCOL_TRACE_SHOW_XML'
    EXPORTING
      xdoc = xstr.

ST:


<?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="DATA" type="ddic:YSTESTS"></tt:root>

  <tt:template>
    <data tt:ref="DATA">
        <chars tt:value-ref="CHARS"></chars>
        <str tt:value-ref="STR"></str>
    </data>
  </tt:template>

</tt:transform>

The XML output is like this, even when deselecting the P_BLANKS checkbox:


<?xml version="1.0" encoding="utf-8" ?> 
<data>
  <chars></chars> 
  <str></str> 
</data>

The only solution I've found so far is to post-process the resulting XML using the iXML interfaces (DOM processing) to iterate over all the nodes and remove the empty elements, but I sure hope there is a simpler and cleaner way to achieve this.

Am I right?

Many thanks

5 REPLIES 5
Read only

Former Member
0 Likes
1,762

Hi Alejandro,

How did you solve this?

I have the same problem and I have no idea what is wrong.

Any help will be highly appreciated.

Thanks,

Read only

JackGraus
Active Contributor
0 Likes
1,763

Hi Alejandro, try this ST:

<tt:cond s-check="not-initial(ref('CHARS'))">
  <chars tt:value-ref="CHARS"/>
</tt:cond>
<tt:cond s-check="not-initial(ref('STR'))">
  <str tt:value-ref="STR"/>
</tt:cond>

Regards Jack

Read only

Former Member
0 Likes
1,762

Hi Jack,

Thank you very much for this, it is working, all the initial fields are not displayed. Now is it possible do this for tags with no reference? for instance

<RCPNT_CORP_NM>

                <tt:cond s-check="not-initial(ref('L1_NM'))">
                  <l1_nm>
                    <tt:value ref="$line.L1_CORP_NM"/>
                  </l1_nm>
                </tt:cond>

<RCPNT_CORP_NM>

My tag L1_NM is not displayed if initial, but the parent <RCPNT_CORP_NM> is displayed even when it doesnt have elements. I tried

<tt:cond s-check="not-initial('RCPNT_CORP_NM')">

but didn't work

Edited by: carlosrv on Sep 27, 2011 6:31 PM

Read only

JackGraus
Active Contributor
0 Likes
1,762

Hi, try this

<tt:cond s-check="not-initial(ref('L1_NM')) or ...">
  <RCPNT_CORP_NM> 
    <tt:cond s-check="not-initial(ref('L1_NM'))">
      <l1_nm>
        <tt:value ref="$line.L1_CORP_NM"/>
      </l1_nm>
      ...
    </tt:cond>
  <RCPNT_CORP_NM>
</tt:cond>

Regards Jack

Read only

0 Likes
1,762

Sorry, haven't received response notifications, and I did this long ago.

What I did was something similar, I used:


<tt:cond check="not-initial(CHARS)">
   <chars tt:value-ref="CHARS"/>
</tt:cond>

Regards