Context
An EFX expression is always read from a place in the notice, called its context. Every field and node reference in the expression starts from the context, and the context decides whether a reference gives a single value or a sequence. This page describes how the context is set, how references are read from it, and where it changes.
Evaluation context
The evaluation context is the one occurrence of a field or a node from which the references of an expression are read.
The construct around the expression sets it:
-
in a single expression or a selector, the field or node after
WITH(see Single expression); -
in a template file, the context declaration of each template line (see Context declaration); a template line without one has the root of the notice,
ND-Root, as its context; -
in a rules file, the
WITHclause of each rule-set (seeWITHclause).
The context is always a single occurrence: one lot, for example, not all the lots. When the notice contains several occurrences, a template line is displayed once for each of them, and the rules of a rule-set are checked once for each of them. A single expression is read for one occurrence, which the application that uses it chooses.
When the context is a node, the references are read from that node. When the context is a field, they are read from the place of that field. You can refer directly to the other fields of the same node. A reference to the context field itself gives its current value, a single value even when the field is repeatable.
A reference can reach any field or node of the notice, not only those below the context. The context decides whether it gives a single value or a sequence (see Repeatability).
Examples
WITH ND-SubNode COMPUTE BT-11-Number + BT-12-Number
Both fields are read from ND-SubNode.
WITH BT-23-Number COMPUTE BT-23-Number > BT-21-Number
The context is a field. BT-23-Number is its current value, and BT-21-Number, read from there, is a single value.
WITH ND-RepeatableNode DISPLAY ${BT-21-Number};
A template line that is displayed once for each occurrence of ND-RepeatableNode.
--- STAGE 1a ---
WITH ND-RepeatableNode
ASSERT BT-21-Number > 0
AS ERROR R-AB1-C2D
FOR BT-21-Number
IN *;
A rule that is checked once for each occurrence of ND-RepeatableNode.
See also: Context changes
Relative and absolute references
A reference is read from the context, unless it starts with a slash: then it is read from the root of the notice.
<field-identifier> <node-identifier> /<field-identifier> /<node-identifier>
- <field-identifier>, <node-identifier>
-
the identifier of a field or a node. Predicates, an index and the other parts of a reference can follow (see References).
A reference without a slash is relative. For a field or node below the context, it gives the occurrences inside the current occurrence of the context. For a field or node elsewhere in the notice, EFX goes from the context to the nearest node that contains both, and from there to the field or node.
A reference with a slash is absolute. It gives all the occurrences in the notice, whatever the context. So every repeatable node above the field counts when EFX decides whether the reference gives a single value or a sequence. Use an absolute reference to read all the occurrences of a field, not only those in the current context, and to reach the whole notice from inside a predicate (see Context changes).
For a field that is not below any repeatable node, such as BT-00-Number, both forms give the same value.
You write the context of a single expression without a slash: WITH /ND-Root COMPUTE … is an error.
Examples
WITH ND-RepeatableNode COMPUTE count(BT-23-Number) < count(/BT-23-Number)
Compares the number of values in the current occurrence of ND-RepeatableNode with the number in the whole notice.
WITH ND-Root COMPUTE count(BT-23-Number[/BT-00-Number > 5])
Inside the predicate, /BT-00-Number is read from the root, not from each value of BT-23-Number.
WITH ND-SubNode COMPUTE /ND-RepeatableNode is present
An absolute node reference.
See also: Absolute reference
Context changes
Inside an expression, a template file or a rules file, the context changes only in the following constructs.
Inside a predicate. A predicate is a condition in square brackets after a field or a node, which keeps some of its
occurrences. To test each occurrence, EFX reads the condition from that occurrence: in
BT-23-Number[BT-23-Text == 'x'], BT-23-Text is read from each value of BT-23-Number, and BT-23-Number inside
the brackets is that value. An absolute reference inside a predicate is still read from the root (see
Predicate).
With a context override. A context variable followed by :: becomes the context of the reference after it:
$node::BT-21-Number reads BT-21-Number from the occurrence that $node holds (see
Context override).
Not in an iteration. An iteration (for … return) or a quantified expression (every, some) does not change the
context. Its variable holds a value, not a place, and the references in its body are still read from the context of
the whole expression. To read the fields of each occurrence of a node, declare a context variable in the iteration,
context:$node in ND-RepeatableNode, and use it with ::.
In a nested template line. The context of a nested template line is read from the context of its parent, the line
under which it is indented. So the lines of a template file form a hierarchy of contexts. An index in the
context of a nested line, such as [at 1], counts the occurrences inside the current context of the parent. The
context variables of a line remain available in the lines nested under it.
In a rule-set. All the rules of a rule-set share the context of its WITH clause. The context does not change from
one rule to the next.
Examples
WITH ND-Root COMPUTE count(BT-23-Number[BT-23-Text == 'x'])
Counts the values of BT-23-Number whose BT-23-Text, in the same node, is x.
WITH ND-Root COMPUTE for context:$node in ND-RepeatableNode return $node::BT-21-Number
The value of BT-21-Number in each occurrence of ND-RepeatableNode. Without $node::, BT-21-Number would be read
from ND-Root each time, and give all the values.
WITH ND-Root COMPUTE every number:$n in BT-23-Number satisfies $n > BT-00-Number
BT-00-Number is read from ND-Root, the context of the whole expression.
WITH context:$node = ND-RepeatableNode DISPLAY Node;
WITH ND-RepeatableInRepeatableNode DISPLAY ${BT-23-Number} ${$node::BT-21-Number};
The nested line reads a field of the context of its parent, through the context variable $node.
EFX-1 also had XPath axes, such as preceding::, to move from the context. EFX-2 has no XPath axes: you reach fields
and nodes with relative and absolute references, predicates and context overrides.
|
See also: Context variable, Iteration, Context declaration