Start of Content Area

Syntax documentation Case Distinction  Locate the document in its SAP Library structure

To execute only one transformation from a set of conditional transformations, use the statement tt:switch:

 

<tt:switch>
 
case1
 
case2
  ...

</tt:switch>

 

In tt:switch, you can specify a list of cases case1, case2, ... Syntactically, every case case1, case2, ... is formulated by a conditional transformation, i.e. 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.

Note the following in the list of cases case1, case2, ...:

·        You may specify only one serialization-relevant case, in which no attribute using, dataor check is specified. This case is called standard serialization.

·        You may specify only one deserialization-relevant case, which does not contain a pattern. This case is called standard deserialization.

 

Serialization

Serialization follows these rules:

...

       1.      The first serialization-relevant case tt:[s-]cond, whose explicitly specified prerequisites are fulfilled, is serialized and the element tt:switch is left.

       2.      If for none of the serialization-relevant cases with explicitly specified attributes using, dataor check the prerequisite is fulfilled, then the standard serialization is executed (if it exists) and the element tt:switch is left.

       3.      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:

...

       1.      The first deserialization-relevant case tt:[d-]cond, which contains an appropriate pattern and whose prerequisites are fulfilled, is executed (deserialization and fulfilling the assertions) and the element tt:switch is left.

       2.      If no deserialization-relevant case, which contains a pattern, can be executed, the system tries to execute the 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.

       3.      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-condshould 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 the 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&lt;10">
            <tt:text>Small</tt:text>
          </tt:s-cond>
          <tt:s-cond check="SIZE&gt;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, during the standard deserialization the value 12 is assigned to SIZE 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 make the 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&lt;10">
              <tt:text>Small</tt:text>
            </tt:cond>
            <tt:cond check="SIZE&gt;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>

 

 

 

End of Content Area