<switch> Element Reference

The <switch> element is similar in function to the switch statement in popular general purpose programming languages. Based on the evaluation of the expr attribute, a particular <case> element's contents will be parsed.

In order for it to be parsed, the <switch> element's expr attribute must evaluate to the <case> element's value attribute. Note, the value attribute is an integer, not an Expression.

The value of each <case> child must be unique.

There is no need for a corresponding break. Execution will only "fall-through" if the <case> being executed is empty.

If no matchess are found, and a <default> element exists as a child of the %switch, then its contents will be parsed. There can be at most one %default child.

Otherwise, nothing will be parsed.

Attributes
NameFormatDescription
expr (required)Expression

Detail

The following example illustrates the use of a <switch>. It describes a message of three octets. The first octet is used for the exrp in <switch> element. The second octet is read by the corresponding <case> contents, and the final octet is read into the check field.

    <xddl>
        <structure>
         <field name="choice" length="8"/>
         <switch expr="choice">
          <case value="1">
           <field name="a" length="4"/>
           <field name="b" length="4"/>
          </case>
          <case value="2">
           <field name="c" length="1"/>
           <field name="d" length="7"/>
          </case>
          <case value="3"/> <!-- "fall through" -->
          <case value="4">
           <field name="e" length="2"/>
           <field name="f" length="6"/>
          </case>
          <default>
           <field name="g" length="2"/>
           <field name="h" length="6"/>
          </default>
         </switch>
         <field name="check" length="8"/>
        </structure>
    </xddl>


    choice.xddl

We can parse the file with different messages to see the different paths are followed:

Here we follow the first case:

    # idm choice.xddl 0104FF

    Name                           Length Value    Hex        Description 
    choice                         8      1        01         
    a                              4      0        00         
    b                              4      4        04         
    check                          8      255      FF         

The "fall-through" case:

    # idm choice.xddl 031AFF 041AFF

    Name                           Length Value    Hex        Description 
    choice                         8      3        03         
    e                              2      0        00         
    f                              6      26       1A         
    check                          8      255      FF         

    choice                         8      4        04         
    e                              2      0        00         
    f                              6      26       1A         
    check                          8      255      FF         

Both of the above messages follow the value="4" case.

And finally the <default> case can be followed if we specify a choice that does not match any other <case>:

    # idm choice.xddl AAFEFF

    Name                           Length Value    Hex        Description 
    choice                         8      170      AA         
    g                              2      3        03         
    h                              6      62       3E         
    check                          8      255      FF