2

I am writing a serialization through ST and it should put several vars/nodes into one XML tag.

Input ABAP itab that should be serialized:

ROW_ID       ROW
               VAL       INDEX
  1            val55        X
               val32        Y
               val46        X

  2            val8         X
               val16        
               val789        

  3            val78        Y
               val53        Y
               val98        Y

where each ROW value is an itab which consists of VAL and INDEX fields

Expected output sample for first row:

  <rows>
    <row r="1">
        <c r="1_xcell1">
            <v>val55</v>
        </c>
        <c r="1_ycell2">
            <v>val32</v>
        </c>
        <c r="1_xcell3">
            <v>val46</v>
        </c>
     <row>
   </rows>

Here

1 in <row r="1"> corresponds to row number from ROW_ID

1_xcell1 in <c r="1_xcell1"> is a concatenation of ROW_ID from the current line of root table, INDEX field of current ROW line, literal cell and loop counter of ROW table

The transformation I ended up with is:

<?sap.transform simple?>
<tt:transform xmlns:tt="http://www.sap.com/transformation-templates" template="main">
  <tt:root name="root"/>
  <tt:variable name="range" val="11"/>
  <tt:template name="main">
      <rows>
        <tt:loop name="row" ref="root">
          <row>
            <tt:attribute name="r" value-ref="row_id"/>
            <tt:assign to-var="range" ref="row_id"/>
            <tt:loop name="cells" ref="$row.ROW">
            <tt:serialize>
              <c>
                <tt:attribute name="r"><tt:write var="range" map="val(I(1)) = xml('1_xcell1'), val(I(2)) = xml('2_xcell2'), val(I(3)) = xml('3_xcell3)"/></tt:attribute>
                <v><tt:value ref="value"/></v>
              </c>
            </tt:serialize>
            </tt:loop>
          </row>
        </tt:loop>
      </rows>
  </tt:template>
</tt:transform>

where I put the ROW_ID into var for using in mapping on the lower lever in <c> tag.

As you can see, this approach will not work because I have no idea how to put loop counter and how to concatenate it with other values.

I found a couple of old threads on answers.sap.com (1, 2, 3), but they are left unanswered.

Is there a concatenation in ST like in XSLT:

`<xsl:element name="{concat($segment2, '_', $tail2)}">`?

Is there a built-in variable for loops like SY-TABIX in ABAP?

Sandra Rossi
  • 11,934
  • 5
  • 22
  • 48
Suncatcher
  • 10,355
  • 10
  • 52
  • 90

2 Answers2

2

This should work (if $row.row_id equals 1, it will generate ...<c r="1_xcell1">...):

            ...
              <c>
                <tt:attribute name="r">
                  <tt:value ref="$row.row_id"/>_xcell<tt:value ref="$row.row_id"/>
                </tt:attribute>
              ...

Concerning the loop counter, I think it can't be done within Simple Transformations, your solution to pass the row number explicitly inside the internal table is the best.

Sandra Rossi
  • 11,934
  • 5
  • 22
  • 48
-1

If you have schema (xsd) file, you can convert it to wsdl with report SPROX_XSD2WSDL then create service consumer with it. You can use below code to use this service consumer for getting xml from auto generated trasform (Also you can check this transform for getting hint for your solution).

  data: lo_wph     type ref to cl_ws_payload_handler.
  data: lt_param   type prx_t_param,
        ls_param   type prx_s_param,
        ls_struct  type zmymessage,
        lv_xmldata type xstring.

      create object lo_wph
        exporting
          proxy_type   = 'CLAS'
          proxy_name   = 'ZMYPROXY'
          proxy_method = 'OP1'.

      ls_param-name = 'INPUT'.
      ls_param-type_name = 'zmymessage'.
      ls_param-ifr_name = 'MyXmlRoot'.
      ls_param-ifr_namespace = 'http://myschemanamespace'.
      ls_param-xml_name = 'MyXmlRoot'.
      ls_param-xml_namespace = 'http://myschemanamespace'.
      get reference of ls_struct into ls_param-value.
      append ls_param to lt_param.

      call method lo_wph->if_ws_payload_handler~get_payload_from_request_data
        exporting
          request_data = lt_param
        receiving
          payload      = lo_wp.

      lv_xmldata = lo_wp->get_xml_binary( ).
mkysoft
  • 5,392
  • 1
  • 21
  • 30