Skip to content

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:

  • The first serialization-relevant case tt:[s-]cond, whose explicitly specified prerequisites are fulfilled, is serialized and the element tt:switch is left.
  • If for none of the serialization-relevant cases with explicitly specified attributes using, data, or check the prerequisite is fulfilled, the standard serialization is executed (if it exists) and the element tt:switch is left.
  • If no standard serialization exists, the exception CX_ST_SWITCH_NO_CASE occurs.
  • For a successful serialization, exactly one case is executed.

    Deserialization

    Deserialization follows these rules:

  • The first deserialization-relevant case tt:[d-]cond that contains a suitable pattern and whose prerequisites are fulfilled is executed (deserialization and fulfilling of the assertions) and the element tt:switch is left.
  • If no deserialization-relevant case that contains a pattern can be executed, the system tries to execute standard deserialization, if it exists. After the execution, the element tt:switch is left. If the possible prerequisites using, data, or check of the standard deserialization are not fulfilled, the exception CX_ST_REF_ACCESS occurs.
  • If no standard deserialization exists, the exception CX_ST_SWITCH_NO_CASE occurs.
  • 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.

    <tt:transform
      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:

    <Paragraph size="Medium">Text</Paragraph>

    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:

    <tt:transform
      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>