ABAP xml

[转]
Part 1 - Expressiveness of Simple Transformations

Simple Transformations are a SAP proprietary programming language that is integrated into ABAP by CALL TRANSFORMATION in kernel release 6.40. Its concept differs from other transformation languages, like JiBX, which is used for XML-Java mapping, or languages suitable both for queries and transformations, like Lore, XCerpt, XDuce or even XSLT, which is also supported in ABAP by CALL TRANSFORMATION. By looking at examples of ABAP package SST_DEMO you will understand how ST works. In contrast to XSLT, Simple Transformations (ST) are fast, memory efficient and symmetric, but lack of expressiveness.

Unfortunately I can‘t describe the power of ST in a mathematical way because, as usual, conditions cause most technical problems and the <tt:cond> seems to be difficult to handle. In fact, despite its name, Simple Transformations have very powerful and sometimes complicated commands.

So, when I‘m asked about expressiveness, usually I tell how many times an ABAP or XML node can be accessed, mention the linear order during processing, lookahead of 1 and so on. If I start to tell about the possibilities to use parameters and breaking symmetry between serilization and deserilization, clever people will start to think how far they can go. A typical question is:

Can I create relational data models from nested structures with an unbounded number of occurrences during deserialization?

This is a natural question when you are doing data exchange with non-SAP systems using standardized XML frameworks and you want to save your data in transparent tables with additional foreign keys. If you are working with small datasets you won‘t have problems when you copy nested internal tables into the target data structure in ABAP. But let‘s ask the question if we can avoid heavy postprocessing. Of course, I would accept a pragmatic solution. Simple post processing in ABAP without copying data from one internal table into another would be acceptable.

The answer to the question above is, "no chance. You have to do heavy post processing." The reason is very simple and has to do with the <tt:loop> statement. Let‘s go into detail. In the following we consider an XML-document with a simple nested structure:

<A>
            <name>A1</name>
            <ZLS>
            <Z>01</Z>
            <Z>02</Z>
            </ZLS>
            </A>
            <A>
            <name>A2</name>
            <ZLS>
            <Z>03</Z>
            </ZLS>
            </A>
            

A typical example for a ST is to transform this XML document into an internal table whose table line contains an component ‘nummer‘ and another internal table:

<?sap.transform simple?>
            <tt:transform template="temp1"
            xmlns:tt="http://www.sap.com/transformation-templates">
            <tt:root name="ROOT"/>
            <tt:template name="temp1">
            <tt:loop ref=".ROOT" name="a">
            <A>
            <name>
            <tt:value ref="$a.name" />
            </name>
            <ZLS>
            <tt:loop ref="$a.zls" name="z">
            <Z>
            <tt:value ref="$z.nummer" />
            </Z>
            </tt:loop>
            </ZLS>
            </A>
            </tt:loop>
            </tt:template>
            </tt:transform>
            

Note that the transformation above is symmetric and can be used in both directions to serialize and deserialize.

If we want to transform the XML stream above to a relational data model first we have to break up the nested structure. The values of <A> elements have to be put into one internal table and then <Z> elements into another one. Afterwards we have to deal with the problem of how to link these internal tables. Without ABAP calls from ST we will have problems generating the foreign keys we need. But perhaps this could be done using clever mapping methods. If there is a new <A> element we write additional information to the <Z> elements. Think of ‘+‘ respective of ‘-‘ in an alternating order each time a new <A> occurs. Later we could calculate foreign keys in a post processing step in ABAP. The result after the transformation would be as follows:

Internal table for <A> elements:

Counter calculated afterwards Value of element <name>
  A1
  A2

Internal table for <Z> elements:

Additional mark Value of element <Z>
+ 01
+ 02
- 03

Now we can do two loops in ABAP. At first we increment the counter for our <A> elements and then introduce a counter for our <Z> elements that is incremented each time our mark changes from ‘+‘ to ‘-‘ or vice versa. We would yield the following result which is exactly what we are looking for:

Internal table for <A> elements:

Counter during postprocessing Value of element <name>
1 A1
2 A2

Internal table for <Z> elements:

Additional mark after postprocessing Value of element <Z>
1 01
1 02
2 03

So let‘s start to code. The following transformation does this job without calculating alternating marks. Just have a look at the inner loop. We change the root from ROOT1 to ROOT2:

<?sap.transform simple?>
                                                    <tt:transform template="temp1"
                                                    xmlns:tt="http://www.sap.com/transformation-templates">
                                                    <tt:root name="ROOT1"/>
                                                    <tt:root name="ROOT2"/>
                                                    <tt:template name="temp1">
                                                    <tt:loop ref=".ROOT1" name="a">
                                                    <A>
                                                    <name>
                                                    <tt:value ref="$a.name"/>
                                                    </name>
                                                    <ZLS>
                                                    <tt:loop ref=".ROOT2" name="z">
                                                    <Z>
                                                    <tt:value ref="$z.nummer" />
                                                    </Z>
                                                    </tt:loop>
                                                    </ZLS>
                                                    </A>
                                                    </tt:loop>
                                                    </tt:template>
                                                    </tt:transform>
                                                    

Then we test it with following quick-hack:

  DATA xml_string TYPE string.
                                                    data: begin of z,
                                                    mark type c,
                                                    nummer type string,
                                                    end of z.
                                                    data: begin of a,
                                                    counter type i,
                                                    name type string,
                                                    zl like table of z,
                                                    end of a.
                                                    data t_a like table of a.
                                                    data t_z like table of z.
                                                    xml_string = `<A>`                  &
                                                    `<name>A1</name>`   &
                                                    `<ZLS>`             &
                                                    `<Z>01</Z>`      &
                                                    `<Z>02</Z>`      &
                                                    `</ZLS>`           &
                                                    `</A>`                 &
                                                    `<A>`                  &
                                                    `<name>A2</name>`   &
                                                    `<ZLS>`             &
                                                    `<Z>03</Z>`      &
                                                    `</ZLS>`         &
                                                    `</A>`.
                                                    CALL TRANSFORMATION my_first
                                                    SOURCE  XML xml_string
                                                    RESULT  ROOT1 = t_a
                                                    ROOT2 = t_z.
                                                    

The result is annoying. The internal table t_a contains data of two <A> elements but t_z had only one entry with value "03".

The explanation is simple. When you want to deal with internal tables you have to use <tt:loop> statements. But everytime the <tt:loop> inner statement is called during deserialization the internal table is cleared, so in fact you are losing information in the example above. At first this example may look strange, just do the transformation back and look at the result, it will differ from the XML-document above! At first glance we have an asymmetric behaviour although we didn‘t use any asymmetric commands. At first I thought of it as a bug but switching the root makes sense. Why should ST inventors forbid those mechanisms and reduce expressivenes of this language?

In the following I want to mention a second aspect that seems to be confusing when you are confronted with ST for the first time.

You can assign the value of an element during deserilization only once!

In the rest of this issue we try to solve following task -- we want to deserialize the content of the <name> elements into one table and the nested <Z> elements into another if the content of its associated <name> element has value "A1". Perhaps you would start to define a variable <tt:variable name="N"/> for this task that stores the value of the current <A> element. I guess your code might look something like the following:

<?sap.transform simple?>
                                                    <tt:transform template="temp1"
                                                    xmlns:tt="http://www.sap.com/transformation-templates">
                                                    <tt:root name="ROOT1"/>
                                                    <tt:root name="ROOT2"/>
                                                    <tt:variable name="N"/>
                                                    <tt:template name="temp1">
                                                    <tt:loop ref=".ROOT1" name="a">
                                                    <A>
                                                    <name>
                                                    <tt:value ref="$a.name"/>
                                                    <tt:read type="C" var="N"/>
                                                    </name>
                                                    <tt:switch-var>
                                                    <tt:cond-var check="var(N)=C(‘A1‘)">
                                                    <ZLS>
                                                    <tt:loop ref=".ROOT2" name="z">
                                                    <Z>
                                                    <tt:value ref="$z.nummer" />
                                                    </Z>
                                                    </tt:loop>
                                                    </ZLS>
                                                    </tt:cond-var>
                                                    <tt:cond-var>
                                                    <tt:skip name="ZLS" count="1"/>
                                                    </tt:cond-var>
                                                    </tt:switch-var>
                                                    </A>
                                                    </tt:loop>
                                                    </tt:template>
                                                    </tt:transform>
                                                    

If you run this transformation you won‘t get the expected result. Why? Compared to XSLT, variables in ST behave like variables in any other procedural language and you can assign them for more than one time. But following two lines doesn‘t work:

            <tt:value ref="$a.name"/>
                                                    <tt:read type="C" var="N"/>
                                                    

First you want to read it into an ABAP structure and then bind it to a variable. Even changing the order of the two commands wouldn‘t help you. Let‘s test it with an easier example:

<?sap.transform simple?>
                                                    <tt:transform template="temp1"
                                                    xmlns:tt="http://www.sap.com/transformation-templates">
                                                    <tt:root name="ROOT1"/>
                                                    <tt:root name="ROOT2"/>
                                                    <tt:template name="temp1">
                                                    <X1>
                                                    <tt:value ref="ROOT1" />
                                                    <tt:value ref="ROOT2" />
                                                    </X1>
                                                    </tt:template>
                                                    </tt:transform>
                                                    

Here is the ABAP code for running this ST:

  DATA xml_string TYPE string.
                                                    DATA field1 TYPE string.
                                                    DATA field2 TYPE string.
                                                    xml_string = `<X1> Test </X1>`.
                                                    CALL TRANSFORMATION my_transformation
                                                    SOURCE  XML xml_string
                                                    RESULT  ROOT1 = field1
                                                    ROOT2 = field2.
                                                    

You will verify that ROOT2 is empty afterwards, so this doesn‘t work. Therefore we have to find another solution to implement our transformation, perhaps using the condition construct. But here we have another problem; the template content of the conditional is either a template (then you can‘t do assignments to variables for example) or it is evaluated unconditionally during deserialization. So, in fact, I can‘t tell you whether this will lead to success.

Summary
So let‘s summarize what we have learned. Programming ST is not difficult and ST does a great job transforming an XML document into nested ABAP structures, back and forth, because it is designed for this task. So to be honest, it‘s not surprising that transformations to a relational model is impossible without heavy post processing because for this task we have to create a set of internal tables from an XML tree and have to add foreign keys. In our small example above those keys are not part of our model so we would have to generate them and, of course, break the symmetry of our transformation thereby. The impossibility is caused by the fact that we can‘t increment variables and we can‘t append deserialized data to an internal table.

If we want to describe the (lack of) power of Simple Transformations I suggest to collect some simple examples for transformations we can‘t realize with ST and reduce other problems to them.

But does the lack of expressiveness really bother you? I don‘t believe in solutions that can solve every problem without causing new problems. Perhaps some new features that could help us will be added to ST in post 6.40 development but designers will have to be careful not to make the language too complex.

On the other hand, a software developer should try to choose the right technology -- iXML, XSLT, XSLT with ABAP calls or Java enhancement, ST or even JAXB -- and not to misuse a certain technology to create programs that can solve a problem in an unexpected way but are hard to understand and possibly difficult to maintain.

Check back soon for part 2 of this series!

ABAP xml

时间: 2024-11-06 13:46:14

ABAP xml的相关文章

事务代码STRANS的使用(XML与ABAP内表相互转换)

ABAP ---> XML  and XML --->ABAP 要求的XML格式如下: <handleMLPurchaseOrderServiceResponse> <out> <headers> <baseReturnHeader> <hresv1>4500524641</hresv1> <hresv2/> <hresv3/> <hresv4/> <hresv5/> <

ABAP内表数据做层次XML输出

*&---------------------------------------------------------------------**& Report  Z_BARRY_TEST_XML*&---------------------------------------------------------------------**& 46C - ECC6 通用*&----------------------------------------------

20170319 ABAP 生成XML文件

方法一:ABAP 使用method方式操作XML 转自:http://www.cnblogs.com/jiangzhengjun/p/4265595.html 方法二:STRANS 转换工具;使用strans 开发将内表转换xml文件,并可以将xml文件解析回到内表: 参考:http://www.cnblogs.com/wuqingbo/p/6229289.html ->一.XML定义? if_ixmlif_ixml_documentif_ixml_nodeif_ixml_elementif_i

[SAP ABAP开发技术总结]ABAP读取写XML文件

目录导航 声明:原创作品,转载时请注明文章来自SAP师太博客,并以超链接形式标明文章原始出处,否则将追究法律责任!原文出自: 20.6.         XML. 236 20.6.1.     生成... 237 20.6.2.     解析... 240 20.6.     XML if_ixml if_ixml_documentif_ixml_node if_ixml_element if_ixml_istream if_ixml_ostream document.element.ATTR

[SAP ABAP开发技术总结]IDoc

目录导航 声明:原创作品,转载时请注明文章来自SAP师太博客,并以超链接形式标明文章原始出处,否则将追究法律责任!原文出自: 18.4.         IDoc. 206 18.4.1.     数据段类型和数据段定义(WE31)... 206 18.4.2.     IDoc定义(WE30)... 207 18.4.3.     自定义IDoc发送与接收实例... 208 18.4.3.1.           发送端800(outbound)配置... 208 1.创建segment(WE

20170401 ABAP调用CIS webservice

问题: SAP  abap SRM java  调webservice 不通, CIS java  这边的webservice 可以通, WHY? key:请求头,系统框架的问题, LF:因为请求头的问题吧CX:请求头有什么差别?  LF:r3发出来的请求,header里面很复杂 ;大概不符合srm的要求, 这个最开始是 波哥调某个系统的时候出现的, 我搞不清楚为啥会出现,而且其他sap开发人员做接口不会出现这个问题吗,之前一直想找到原因,但还是因为不够执著,且能力也有欠缺未能成功find re

ABAP开发顾问必备:SAP ABAP开发技术总结

声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将追究法律责任!原文链接:http://www.cnblogs.com/jiangzhengjun/p/4260224.html 该文档是根据我过去多年学习文档与工作文档总结而成,项目开发过程中我都会参考此文档,主要ABAP很多细节上的东西不可能你记得很牢固,或者你记得一时,但过不了几天做别的项目就会

通过接口标准化ABAP OO开发

本文是对接口编程的讨论,希望能对年轻的开发者有所帮助. 要点: 通过接口对类方法进行更高层的抽象 接口使代码清晰易读 接口使你可以创建模拟对象(Mockup Object)以提高代码的可测试性 帮助实现SOLID原则 可以在不使用RTTS和类型转换的前提下使用多种类的不同实例. 因为在学习ABAP之前,我曾经学习过其它面向对象语言,因此我很纠结于ABAP中不存在的一个特性——重载方法(overload). 也许你会问,重载是什么? 重载就是函数或者方法有相同的名称,但是参数列表和实现不相同的情形

ABAP/4 技术总结 V3.0

SAP --ABAP/4 技术总结 V3.0 2014-10-14 --江正军 1.      基础... 1 1.1. 基本数据类型... 1 1.1.1.        P类型(压缩型)数据... 1 1.2.           TYPE.LIKE. 2 1.3.           DESCRIBE. 3 1.4.           字符串表达式... 3 1.5.           Data element.Domain. 4 1.6.           词典预定义类型与ABAP