ABAP Keyword Documentation → ABAP - Reference → Data Interfaces and Communication Interfaces → ABAP and XML → Simple Transformations → ST - Serialization and Deserialization → ST - Flow Control
ST - tt:switch, Case Distinction
Other versions: 7.31 | 7.40 | 7.54
Syntax
<tt:switch>
<tt:[s-|d-]cond ...
<tt:[s-|d-]cond ...
...
</tt:switch>
Effect
To execute only one transformation from a set of conditional transformations, you can use the statement tt:switch. A list of cases is specified in tt:switch, where each case is syntactically formulated by a conditional transformation, that is, a subelement tt:[s-|d-]cond. In this case, tt:[s-]cond defines a serialization-relevant case, and tt:[d-]cond a deserialization-relevant case. Other direct subelements are not possible in tt:switch.
In contrast to conditional transformations positioned outside of tt:switch, for a case there is no need to specify at least one attribute using, data, or check, provided that the content of tt:[s-|d-]cond is not a pattern.
In the list of cases, you must note the following:
- You may specify only one serialization-relevant case, in which no attribute using, data, or check is specified. This case is called standard serialization.
- You may specify only one deserialization-relevant case that does not contain a pattern. This case is called standard deserialization.
Serialization
Serialization follows these rules:
For a successful serialization, exactly one case is executed.
Deserialization
Deserialization follows these rules:
For a successful deserialization, exactly one case is executed.
Note
When programming case distinctions, you should formulate serialization and deserialization as much as possible in common cases tt:cond, whereas the directional cases tt:s-cond and tt:d-cond should be used only if no common formulation is possible.
Example
The transformation below during serialization sets the attribute size of element Paragraph depending on the value of the data node SIZE to exactly one value of "Small", "Big", or "Medium", where "Medium" is generated during standard serialization.
xmlns:tt="http://www.sap.com/transformation-templates">
<tt:root name="SIZE"/>
<tt:template>
<Paragraph>
<tt:attribute name="size">
<tt:switch>
<tt:s-cond check="SIZE<10">
<tt:text>Small</tt:text>
</tt:s-cond>
<tt:s-cond check="SIZE>20">
<tt:text>Big</tt:text>
</tt:s-cond>
<tt:s-cond>
<tt:text>Medium</tt:text>
</tt:s-cond>
<tt:d-cond using="exist(SIZE)" data="SIZE=8">
<tt:text>Small</tt:text>
</tt:d-cond>
<tt:d-cond using="exist(SIZE)" data="SIZE=16">
<tt:text>Medium</tt:text>
</tt:d-cond>
<tt:d-cond using="exist(SIZE)" data="SIZE=28">
<tt:text>Big</tt:text>
</tt:d-cond>
<tt:d-cond using="exist(SIZE)" data="SIZE=12">
<tt:skip/>
</tt:d-cond>
</tt:switch>
</tt:attribute>
<tt:text>Text</tt:text>
</Paragraph>
</tt:template>
</tt:transform>
If, for example, the input values for SIZE lie between 10 and 20, the result looks as follows:
During deserialization, data is used to assign to the data node SIZE a value 8, 16, or 28 for each of the three valid attribute values "Small", "Medium", and "Big". If a different attribute value is specified, the value 12 is assigned to SIZE in standard deserialization and the invalid attribute is skipped with tt:skip.
In the above transformation, serialization and deserialization are treated completely separately; they are divided within one single tt:switch element. Since there is no common case for serialization and deserialization, you can also use tt:serialize and tt:deserialize and two tt:switch elements to achieve the same result; in this case, you can omit the identifiers s- and d- before cond and you must query whether SIZE is bound to an ABAP data object only once:
xmlns:tt="http://www.sap.com/transformation-templates">
<tt:root name="SIZE"/>
<tt:template>
<Paragraph>
<tt:attribute name="size">
<tt:serialize>
<tt:switch>
<tt:cond check="SIZE<10">
<tt:text>Small</tt:text>
</tt:cond>
<tt:cond check="SIZE>20">
<tt:text>Big</tt:text>
</tt:cond>
<tt:cond>
<tt:text>Medium</tt:text>
</tt:cond>
</tt:switch>
</tt:serialize>
<tt:deserialize>
<tt:cond using="exist(SIZE)">
<tt:switch>
<tt:cond data="SIZE=8">
<tt:text>Small</tt:text>
</tt:cond>
<tt:cond data="SIZE=16">
<tt:text>Medium</tt:text>
</tt:cond>
<tt:cond data="SIZE=28">
<tt:text>Big</tt:text>
</tt:cond>
<tt:cond data="SIZE=12">
<tt:skip/>
</tt:cond>
</tt:switch>
</tt:cond>
</tt:deserialize>
</tt:attribute>
<tt:text>Blahblah</tt:text>
</Paragraph>
</tt:template>
</tt:transform>