2024 Sep 12 12:30 PM
Hi, experts.
How to allow an empty string with check condition in xml transformation?
U have this condition at mine xml transformation
<tt:cond s-check="not-initial(ref('ROOT.PROVIDER.BRANCHCODE'))">
<branchCode tt:value-ref="ROOT.PROVIDER.BRANCHCODE"/>
</tt:cond>
and call the transformation
call transformation (LV_TRANS_NAME)
source xml XML_FINAL2
result ROOT = IT_TAB.
If document has empty <branchCode></branchCode> - everything is ok, but some documents have
space inside ( <branchCode></branchCode> )and i'm getting dump.
How to allow an space and save check to not initial.
Type of BRANCHCODE - CHAR4.
2024 Sep 12 1:23 PM
s-check applies only to Serialization. You are currently Deserializing (SOURCE XML).
NB: don't confuse space (one space) and empty string (zero space).
2024 Sep 13 6:38 AM - edited 2024 Sep 13 6:39 AM
Ok, i would try to describe.
There are 4 options of how <branchCode> can attend in document
1) No <branchCode> in document
2) empty branchCode <branchCode></branchCode>
3) something inside branchCode <branchCode>value</branchCode>
4) space inside branchCode <branchCode> </branchCode>
When i to "Deserialize " xml with space inside, i'm getting a dump, when it's space inside <branchCode> (fourth option)
if i delete the 's-check' i will have a dump in case, where document hasn't <branchCode> inside (first option).
so how can i save mine s-check condition and allow to fill BRANCHCODE with space?
2024 Sep 13 9:12 AM
2024 Sep 13 1:39 PM - edited 2024 Sep 13 1:40 PM
What is important in your case is that you are using a deserialization, so in that case <tt:cond s-check="..."> behaves like <tt:cond> which means that the transformation accepts an XML where the tag <branchCode> is absent, otherwise the value is transferred to the ABAP component BRANCHCODE (whatever it's empty or one space or any other value). I guess that you have an issue because of the next tags.
I have a test program in which I tried the code below, and it works as expected:
METHOD test1.
DATA(xml) ='<Invoice> '
&& ' <branchCode> </branchCode>'
&& '</Invoice> '.
DATA(transfo_result) = VALUE ty_transfo_result( ).
CALL TRANSFORMATION (transfo_name)
SOURCE XML xml
RESULT root = transfo_result.
cl_abap_unit_assert=>assert_equals( act = transfo_result
exp = VALUE ty_transfo_result( branchcode = '' ) ).
ENDMETHOD.
METHOD test2.
DATA(xml) ='<Invoice> '
&& ' <branchCode></branchCode>'
&& '</Invoice> '.
DATA(transfo_result) = VALUE ty_transfo_result( ).
CALL TRANSFORMATION (transfo_name)
SOURCE XML xml
RESULT root = transfo_result.
cl_abap_unit_assert=>assert_equals( act = transfo_result
exp = VALUE ty_transfo_result( branchcode = '' ) ).
ENDMETHOD.
METHOD test3.
DATA(xml) ='<Invoice> '
&& ' <branchCode>A</branchCode>'
&& '</Invoice> '.
DATA(transfo_result) = VALUE ty_transfo_result( ).
CALL TRANSFORMATION (transfo_name)
SOURCE XML xml
RESULT root = transfo_result.
cl_abap_unit_assert=>assert_equals( act = transfo_result
exp = VALUE ty_transfo_result( branchcode = 'A' ) ).
ENDMETHOD.
METHOD setup.
transfo_name = create_transfo( concat_lines_of( sep = |\r\n| table = VALUE string_table(
( `<?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"/> ` )
( ` ` )
( ` <tt:template> ` )
( ` <Invoice> ` )
( ` <tt:cond s-check="not-initial(ref('ROOT.BRANCHCODE'))">` )
( ` <branchCode tt:value-ref="ROOT.BRANCHCODE"/> ` )
( ` </tt:cond> ` )
( ` </Invoice> ` )
( ` </tt:template> ` )
( ` ` )
( `</tt:transform> ` ) ) ) ).
ENDMETHOD.