Operators

Operators combine values into new values or into conditions. This page describes:

  • the arithmetic operators, including those that compute with dates and durations;

  • the comparison and logical operators;

  • the operators that test a value or a sequence: membership, pattern matching, presence, emptiness, uniqueness and duplicates.

The last entry explains how parentheses group the parts of an expression, and in which order the operators apply.

You write operators in lower case only. Their operands, that is, the values that they combine, can be literals, references, variables, function calls or other operations. Each operator accepts only certain data types. The translator checks them when it translates the expression, before any notice is processed (see Type checking).

Arithmetic operators

The arithmetic operators compute a number from two numbers.

Syntax
<number> + <number>
<number> - <number>
<number> * <number>
<number> / <number>
<number> % <number>
<number>

an expression that gives a single number (scalar): a number literal, a reference to a numeric field, a numeric variable, a function call that returns a number, or another arithmetic operation.

The operators are addition (+), subtraction (-), multiplication (*), division (/) and remainder (%). The result is a number.

Division gives a decimal result: 10 / 4 is 2.5. The remainder operator % gives the remainder of a division: 10 % 4 is 2, because 10 is twice 4, with a remainder of 2. The remainder has the sign of the first number: -10 % 4 is -2. You often use % to test whether a number is a whole number (BT-00-Number % 1 == 0) or an even number (BT-00-Number % 2 == 0).

Every numeric field gives a number, whatever its field type (for example number, integer or amount). So you can combine the values of different numeric fields (see Field types and their EFX data types).

Multiplication, division and remainder apply before addition and subtraction. Operators of the same level apply from left to right: 10 - 2 - 3 is 5. You can change this order with parentheses (see Parentheses and precedence).

When an operand has no value, for example a field that is not present in the notice, the result has no value either. A comparison with such a result is FALSE (see Comparison operators).

A minus sign written directly before a number belongs to the number literal: -3 is a negative number (see Number literal). EFX has no unary minus, that is, no minus sign that you can put before any expression to change its sign. Write 0 - BT-00-Number or -1 * BT-00-Number instead.

Restrictions

  • Both operands must be numbers. In particular, + does not join texts: use the function concat for this.

  • Each operand must be a single value. A reference to a field that can have several values from the context, such as BT-13-Number from ND-Root, is an error. To compute with all the values of a sequence, use a function such as sum, or an iteration.

  • Apart from numbers, +, - and * accept only the combinations of dates and durations described in Date and duration arithmetic.

Examples

BT-00-Number + 1
BT-00-Number * 2 - 1

The multiplication applies first, then the subtraction.

(BT-00-Number + BT-00-Integer) / 2

The average of two numeric fields. Because of the parentheses, the addition applies before the division.

BT-00-Number % 2 == 0

TRUE when BT-00-Number is an even number.

From the SDK (validation/rules.efx)
ASSERT every number:$ordImp in BT-541-Lot-WeightNumber[BT-5421-Lot == 'ord-imp'] satisfies ($ordImp % 1 == 0)

A rule that requires every weight of the lot award criteria of type ord-imp to be a whole number.

Date and duration arithmetic

The operators -, + and * also compute with dates and durations. With them, you can compute the time between two dates, move a date by a duration, add or subtract two durations, and multiply a duration.

Syntax
<date> - <date>
<date> + <duration>
<duration> + <date>
<date> - <duration>
<duration> + <duration>
<duration> - <duration>
<number> * <duration>
<duration> * <number>
<date>

an expression that gives a single date.

<duration>

an expression that gives a single duration.

<number>

an expression that gives a single number.

The difference between two dates is the duration from the second date to the first, in days. It is positive when the first date is later than the second: 2024-03-01Z - 2024-02-01Z is P29D. You usually compare such a duration with a limit, for example to check that the time between two dates is not longer than one year (see Comparison operators).

The sum or difference of two durations is a duration: P3W - P1D is P20D. The product of a number and a duration is the duration multiplied by that number, in either order: 3 * P1W is P21D, and P1M * 2 is P2M. A negative number gives a negative duration.

A date plus a duration, or a date minus a duration, is a date. For example, 2024-01-31Z + P1M is the last day of February 2024, and BT-00-EndDate - P30D is the date 30 days before BT-00-EndDate. In an addition, you can write the duration first (P30D + BT-00-StartDate), but not in a subtraction. The functions add-duration and subtract-duration give the same results.

Restrictions

  • You cannot add two dates: BT-00-StartDate + BT-00-EndDate is an error. You cannot subtract a date from a duration: P1D - BT-00-StartDate is an error.

  • You cannot add or subtract times, or combine a time with a duration: BT-00-StartTime - BT-00-EndTime and BT-00-StartTime + P1D are errors.

  • You cannot divide a duration: P2D / 2 is an error.

  • A duration is either in years and months, or in weeks and days (see Duration). You cannot add a duration of one kind to a duration of the other kind, or subtract one from the other: P1Y + P2D is an error.

Examples

BT-00-EndDate - BT-00-StartDate

The number of days from BT-00-StartDate to BT-00-EndDate, as a duration.

BT-00-EndDate - BT-00-StartDate <= P1Y

TRUE when the period from BT-00-StartDate to BT-00-EndDate is not longer than one year.

BT-00-Duration * 2

Twice the duration in BT-00-Duration.

BT-00-Number * BT-00-Duration

The duration in BT-00-Duration multiplied by the number in BT-00-Number.

BT-00-StartDate + BT-00-Duration <= BT-00-EndDate

TRUE when BT-00-StartDate plus BT-00-Duration is not later than BT-00-EndDate.

BT-00-EndDate - P30D > BT-00-StartDate

TRUE when BT-00-StartDate is more than 30 days before BT-00-EndDate.

EFX-1 could subtract two dates, and add or subtract two durations. To add a duration to a date, or to subtract it, EFX-1 needed the functions add-measure and subtract-measure, now called add-duration and subtract-duration. EFX-2 also accepts + and - for this. In EFX-1, the type duration was called measure.

Comparison operators

A comparison compares two values of the same type and gives an indicator: TRUE or FALSE.

Syntax
<value> == <value>
<value> != <value>
<value> < <value>
<value> <= <value>
<value> > <value>
<value> >= <value>
<value>

an expression that gives a single value (scalar) of type text, number, indicator, date, time or duration. Both values must have the same type.

Operator TRUE when

==

the two values are equal

!=

the two values are different

<

the first value is less than the second

<=

the first value is less than or equal to the second

>

the first value is greater than the second

>=

the first value is greater than or equal to the second

The order that <, <=, > and >= use depends on the type:

  • Numbers: by value.

  • Dates and times: in order of time.

  • Texts: character by character, in the order of the Unicode codes of the characters. This is not the order of a dictionary. Every upper-case letter of the Latin alphabet comes before every lower-case letter ('B' < 'a' is TRUE), and accented letters come after z.

  • Indicators: FALSE is less than TRUE.

  • Durations: EFX adds each duration to the current date and compares the two dates that result. So a duration in years or months and a duration in weeks or days can compare differently on different dates. For example, P1M > P30D is TRUE on 15 January, and FALSE on 15 February. Durations of the same kind always compare in the same way: P1Y == P12M and P1W == P7D are TRUE.

Codes are texts, so you compare the value of a code field with a text, for example BT-00-Code == 'a'.

A comparison gives an indicator, which you can compare in turn. EFX reads comparisons from the left, so 1 < 2 == TRUE means (1 < 2) == TRUE. The conditions on this page, such as in and is present, apply before the comparisons (see Parentheses and precedence).

When one of the values does not exist, for example because a field is not present in the notice, the comparison is FALSE, whatever the operator. This also applies to !=: BT-00-Text != 'a' is FALSE when BT-00-Text is not present. To accept that case, test the presence of the field as well, as in the last example below.

Restrictions

  • The two values must have the same type: BT-00-Text == 42 and BT-00-StartDate == BT-00-StartTime are errors. To compare values of different types, convert one of them first (see Type conversion).

  • Each value must be a single value. A reference to a field that can have several values from the context, such as BT-13-Text from ND-Root, is an error. Instead:

  • You write equality as ==. A single = is not a comparison operator: you use it in declarations.

Examples

BT-00-Number >= 10
BT-00-StartDate < BT-00-EndDate
BT-00-StartTime <= 12:00:00Z
BT-00-Indicator == TRUE
BT-00-Text is not present or BT-00-Text != 'a'

TRUE when BT-00-Text is not present, or when it is present with a value other than 'a'.

In EFX-1, you could compare a field with several values with a single value, and the comparison was TRUE as soon as one of the values matched. Because of this, mistakes could remain unnoticed, so EFX-2 reports such a comparison as an error. Write 'a' in BT-13-Text instead of BT-13-Text == 'a', or use a quantified expression.

Logical operators

The logical operators and and or combine two conditions into one.

Syntax
<condition> and <condition>
<condition> or <condition>
<condition>

an expression that gives an indicator, such as a comparison, a test described on this page, an indicator literal or an indicator variable.

and gives TRUE when both conditions are TRUE. or gives TRUE when at least one of the conditions is TRUE.

and applies before or: TRUE or FALSE and FALSE is TRUE, because EFX reads it as TRUE or (FALSE and FALSE). You can change this order with parentheses. Both operators apply after the arithmetic, comparison and test operators, so you can combine conditions without parentheses (see Parentheses and precedence).

EFX has no negation operator. To negate a condition, use the function not. The word not in not in, not like, is not present, is not empty and is not unique in is part of those operators.

Restrictions

  • Both operands must be indicators: BT-00-Text and TRUE is an error.

  • You write the operators as and and or: AND, && and || are errors.

Examples

BT-00-Number > 0 and BT-00-Number < 100
BT-00-Text is present or BT-00-Code is present
(BT-00-Text == 'a' or BT-00-Text == 'b') and BT-00-Number > 1

Without the parentheses, EFX would read the expression as BT-00-Text == 'a' or (BT-00-Text == 'b' and BT-00-Number > 1).

not(BT-00-Indicator == TRUE and BT-00-Number > 0)
BT-00-Indicator or BT-00-Number > 0

An indicator field used directly as a condition: its value is TRUE or FALSE.

Membership (in, not in)

The operator in tests whether a value is one of the values of a sequence.

Syntax
<value> in <sequence>
<value> not in <sequence>
<value>

an expression that gives a single value.

<sequence>

a sequence of values of the same type as <value>: values in square brackets, a codelist reference, a field reference, a variable, an iteration, or a function call that returns a sequence (see Sequence construction). You can also write a single value, which counts as a sequence of one value.

in gives TRUE when the value is equal to at least one value of the sequence. Values are equal in the same sense as for == (see Comparison operators). not in gives the opposite result.

A codelist reference, such as [...authority-activity], stands for the sequence of all the codes of the codelist (see Codelist reference). You can use a reference to any field as the sequence, whether or not the field can repeat.

When the value does not exist, for example because a field is not present in the notice, in gives FALSE and not in gives TRUE.

Restrictions

  • The value must be a single value. On the left of in, a reference to a field that can have several values from the context, such as BT-13-Text from ND-Root, is an error. To test all the values of a sequence, use a quantified expression.

  • The value and the sequence must have the same type: 'a' in BT-13-Number is an error.

Examples

BT-00-Code in [...authority-activity]

TRUE when the value of BT-00-Code is one of the codes of the codelist authority-activity.

BT-00-Number not in [0, 1]
BT-00-Text in BT-13-Text

TRUE when the value of BT-00-Text is equal to at least one value of BT-13-Text.

'x' in for text:$t in BT-13-Text return lower-case($t)

An iteration as the sequence. The iteration extends to the end of the expression. So, to combine the condition with another one, put the iteration, or the whole condition, in parentheses.

In EFX-1, you wrote sequences in parentheses, as in BT-00-Code in ('a', 'b'), and codelists as (codelist-name). EFX-2 uses square brackets: BT-00-Code in ['a', 'b'] and BT-00-Code in [...codelist-name]. The EFX-1 forms are errors in EFX-2. In EFX-1, the value on the left of in could also be a field with several values.

Pattern matching (like)

The operator like tests whether a text matches a pattern written as a regular expression.

Syntax
<text> like <pattern>
<text> not like <pattern>
<text>

an expression that gives a single text, for example a reference to a text or code field.

<pattern>

a text literal that contains a regular expression (see Regular expressions).

like gives TRUE when the text contains a match for the pattern. The match does not need to cover the whole text: the pattern '[0-9]' matches any text that contains a digit. To require the whole text to match, start the pattern with ^ and end it with $. not like gives the opposite result.

Before the match, EFX removes the spaces at the start and at the end of the text. It also replaces each run of spaces, tabs or line breaks inside the text with a single space. EFX matches a text that does not exist, for example a field that is not present in the notice, as an empty text.

The translator checks the pattern when it translates the expression. The pattern can use only the parts of regular expressions that EFX supports, so that it gives the same result in every environment.

Restrictions

  • The pattern must be a text literal. A variable, a field reference or any other expression is an error.

  • The pattern must use only the supported parts of regular expressions: '\d' is an error (see Regular expressions).

  • The value must be a text: BT-00-Number like '[0-9]+' is an error. To match the digits of a number, convert the number first with the function text.

  • The value must be a single value.

Examples

BT-00-Text like '^[A-Z]{2}-[0-9]{3}$'

TRUE when the whole text is two capital letters, a hyphen and three digits, as in AB-123.

BT-00-Text like '[0-9]'

TRUE when the text contains at least one digit.

BT-00-Code not like '^[a-z-]+$'

TRUE when the code contains a character other than a lower-case letter or a hyphen.

text(BT-00-Number) like '^[0-9]+$'
In EFX-1, the pattern could use any part of the regular expressions of XPath. EFX-2 accepts only the parts listed in Regular expressions.

See also: replace-regex, contains

Presence (is present)

The operator is present tests whether a field, a node or an attribute occurs in the notice.

Syntax
<reference> is present
<reference> is not present
<reference>

a field reference, a node reference, an attribute reference or a context variable. A reference can start with a slash, have predicates and an index, or follow a context override (see Field reference, Node reference and Attribute reference).

is present gives TRUE when the notice contains at least one occurrence of the field, node or attribute, looked up from the context of the expression. The value does not matter: a field that occurs with an empty value is present. For a field or node that can repeat, one occurrence is enough. With a predicate, at least one occurrence must meet the condition of the predicate.

is not present gives TRUE when there is no such occurrence.

Restrictions

  • You cannot test the presence of a literal, of a function call, or of a variable other than a context variable: 'a' is present is an error.

Examples

BT-00-Text is present
ND-SubNode is not present
BT-13-Text[BT-13-Text == 'a'] is present

TRUE when at least one occurrence of BT-13-Text has the value 'a'.

/BT-00-Text is not present

An absolute reference: the result is the same from any context.

In EFX-1, you could test the presence of fields and attributes only. EFX-2 also accepts nodes.

Emptiness (is empty)

The operator is empty tests whether a sequence has no values.

Syntax
<sequence> is empty
<sequence> is not empty
<sequence>

an expression that gives a sequence: a field reference, a variable, values in square brackets, an iteration or a function call that returns a sequence. You can also write a single value, which counts as a sequence of one value.

is empty gives TRUE when the sequence contains no value. is not empty gives TRUE when it contains at least one.

You can use a reference to any field, whether or not the field can repeat. The field is empty when it has no value from the context, that is, when it is not present in the notice. A field that is present with a blank value is not empty. To test whether a text is blank, use the function empty.

Examples

BT-13-Text is empty

TRUE when the notice contains no occurrence of BT-13-Text.

BT-00-Repeatable-Text is not empty
value-intersect(BT-13-Text, BT-14-Text) is empty

TRUE when BT-13-Text and BT-14-Text have no value in common.

In EFX-1, is empty tested a text: BT-00-Text is empty was TRUE when the field had an empty value, and FALSE when the field was not present. In EFX-2, is empty tests a sequence: BT-00-Text is empty is TRUE when the field is not present, and FALSE when it is present with an empty value. For the EFX-1 meaning, you now write empty(BT-00-Text).

Uniqueness (is unique in)

The operator is unique in tests whether a value occurs exactly once in a sequence.

Syntax
<value> is unique in <sequence>
<value> is not unique in <sequence>
<value>

an expression that gives a single value.

<sequence>

a sequence of values of the same type as <value>. You can also write a single value, which counts as a sequence of one value.

is unique in gives TRUE when exactly one value of the sequence is equal to the value. It gives FALSE in three cases: when the value occurs more than once in the sequence, when it does not occur at all, and when the value does not exist. is not unique in gives the opposite result.

You use this operator mostly to check that an identifier is not used twice in a notice. The sequence then contains all the values of the identifier in the notice, including the value that you test. You can give it, for example, with an absolute reference or with a variable.

Restrictions

  • The value must be a single value. On the left of is unique in, a reference to a field that can have several values from the context is an error.

  • The value and the sequence must have the same type.

Examples

BT-13-Text is unique in /BT-13-Text

In the context of ND-RepeatableInSubNode, TRUE when no other occurrence of BT-13-Text in the notice has the same value as the current one.

'b' is unique in ['a', 'b', 'c']

TRUE.

2 is unique in [1, 2, 3, 2]

FALSE, because 2 occurs twice.

From the SDK (validation/stage-5m.efx)
LET text* : $global-lot-ids = BT-137-Lot:rawValue;

A variable declared at the start of the validation stage collects all the lot identifiers of the notice.

From the SDK (validation/stage-5m.efx)
WITH BT-137-Lot
  ASSERT BT-137-Lot is unique in $global-lot-ids

A rule of the same stage then checks each lot identifier against that variable. The example leaves out the clauses that follow the condition.

In EFX-1, the value had to be a field or attribute reference, and the sequence an absolute field reference. EFX-2 accepts any single value and any sequence of the same type.

Duplicates (has duplicates, has no duplicates)

The operator has duplicates tests whether a value occurs more than once in a sequence.

Syntax
<sequence> has duplicates
<sequence> has no duplicates
<sequence>

an expression that gives a sequence: a field reference, a variable, values in square brackets, an iteration or a function call that returns a sequence. You can also write a single value, which counts as a sequence of one value.

has duplicates gives TRUE when at least two values of the sequence are equal. has no duplicates gives TRUE when all the values are different. A sequence with no value, or with a single value, has no duplicates.

Examples

BT-13-Text has no duplicates
[1, 2, 1] has duplicates

TRUE.

From the SDK (validation/rules.efx)
ASSERT BT-1351-Procedure-Language has no duplicates

A rule that forbids the same language twice.

has duplicates and has no duplicates are new in EFX-2.

Parentheses and precedence

Parentheses group the parts of an expression. When there are no parentheses, the precedence of the operators decides how EFX groups the parts.

Syntax
( <expression> )
<expression>

any expression.

An expression in parentheses has the same value and the same type as the expression inside. The parentheses make the expression inside apply as a whole, before the operators around it.

Without parentheses, operators apply in the order of the following table, from the highest precedence to the lowest. Operators on the same row have the same precedence.

Level Operators Grouping

1

Parentheses ( … ); function calls; and what follows a reference or another expression: predicates [ … ], indexes [at …], attributes /@…, context overrides :: and the properties :rawValue, :preferredLanguage and :preferredLanguageText

from left to right

2

* / %

from left to right

3

+ -

from left to right

4

The conditions, each applying before the ones listed after it: in and not in; is unique in and is not unique in; is empty and is not empty; has duplicates and has no duplicates; like and not like; is present and is not present

from left to right

5

== != < <= > >=

from left to right

6

and

from left to right

7

or

from left to right

8

if … then … else, for … return, every … satisfies, some … satisfies

the last part extends as far as possible to the right

For example, 1 + 2 * 3 is 7, because * applies before +, and (1 + 2) * 3 is 9. In BT-00-Number + 1 in [2, 3], the addition applies first, then in.

This order has three consequences:

  • A comparison takes the result of a condition on either side. For example, EFX reads BT-00-Indicator == BT-00-Text in ['a', 'b'] as BT-00-Indicator == (BT-00-Text in ['a', 'b']).

  • EFX reads operators of the same level from the left. For example, it reads TRUE == FALSE == FALSE as (TRUE == FALSE) == FALSE, which is TRUE.

  • The last part of a conditional expression, an iteration or a quantified expression extends as far as possible to the right. In if BT-00-Number > 0 then 1 else 2 == 3, the comparison 2 == 3 is the value after else. In every number:$n in BT-13-Number satisfies $n > 0 or BT-00-Text is present, the or belongs to the condition after satisfies. To use such an expression as an operand, put it in parentheses.

When an expression combines several operators, use parentheses to show the grouping that you intend.

Restrictions

  • Parentheses do not build a sequence: ('a', 'b') is an error. You write a sequence in square brackets (see Sequence construction).

Examples

1 + 2 * 3

7.

(1 + 2) * 3

9.

TRUE or FALSE and FALSE

TRUE, because and applies before or.

(TRUE or FALSE) and FALSE

FALSE.

(every number:$n in BT-13-Number satisfies $n > 0) or BT-00-Indicator == TRUE
(if BT-00-Number > 0 then 'positive' else 'other') == 'positive'

Because of the parentheses, the whole conditional expression is the left operand of ==.

In EFX-1, the grouping of an expression depended partly on the types of its parts. In EFX-2, it depends only on the order in the table above. If an expression relies on another grouping, you must add parentheses.