简体   繁体   中英

SAP Simple Transformation with arbitrary XML tags

I have XML of unknown structure and I want to apply ST (Simple Transformation) on it to get "somehow" that content out of XML into ABAP structures.

For now I have following test report:

report  ztbu_st_with_copy.

data: lf_xml type string.
concatenate '<tab><obj>'
              '<id>A1</id>'
              '<first>Erste</first>'
              '<second>Zweite</second>'
            '</obj><obj>'
              '<id>B2</id>'
              '<item>'
                '<here>Tady</here>'
                '<there>Tam</there>'
              '</item>'
            '</obj>'
            '</tab>'
       into lf_xml.

types: begin of ys_obj,
         id type string,
         rest type string,
       end of ys_obj,
       yt_obj type standard table of ys_obj.

data: lt_obj type yt_obj.

call transformation ztbu_st_copy_test
  source xml lf_xml
  result root = lt_obj.

uline.

data: ls_obj like line of lt_obj.
loop at lt_obj into ls_obj.
  write: / sy-tabix, ls_obj-id.
endloop.

uline.

and I have following ST transformation ZTBU_ST_COPY_TEST (the one called above):

<?sap.transform simple?>
<tt:transform xmlns:tt="http://www.sap.com/transformation-templates">

<tt:root name="ROOT"/>

<tt:template>
  <tab>
  <tt:loop ref=".ROOT" name="obj">
      <obj>
        <id>
          <tt:value ref="$obj.ID" />
        </id>
        <tt:skip />
      </obj>
  </tt:loop>
  </tab>
</tt:template>

</tt:transform>

Now it works fine and it will bring IDs into the fields of table LT_OBJ. However the rest is ignored due to use of <TT:SKIP> . My goal would be to get the rest of XML document (these FIRST, SECOND, HERE and THERE or any arbitrary XML) into field REST in "some" format - probably as rough XML stored in STRING variable.

I understand I need to replace <TT:SKIP> with something smarter, but I can't figure it out what that should be... Any idea?

Side note: yes, I know, it would be better to use XSLT or something else, instead of ST, but I have no choice and I need to make it using ST.

ST are constraint since the can be used both ways (abap <-> xml). They are great because they are fast. But they map ABAP values to XML nodes and there's not much options there. I beleive you can't do this with ST. SXML would be best for your senario.

I've found a way to get raw XML in generated transformations for proxy services. For your example the transformation looks like this:

<?sap.transform simple?>
<tt:transform xmlns:tt="http://www.sap.com/transformation-templates">

  <tt:root name="ROOT"/>

 <tt:template>
    <tab>
      <tt:loop name="obj" ref=".ROOT">
        <obj tt:ap="y" tt:option="allNsps,noRootAttr" tt:value-ref="$obj.REST"/>
      </tt:loop>
    </tab>
  </tt:template>

</tt:transform>

Note that this doesn't parse the id. Everything within the obj tag is put into REST . Also the XML cannot be completely unknown because you have to know the surrounding tag of your fragment. (In this case obj )

To make it work the type of REST has to be of XSDANY (a RAWSTRING ). In your code:

TYPES: BEGIN OF ys_obj,
         id   TYPE string,
         rest TYPE xsdany,
       END OF ys_obj,
       yt_obj TYPE STANDARD TABLE OF ys_obj.

To transform xstrings to cstrings you can use class cl_proxy_service . In your write code:

LOOP AT lt_obj INTO ls_obj.
  WRITE: / sy-tabix, ls_obj-id, cl_proxy_service=>xstring2cstring( ls_obj-rest ).
ENDLOOP.

The important bit in the transformation is the tt:option attribute. You can look it up in the Keyword documentation.

If you have a SAP developer's license, this is very possible through the SDK provided with the license. I've written something similar using VB.NET, if you're interested in some samples let me know.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM