Operators

The expression language includes a number of operators, which are used to perform calculations and comparisons. Operators give purpose to the functions described above, allowing the returned values to be compared with each other and with fixed values. Comparisons for equality, for inequality and for comparative size can be made.

Addition and subtraction ('+ and '-')

Numeric values can be added together using the '+' operator, and subtracted using '-':

<if test="0 + 1"><!-- ENABLED (1) --></if>

<if test="1 - 1"><!-- NOT ENABLED (0) --></if>

Note that, to avoid confusion with hyphens in names, the '-' symbol is recognized as a minus by adding whitespace before it:

test="first-node - second-node"

Equality and inequality ('=' and '!=')

The '=' operator compares two items for equality. If they are equal, the boolean value 'true' is the result. Otherwise, the value 'false' is the result. Boolean values, numeric values and strings can all be compared using this operator:

<if test="true() = true()"><!-- BOOLEAN COMPARE --></if>

<if test="3 = 3"><!-- NUMERIC COMPARE --></if>

<if test="'Hi' = 'Hi'"><!-- STRING COMPARE --></if>

In the following example, two strings are compared. Some action is taken if the text content of the current element contains '234' at position three:

<number>123456789</number>]

test="self::node()[substring(text(),3,3) =
						'345'"]

Before making the comparison, however, the two sides must be made to be of the same object type (if they are not already). This is done automatically. If one side is a boolean value, the other side is converted into a boolean value. Otherwise, if one side is a number, the other side is converted to a number. Otherwise, if one side is a string, and the other is not, it is made into a string. In the following example, a string is compared with a number. As one of the items is a number, the other one (the string) is converted into a number. The two numbers are then compared:

<if test="'3' = 3"><!-- ENABLED --></if>

<if test="7 = '3'"><!-- NOT ENABLED --></if>

The opposite of equality is inequality. Placing an exclamation mark before the equals symbol, '!=', indicates the need for the compared items to be different rather than the same. This test can be made wherever a test for equality can be made. The result is 'true' if the compared items differ:

<if test="3 != 3"><!-- NOT ENABLED --></if>

<if test="'3' != 7"><!-- ENABLED --></if>

Larger and smaller ('<' and >')

When two values are different, it is sometimes useful to know which of the two values is greater. The '<' symbol between two values tests whether or not the first value is smaller than the second, returning 'true' if it is. However, the rules of XML mean that this symbol must be replaced by the sequence '&lt;' (less than):

<if test="9 &lt; 3"><!-- NOT ENABLED --></if>

<if test="3 &lt; 9"><!-- ENABLED --></if>

Similarly, the '>' symbol between two values tests whether or not the first value is larger than the second. In this case, the equivalent entity reference can be used ('&gt;' (greater than)), but it is not necessary:

<if test="9 > 3"><!-- ENABLED --></if>

<if test="3 &gt; 9"><!-- NOT ENABLED --></if>

Numeric comparisons are made, provided that neither value is a node or set of nodes (explained later). Strings and booleans are converted to numbers ('0' or '1' in the latter case) prior to making the comparison.

At least ('<=' and '>=')

A slight variation of less-than and greater-than comparisons allows a different question to be asked. Is is sometimes useful to know whether a value is at least as great as another, or at most as great as another. This is done simply by combining the '<', '>' and '=' symbols.

The combination '>=' means 'greater than or equal to'. It is important that these symbols appear in this order:

<if test="4 >= 3"><!-- ENABLED (greater) --></if>

<if test="3 >= 3"><!-- ENABLED (identical) --></if>

The combination '<=' (actually '&lt;=') means 'less than or equal to'. Again, the ordering of the two symbols is important:

<if test="3 &lt;= 4"><!-- ENABLED (smaller) --></if>

<if test="3 &lt;= 3"><!-- ENABLED (identical) --></if>

And

The 'and' operator is used to connect two expressions. Both sub-expressions must return a boolean value, and both must be true for the complete expression to be true. If the first part of such as expression is false, the processor does not need to bother to evaluate the second part, as the whole expression must be false:

<if test="9 > 5 and 5 > 1"><!-- ENABLED (both OK) --></if>

<if test="9 > 5 and 1 > 5"><!-- NOT ENABLED --></if>

<if test="5 > 9 and 5 > 1"><!-- NOT ENABLED --></if>

<if test="5 > 9 and 41 > 5"><!-- NOT ENABLED --></if>

Or

The 'or' operator is also used to connect two expressions. Either sub-expression can be true for the complete expression to be true. The four examples below are identical to the ones above, except for the operator in use. In this case, though, three of the tests are successful instead of just one:

<if test="6 > 5 or 5 > 4"><!-- ENABLED (both OK) --></if>

<if test="6 > 5 or 4 > 5"><!-- ENABLED (left OK) --></if>

<if test="5 > 6 or 5 > 4"><!-- ENABLED (right OK) --></if>

<if test="5 > 6 or 4 > 5"><!-- NOT ENABLED --></if>

Precedence

When an expression contains a number of operators, interpretation depends on the precedence assigned to each operator type. This is important because it would otherwise be possible to interpret the same expression in a number of different ways. For example, '5 < 6 and 9 < 8' would, if simply read from left to right, compare '5 < 6' (true), then test the 'and' condition against '9' (true, as 'true' and '9' are both true), before checking that the result is smaller than '8' (true, because 'true' is equivalent to '1', which is smaller than '8'). Interpreted this way, the expression would be true, but interpreted with '<' as a higher precedence than 'and' gives a different result. '5' is smaller than '6', so this is true, but '9' is not smaller than '8', so this is not true, and the two halves of the expression are not both true. The expression therefore returns 'false'.

The '<', '>', '<=' and '>=' operators have highest precedence, followed by '=' and '!=', and finally 'and' and 'or'. The examples above therefore work as intended. In the example below, six is greater than five, so this part of the expression is true, and three is smaller than four, so this part of the expression is also true. Therefore, the left part equals the right part (they are both true), so the whole expression is true:

<if test="6 > 5 = 3 < 4"><!-- ENABLED (both true) --></if>

The '+' and '-' operators described earlier have lower precedence than any of these:

<if test="1+1 > 9-8">
  <!-- ENABLED (2 greater than 1) -->
</if>

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset