Conditional execution

TopLeaf provides the facility to perform an action based on the value or existence of a variable. This is controlled using the following attributes:

  • var — The name of a variable containing a string or value that is to be tested. The declared var name may contain, but not begin with one or more variable references. This attribute must be present.

  • target — A string or value to test the variable against. This attribute is required, unless testing if the variable var is defined.

  • test — The test condition, which is one of:

    • same (identical to, the default if omitted)

    • before (lexically precedes)

    • after (lexically follows)

    • equal (numeric equality)

    • less (less than)

    • greater (greater than)

    • matches (pattern match, see “Pattern matching”)

    • defined (the variable exists, or in the case of an attribute variable, the attribute is explicitly declared in the tag or marker currently being mapped).

The not- prefix reverses the sense of the test being applied. For example, not-less means “not less than” or “greater than or equal to”.

The following examples show how to use these attributes within an <if> command, a flow control construct that allows the execution of its content when the specified condition is true.

Example 1: Test if the Name attribute contains the string Fred as its sole content:

<if var="@Name" target="Fred">...

Example 2: Test if the value of the Height variable is numerically equal to the value stored in the {Width} variable:

<if var="Height" test="equal" target="{Width}">...

Example 3: Test if the Word variable contains a string that begins with the letter A:

<if var="Word" test="matches" target="A*">...

Example 4: test if the string stored in the Name variable lexically precedes the literal string Smith:

<if var="Name" test="before" target="Smith">...

Example 5: Test if a width attribute is at least half the value stored in the ColumnWidth variable:

<if var="@width" test="not-less" target="{ColumnWidth}/2">...

Example 6: Test if the Name attribute does not contain Fred as a substring.

<if var="@Name" test="not-matches" target="*Fred*">...

Example 7: If the value assigned to the Index variable is 7, test if the variable City_7 contains the string Hobart:

<if var="City_{Index}" target="Hobart">...

Remember, the target string is interpreted as a number, a pattern or simple text depending on the test being applied.