简体   繁体   English

带有任意 XML 标签的 SAP 简单转换

[英]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.我有未知结构的 XML,我想在其上应用 ST(简单转换)以“以某种方式”将 XML 中的内容转换为 ABAP 结构。

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):我有以下 ST 转换 ZTBU_ST_COPY_TEST (上面称为的那个):

<?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.现在它工作正常,它会将 ID 带入表 LT_OBJ 的字段中。 However the rest is ignored due to use of <TT:SKIP> .但是,由于使用了<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.我的目标是将 XML 文档的其余部分(这些 FIRST、SECOND、HERE 和 THERE 或任何任意 XML)以“某种”格式放入字段 REST - 可能是存储在 STRING 变量中的粗略 XML。

I understand I need to replace <TT:SKIP> with something smarter, but I can't figure it out what that should be... Any idea?我知道我需要用更智能的东西替换<TT:SKIP> ,但我不知道那应该是什么......知道吗?

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.旁注:是的,我知道,最好使用 XSLT 或其他东西,而不是 ST,但我别无选择,我需要使用 ST。

ST are constraint since the can be used both ways (abap <-> xml). ST 是约束,因为可以同时使用两种方式(abap <-> xml)。 They are great because they are fast.他们很棒,因为他们很快。 But they map ABAP values to XML nodes and there's not much options there.但是它们将 ABAP 值映射到 XML 节点,并且没有太多选项。 I beleive you can't do this with ST.我相信你不能用 ST 做到这一点。 SXML would be best for your senario. SXML 最适合您的场景。

I've found a way to get raw XML in generated transformations for proxy services.我找到了一种在生成的代理服务转换中获取原始 XML 的方法。 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.请注意,这不会解析 id。 Everything within the obj tag is put into REST . obj标签中的所有内容都放入REST Also the XML cannot be completely unknown because you have to know the surrounding tag of your fragment.此外,XML 不能完全未知,因为您必须知道片段的周围标记。 (In this case obj ) (在这种情况下obj

To make it work the type of REST has to be of XSDANY (a RAWSTRING ).为了使其工作, REST的类型必须是XSDANY (一个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 .要将 xstrings 转换为 cstrings,您可以使用类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.转换中的重要部分是tt:option属性。 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.如果您拥有 SAP 开发人员许可证,则可以通过随许可证提供的 SDK 实现这一点。 I've written something similar using VB.NET, if you're interested in some samples let me know.我已经使用 VB.NET 编写了类似的内容,如果您对某些示例感兴趣,请告诉我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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