Skip to content

894: References in triggered calculations resolve relative to the trigger instead of the target - #896

Merged
lognaturel merged 4 commits into
XLSForm:masterfrom
lindsay-stevens:pyxform-894
Dec 1, 2026
Merged

lognaturel merged 4 commits into
XLSForm:masterfrom
lindsay-stevens:pyxform-894

Conversation

@lindsay-stevens

@lindsay-stevens lindsay-stevens commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

Closes #894

Why is this the best possible solution? Were any other approaches considered?

As noted in the commit messages it seems like this is the correct behaviour for the setvalue element's value attribute.

What are the regression risks?

It is a bug fix for a specific use case rather than a behaviour change i.e. triggered calculation inside repeat with non-sibling grouping of the trigger and calculate elements.

As part of the search for relevant code it seemed appropriate to also stop emitting a value attribute for non-setvalue elements. The ODK spec says such attributes are ignored but it's possible some form clients expected them, but it seems unlikely. If that looks too risky then the last commit in this PR could be dropped or moved to a different branch.

Does this change require updates to documentation? If so, please file an issue here and include the link below.

To me, the setvalue value evaluation context is important enough to clarify in the ODK spec, but maybe it's very obvious?

Before submitting this PR, please make sure you have:

  • included test cases for core behavior and edge cases in tests
  • run python -m unittest and verified all tests pass
  • run ruff format pyxform tests and ruff check pyxform tests to lint code
  • verified that any code or assets from external sources are properly credited in comments

- repro for pyxform/XLSForm#894 and similar cases where the source is in the
  lowest common ancestor repeat, and nested inside a repeat. In all
  cases the @value path is relative to itself, but the path should be
  relative to the source element "s".
- The ODK spec doesn't seem to explicitly say what the evaluation
  context for a setvalue should be, but in practice (according to
  pyxform/XLSForm#894), Collect and Web Forms use the "ref" path context.
- The ODK spec references the XForms 1.0 spec, which says in "4.3.6 The
  xforms-recalculate Event" that when recalculating the form, "the XPath
  expression is evaluated in the context of the instance node whose
  value or model item property is associated with the compute". This is
  somewhat ambiguous as to what "associated with" means, but section
  "10.1.9 The setvalue Element" is defined as "This action explicitly
  sets the value of the specified instance data node" and the "value"
  attribute is defined as: "Optional XPath expression to evaluate, with
  the result stored in the selected instance data node". Also, section
  "3.2.3 Single-Node Binding Attributes" prefaces the "ref" attribute
  definition with "The following attributes define a binding between a
  form control or an action and an instance data node defined by an
  XPath expression". Which together suggest that the "setvalue"
  attributes define "associated with", rather than e.g. the triggering
  element / document position / parent element of the setvalue element.
- trying to be more explicit/informative about how that value is used
- although setvalue is probably the most common action type for
  XLSForms, the XForms spec only defines the "value" attribute for the
  "setvalue" action, and the ODK spec explicitly says that any "value"
  attribute is ignored for "odk:setgeopoint" and "odk:recordaudio"
  (which currently are the only supported actions besides "setvalue").

@lognaturel lognaturel left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! Merging in now so it can get exercised. The minor refactoring looks good. I can't imagine value when it's not needed will be missed.

@lognaturel
lognaturel merged commit 8fe8030 into XLSForm:master Dec 1, 2026
14 checks passed
@lognaturel

Copy link
Copy Markdown
Contributor

Filed getodk/xforms-spec#376 for spec clarification.

@lognaturel lognaturel added the needs testing ready for QA review label Dec 1, 2026
@lognaturel

Copy link
Copy Markdown
Contributor

@dbemke @srujner @WKobus this change is now on the staging server and would benefit from a quick check with both Collect and Web Forms. It will be included in the upcoming release. Thank you!

@lindsay-stevens
lindsay-stevens deleted the pyxform-894 branch December 1, 2026 06:38
@srujner

srujner commented Dec 1, 2026

Copy link
Copy Markdown

@lindsay-stevens @lognaturel
We did a quick check. Looks like issue is fixed, but we encountered another one. Or maybe it is expected right now?

You can get this in e.g. those forms:
https://test.getodk.cloud/projects/655/forms/readonly_3_matrix_en/submissions
after using sliders in the SOURCE field

or this one:
https://test.getodk.cloud/projects/739/forms/min_setvalue_nested_repeat_en/submissions
After filling the: "B — trigger (outer level)" field

It is not possible to reproduce it on production server and it worked in the last regression testing

Zrzut ekranu 2026-12-01 080321

@srujner

srujner commented Dec 1, 2026

Copy link
Copy Markdown

@lindsay-stevens

lindsay-stevens commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor Author

@srujner Thanks!

For the file readonly_3_matrix_en the XForm produced by master (8fe8030) is identical to v4.5.0 output, so that issue is perhaps not from pyxform?

For the file min_setvalue_nested_repeat_en the form is invalid. On row 23 the trigger refers from c_target to c_trigger which is inside a nested repeat inner_c. The calculation also uses a variable reference, but doesn't define the repeat instance to select.

structure:

<outer_c><!-- repeat -->
  <c_target/>
  <inner_c_count/>
  <inner_c><!-- repeat -->
    <c_trigger>5</c_trigger>
  </inner_c>
</outer_c>

old output before this PR (invalid since ../c_trigger would (silently?) not resolve relative to c_target):

<setvalue ref="/data/outer_c/c_target" event="xforms-value-changed" value=" ../c_trigger  * 2"/>

new output after this PR (invalid since inner_c is a nodeset and no index or other filtering is provided):

<setvalue ref="/data/outer_c/c_target" event="xforms-value-changed" value=" ../inner_c/c_trigger  * 2"/>

To fix this the calculation in row 23 could use position() or indexed-repeat() or similar, to select a single c_trigger value. Or use an aggregate function sum(), max(), etc that accepts a nodeset argument and returns a single value.

The variable reference(s) in the trigger column doesn't need to do the same filtering, since it is specifying where the setvalue will go in the XForm body. But as a result the calculation might want to use once() to control recalculation.

@srujner

srujner commented Dec 1, 2026

Copy link
Copy Markdown

@lindsay-stevens
I can't change the labels, but behavior has been verified.

I'll create a separate issue in web forms for the readonly_3_matrix_en form and error

@lindsay-stevens lindsay-stevens removed the needs testing ready for QA review label Dec 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

References in triggered calculations resolve relative to the trigger instead of the target

3 participants