Table of Contents |
---|
minLevel | 3 |
---|
outline | true |
---|
style | none |
---|
|
Description
The if statement selects a statement for execution based on the value of a boolean expression.
...
Code Block |
---|
language | text |
---|
theme | Eclipse |
---|
title | Conditional Statement Syntax |
---|
|
<if> <if (condition=”boolean-expression”) "boolean-expression">
<then>
embedded-statement
</then>
<else>
embedded-statement
</else>
</if>
|
Syntax Notes
Reserved keywords
- The boolean expression is evaluated for determining the path to take for execution.
- If the boolean expression yields true, control is transferred to the first embedded statement. When and if control reaches the end point of that statement, control is transferred to the end point of the if statement.
- If the boolean expression yields false and if an else part if present, control is transferred to the second embedded statement. When and if control reaches the end point of that statement, control is transferred to the end point of the if statement.
- If the boolean expression yields false and if an else part if not present, control is transferred to the end point of the if statement.
- if statements can be nested.
The key part of writing and using the if statement is creating boolean expressions. The boolean expression has to evaluate to true or false. The examples below provide different types
Examples
Code Block |
---|
language | xml |
---|
theme | Eclipse |
---|
title | If then else with constant expression |
---|
|
<if condition="true">
<then>
<Log Message="I am in true block!"/>
</then>
<else>
<Log Message='I am in false block!"/>
</else> |
Code Block |
---|
language | xml |
---|
theme | Eclipse |
---|
title | If then else with constant expression |
---|
|
<if condition="true">
<then>
<Log Message="I am in true block!"/>
</then>
<else>
<Log Message='I am in false block!"/>
</else> |
Code Block |
---|
language | xml |
---|
theme | Eclipse |
---|
title | Boolean value stored in some user defined tags |
---|
|
<if condition="[:RuntimeParams.EnforceKey:]">
<then>
<Log Message="I am in true block!"/>
</then>
<else>
<Log Message="I am in false block!"/>
</else>
</if>
</Query> |
Using XPath Boolean Expressions
There are two ways in which XPath Expressions can be used as part of boolean expression.
a) XPath as part of individual operand of the expression. For example: $ContextData/projectkey below.
Code Block |
---|
language | xml |
---|
theme | Eclipse |
---|
title | Boolean Expression for string comparison. |
---|
|
<if condition="$ContextData/projectKey eq '[:RuntimeParams.ProjectSettings_ProjectKey:]'">
<then>
<Log Message="I am in true block!"/>
</then>
<else>
<Log Message="I am in false block!"/>
</else>
</if> |
b) Whole boolean expression itself can be XPath expression. This is done using by starting the expression with "/"
Code Block |
---|
language | xml |
---|
theme | Eclipse |
---|
title | Entire Boolean Expression as XPath |
---|
|
<if condition="/'[:$Macro.Argument.StopWid:]' != '' and '[:$Macro.Argument.StopTarget:]' != ''">
<then>
<Log Message="I am in true block!"/>
</then>
<else>
<Log Message="I am in false block!"/>
</else>
</if> |
Using Multiple operands
Take a special note in second expression the >= within boolean expression must be escaped. For example: >= is written as >=
Code Block |
---|
language | xml |
---|
theme | Eclipse |
---|
title | Multiple Operands |
---|
|
<if condition="[:$Macro.Argument.RouteLoc:]/not(boolean(Site)) and '[:$Macro.Argument.SelfWid:]' != '' and '[:$Macro.Argument.StopTarget:]' != ''">
<then>
<Log Message="I am in true block!"/>
</then>
<else>
<Log Message="I am in false block!"/>
</else>
</if>
<if condition="/'[:$Macro.Argument.SiteCur:]' = '[:CONFIG.CompanyId:]' or
[:$ProcessData.$BTV.Worker.SlackCounter:] >= [:CONFIG.RULES.MaxWorkerSlack:] or
[:$LocalData.Rand:] > [:$ProcessData.$BTV.Worker.SlackTendency:]">
<then>
<Log Message="I am in true block!"/>
</then>
<else>
<Log Message="I am in false block!"/>
</else>
</if> |
Nested If Statements
Note that if you do not have else, usage of <then> is optional.
Code Block |
---|
language | xml |
---|
theme | Eclipse |
---|
title | Multiple Operands |
---|
|
<if condition="/'[:$Macro.Argument.StopWid:]' != '' and '[:$Macro.Argument.StopTarget:]' != ''">
<Log Message="I am in true block!"/>
<if condition="/'[:$Macro.Argument.SiteCur:]' = '[:CONFIG.CompanyId:]' or
[:$ProcessData.$BTV.Worker.SlackCounter:] >= [:CONFIG.RULES.MaxWorkerSlack:] or
[:$LocalData.Rand:] > [:$ProcessData.$BTV.Worker.SlackTendency:]">
<Log Message="I am in true, true block!"/>
</if>
</if> |
Examples of "if" Statements
Various examples of "if" statements
Code Block |
---|
language | xml |
---|
theme | Emacs |
---|
title | Examples of IF Statements |
---|
|
<!-- Combining number comparsion with non number comparison -->
<!-- Note here: count is a XPath function. This menthod essentially find number of Result Elements -->
<!-- Note that CPUUtilization and MemoryUtilitzation must return boolean -->
<If condition="[:/boolean('[:$LocalData.MonitorData.Find/count(Result) ne 0:]'
and ('[:$ProcessData.CPUUtilization:]'
lt '[:$ProcessData.MemoryUtilization:]')):]">
<Then>
</Then>
</If> |
Comparing Numbers from Context Data;
Code Block |
---|
language | xml |
---|
theme | Emacs |
---|
title | Examples of IF Statements |
---|
|
<Query>
<SetContextData Key="AMB" Value="90"/>
<If Condition="[:/[:$ContextData.AMB:] lt 100:]">
<Then>
<SetResponseData>
<Key>Message.Value.AMBResult</Key>
<Value>Less than 100</Value>
</SetResponseData>
</Then>
<Else>
<SetResponseData>
<Key>Message.Value.AMBResult</Key>
<Value>Greater than 100</Value>
</SetResponseData>
</Else>
</If>
</Query> |