What is the best practice to bind an XForms nodeset to a metadata value?

1. What is the problem? Be very detailed.
I want to display metadata (Username) to the enumerator to remind them to update to their own name.

One way of doing so would be to set the default value of a field (text, read-only) to a metadata value (here: Username) and prompt the user to go back and update said field.

I managed to do so by hand-editing the XML:

/* The original metadata field of kind "Username" tells JavaRosa to preload its "property" with param "username". */
<bind nodeset="/data/reporter" type="string" jr:preload="property" jr:preloadParams="username"/>

/* A read-only text field as built in ODK Build */
<bind nodeset="/data/site_visit/reporter_repeat" type="string" readonly="true()"/>

/* Adding the two jr: properties to the read-only text field results in the Username being pre-loaded in the value */
<bind nodeset="/data/site_visit/reporter_repeat" type="string" jr:preload="property" jr:preloadParams="username" readonly="true()"/>

So that works, but hand-editing XML is a source of error (requires additional form validation) and a manual step (my forms live in ODK Build).

4. What steps can we take to reduce the problem?
Exactly my question. Is there a better/safer/more reproducible way than to hand-edit XForm XML?

5. Anything else we should know or have? If you have a test form or screenshots or logs, attach below.
I've attached the form in question as ODK Build savefile, as (modified) XML, and as XLSForms.
SiteVisitStart.zip (6.2 KB)

Related idea to place metadata in ODK Collect main menu: ODK Collect widget/shortcut for setting metadata (e.g. username) - #13 by Florian_May

Hi Florian,

We do such a thing with another approach, the user name and email widget are prefilled (not read-only) with default value taken from metedata (calculation). And a note warns the user to modify if it is wrong and to correctly fill the metadata in Collect.
But I do not know how to do it with ODKBuild.
Recently I asked myself about using the last_saved_value(possibility), not for email or username which concern all forms in a collect devices, but for "form custom settings" that are actually stored in the phone number metadata field.

1 Like

Is this what you want? (.odkbuild file, try loading it into ODKBuild)

{"title":"username","controls":[{"name":"name","kind":"Username","metadata":{},"type":"metadata"},{"name":"showusername","label":{"0":"your name is"},"hint":{},"defaultValue":"","readOnly":false,"required":false,"requiredText":{},"relevance":"","constraint":"","calculate":"/data/name","length":false,"invalidText":{},"metadata":{},"type":"inputText"}],"metadata":{"version":2,"activeLanguages":{"0":"English","_counter":0,"_display":"0"},"optionsPresets":[],"htitle":null,"instance_name":"","public_key":"","submission_url":""}}

Sorry I saw that just now - "calculate":"/data/name" seems to work just fine.
Note to future Florian: calculate refers to my field codes, whereas jr:preload="property" jr:preloadParams="username" loads the metadata property "username".