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

How to allow an empty string with check condition in xml transformation?

Former Member
0 Kudos
677

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.
 

 

4 REPLIES 4
Read only

Sandra_Rossi
Active Contributor
0 Kudos
627

s-check applies only to Serialization. You are currently Deserializing (SOURCE XML).

NB: don't confuse space (one space) and empty string (zero space).

Read only

0 Kudos
573

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?

Read only

0 Kudos
563

Look at the documentation. s-check, d-check, check.

Read only

0 Kudos
545

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.