Sequences and iteration

A sequence is an ordered series of values of the same type, such as all the values of a repeatable field. This page explains:

  • how you write a sequence, or take one from the notice;

  • how you take a single value from a sequence by its position, with an index;

  • how an iteration computes a new sequence from the values of other sequences;

  • how a quantified expression tests the values of sequences.

The operators that test a sequence as a whole, such as in and is empty, have their own entries on the page Operators.

Sequence construction

You write a sequence as values in square brackets, or you take it from a reference, a variable, an iteration or a function call.

Syntax
[<value>]
[<value>, <value>, …]
<field-reference>
<codelist-reference>
<variable-reference>
<iteration>
<function-call>
( <sequence> )
<value>

an expression that gives a single value (scalar). All the values in the brackets have the same type.

<field-reference>

a reference to a field. The sequence contains the values of the field found from the context (see Field reference).

<codelist-reference>

a reference to a codelist, such as [...authority-activity]. The sequence contains all the codes of the codelist (see Codelist reference).

<variable-reference>

a reference to a variable or parameter declared with a sequence type, such as text* (see Variable declaration). Where a sequence is expected, a variable declared with a single-value type gives a sequence of one value.

<iteration>

an expression for … return … (see Iteration).

<function-call>

a call to a function that returns a sequence, such as distinct-values or value-union, or to a user-defined function declared with a sequence type.

<sequence>

any of the forms above. Parentheses around a sequence do not change it.

Square brackets give the sequence of the values written between them, in the order in which you write them. The values are not limited to literals. They can be field references, calculations, function calls or conditional expressions, as long as each of them gives a single value of the same type. You use the same notation, with literals only, for the values of sequence parameters (see Sequence literal).

A value in the brackets that does not exist, for example a field that is not present in the notice, adds nothing to the sequence. So count([BT-00-Text, BT-01-SubLevel-Text]) gives the number of these two fields that are present.

You can use any field reference where a sequence is expected. A field that gives a single value is then a sequence of one value, or of no value when the field is not in the notice. A field that can have several values from the context gives one value for each occurrence (see Repeatability).

Restrictions

  • The brackets contain at least one value: [] is an error.

  • All the values in the brackets must have the same type: [1, 'x'] is an error.

  • Each value in the brackets must be a single value. So you cannot put brackets inside brackets ([['a'], 'b'] is an error), and, inside brackets, a reference to a field that can have several values from the context is an error. To combine the values of two sequences, use a function such as value-union.

  • Parentheses do not build a sequence: ('a', 'b') is an error.

Examples

['works', 'supplies', 'services']
[BT-00-StartDate, BT-00-EndDate, 2024-12-31Z]

A sequence of dates that contains the values of two date fields and a date literal.

[BT-00-Number + 1, BT-00-Number * 2]
count([BT-00-Text, BT-01-SubLevel-Text])
value-union(BT-13-Text, BT-14-Text)

The values of BT-13-Text and BT-14-Text, without duplicates.

In EFX-1, you wrote sequences in parentheses, as in ('works', 'supplies', 'services'), and codelists as (codelist-name). In EFX-2, you write sequences in square brackets, and codelists as [...codelist-name]. The EFX-1 forms are errors in EFX-2.

Index

An index selects one value of a sequence, or one occurrence of a field or node, by its position.

Syntax
<sequence>[at <position>]
<sequence>

an expression that gives a sequence: a field or node reference, values in square brackets, a codelist reference, a variable, a function call that returns a sequence, or any other sequence in parentheses. It can already have predicates or another index.

<position>

an expression that gives a single number: 1 for the first value, 2 for the second, and so on.

The keyword at shows that the brackets hold a position. Brackets without at hold a condition, that is, a predicate.

An index selects the value at the given position in the whole sequence: BT-13-Text[at 2] is the second of all the values of BT-13-Text found from the context. The result is a single value of the type of the sequence. Positions start at 1. When there is no value at the position, the result has no value. This happens when the position is less than 1, greater than the number of values, or not a whole number. A comparison with such a result is FALSE (see Comparison operators).

EFX computes the position once, from the same context as the sequence, and not again for each value of the sequence. In BT-13-Text[at BT-00-Number], for example, EFX reads BT-00-Number from the same context as BT-13-Text. This is the difference from a predicate, whose condition EFX checks for each value. Inside an iteration, EFX computes the position again in each round: for number:$i in [1, 2] return BT-13-Text[at $i] gives the first two values of BT-13-Text.

After a field or node reference, an index selects one occurrence of the field or node. You can then use the result wherever you can use a field or node reference: with :rawValue, before /@, in a presence condition, or with a predicate, which then tests the selected occurrence. A predicate written before the index applies first: BT-13-Text[BT-13-Text != ''][at 1] is the first value that is not empty.

An index can also follow the field or node of a context. You can write it in the context of a template line, of a rule-set, of a context variable (in a WITH clause or in an iteration), in the INDEX clause of a dictionary and in a selector. The index then selects one of all the occurrences that the context matches. In a nested template line, it counts the occurrences within the current context of the parent template line (see Context changes).

Restrictions

  • The position must be a single number. BT-13-Text[at 'a'] is an error, and so is BT-13-Text[at BT-14-Number] from ND-Root.

  • A number in brackets without at is not an index: BT-13-Text[1] is an error, because EFX expects a condition.

  • Before an index, you must put an iteration, a conditional expression or a quantified expression in parentheses, as in the fourth example. Otherwise, its last part extends to the end of the expression.

Examples

BT-13-Text[at 1]

The first value of BT-13-Text.

['a', 'b', 'c'][at 2]

'b'.

BT-13-Number[at 1] + 1
(for number:$n in BT-13-Number return $n * 2)[at 1]

Twice the first value of BT-13-Number.

BT-13-Text[BT-13-Text != ''][at 1]

The first value of BT-13-Text that is not empty.

BT-00-Repeatable-Text[at 2]:rawValue == 'x'

The content of the second occurrence of a repeatable field.

WITH ND-RepeatableNode DISPLAY ${BT-00-Text-In-Repeatable-Node};
    WITH BT-23-Text[at 1] DISPLAY $value;

In a template file: for each occurrence of ND-RepeatableNode, EFX displays the nested template line for the first BT-23-Text only.

From the SDK (view-templates/25.efx)
${if (count($tpo_names) > 0) then concat(' - ', $tpo_names[at 1]) else ''}

An expression block that shows the first value of a sequence variable.

The index is new in EFX-2.

Iteration

An iteration computes a sequence. It computes an expression once for each value of one or more sequences, and puts the results together.

Syntax
for <iterator> return <expression>
for <iterator> return distinct <expression>
for <iterator>, <iterator>, … return <expression>
for <iterator>, <iterator>, … return distinct <expression>
<iterator>

<type>:$<name> in <sequence>. It declares an iterator variable $<name> that takes each value of <sequence> in turn (see Iterator variable). <type> is text, indicator, number, date, time or duration. <sequence> is a sequence of that type, or a single value, which counts as a sequence of one value. With the type context, <sequence> is a field or node reference, and the variable takes each occurrence of the field or node in turn (see Context variable).

<expression>

the expression that EFX computes for each value. It can refer to the iterator variables. It gives a single value or a sequence.

EFX computes the expression once for each value of the sequence of the iterator, in the order of the sequence. The result of the iteration is a sequence:

  • When the expression gives a single value, the result contains one value for each round.

  • When the expression gives a sequence, EFX joins the sequences of all the rounds, in order, into one sequence.

  • With distinct, EFX removes duplicate values from the result.

With several iterators, the iteration computes the expression for every combination of their values. The first iterator changes slowest: for number:$x in [1, 2], number:$y in [10, 20] return $x + $y gives 11, 21, 12, 22. An iterator can use the variables of the iterators written before it, as in for number:$x in [1, 2], number:$y in [$x, 10] return $y.

An iteration does not change the context: EFX reads a field reference in the expression from the context of the whole expression. To refer to the fields of the current occurrence of a context variable, use a context override, such as $n::BT-00-Text-In-Repeatable-Node (see Context override).

The iterator variables exist only inside the iteration. The expression after return extends as far as possible to the right: in for text:$t in BT-13-Text return $t == 'a' or BT-00-Text is present, the or belongs to the expression after return. You can write an iteration as the last operand of an operator, as in 'a' in for text:$t in BT-13-Text return $t. To use it as an operand anywhere else, for example before an index or before another operator, put it in parentheses.

Restrictions

  • The type of the iterator variable must match the type of the sequence: for number:$t in BT-13-Text return $t is an error.

  • A variable of type context goes through a field or node reference only: for context:$x in ['a'] return $x is an error.

  • You cannot use an iterator variable outside its iteration. An iteration nested in another cannot declare a variable with the same name.

  • You write for, in, return and distinct in lower case. In upper case, FOR and IN are keywords of rules, with another meaning.

Examples

for text:$t in BT-13-Text return upper-case($t)

The values of BT-13-Text, in capital letters.

for number:$n in BT-13-Number return $n * 2
for context:$n in ND-RepeatableNode return $n::BT-00-Text-In-Repeatable-Node

The value of BT-00-Text-In-Repeatable-Node in each occurrence of ND-RepeatableNode.

for context:$n in ND-RepeatableNode return distinct $n::BT-23-Text

For each occurrence of ND-RepeatableNode, the expression gives a sequence: the values of BT-23-Text in that occurrence. EFX joins these sequences and removes the duplicates.

for text:$t in BT-13-Text, text:$u in BT-14-Text return concat($t, '-', $u)

Every combination of a value of BT-13-Text with a value of BT-14-Text.

sum(for context:$n in ND-RepeatableNode return $n::BT-23-Number)
From the SDK (view-templates/10.efx)
${for text:$spcode in BT-06-Lot, text:$spdesc in BT-777-Lot[BT-06-Lot == $spcode] return $spdesc}

An expression block that displays, for each value of BT-06-Lot, the matching descriptions.

distinct, and iterations whose expression gives a sequence, are new in EFX-2. The EFX-1 iterator type measure is now duration.

Quantified expression

A quantified expression tests whether a condition is TRUE for every value of a sequence, or for at least one value.

Syntax
every <iterator> satisfies <condition>
some <iterator> satisfies <condition>
every <iterator>, <iterator>, … satisfies <condition>
some <iterator>, <iterator>, … satisfies <condition>
<iterator>

<type>:$<name> in <sequence>, as in an iteration.

<condition>

an expression that gives an indicator. It can refer to the iterator variables.

every gives TRUE when the condition is TRUE for every value of the sequence. some gives TRUE when the condition is TRUE for at least one value. With several iterators, EFX checks the condition for every combination of their values.

When the sequence has no value, every gives TRUE and some gives FALSE. So a rule that requires every value of a field to meet a condition is also met when the field is not present.

The condition extends as far as possible to the right: in every text:$t in BT-13-Text satisfies $t == 'a' or BT-00-Text is present, the or belongs to the condition. To combine a quantified expression with other conditions, put it in parentheses.

Restrictions

  • The condition must be an indicator: every text:$t in BT-13-Text satisfies $t is an error.

  • The type of each iterator variable must match the type of its sequence.

  • You write every, some, in and satisfies in lower case. In upper case, WITH EVERY has another meaning in rule-sets (see WITH clause).

Examples

every number:$n in BT-13-Number satisfies $n > 0
some text:$t in BT-13-Text satisfies $t like '^[0-9]+$'

TRUE when at least one value of BT-13-Text consists of digits only.

every text:$t in BT-13-Text satisfies $t in BT-14-Text

TRUE when every value of BT-13-Text is also a value of BT-14-Text.

some text:$t in BT-13-Text, text:$u in BT-14-Text satisfies $t == $u
every context:$n in ND-RepeatableInSubNode satisfies $n::BT-13-Text is present

TRUE when every occurrence of ND-RepeatableInSubNode contains BT-13-Text.

From the SDK (validation/rules.efx)
ASSERT every text:$lg in BT-1351-Procedure-Language satisfies $lg in value-union(BT-702(a)-notice, BT-702(b)-notice) or (BT-1351-Procedure is not present)

A rule condition in which the or belongs to the condition after satisfies. Each value of BT-1351-Procedure-Language must be one of the languages of the notice, or else BT-1351-Procedure must not be present.

The syntax has not changed. The EFX-1 iterator type measure is now duration.