What you would like to do is apply the substr
function to each result of your lookup expression and then compute the maximum value of that resulting set.
What you've tried doesn't quite work because calling substr
on a result with multiple values doesn't work. You'd like to call it on each individual value instead but we don't have a way to express that. Similarly, you can't call max
on a comma-separated string, only on a result set of multiple values.
You can kind of sort of almost do what you want with a repeat but currently there isn't a way to hide repeat from the user or prevent it from being submitted so it's not ideal. @Xiphware has built a number of interesting forms using this technique and you can see an example at ODK geofence (v1)
What I would recommend now is storing the individual components of the ID in addition to the full ID. It doesn't feel great to have that redundancy but it shouldn't have a big impact on performance or data storage since the values are so small and use a limited range of characters. To be explicit, I mean storing CRS-2344-DAF1
, 2344
and 1
and ensuring through your form design that they are always synchronized.
As a side note, all of the computations that we do in forms use an expanded subset of XPath 1.0. XPath 2.0+ includes syntax like:
instance('external_csv')/root/item[search_column = ${matching_value}]/substr(value, 4, 8)
This makes it possible to specify a function/functions to apply to each value in a nodeset and would address this need. That's something we'd like to support one day.