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