I have a use case that requires the calculation of the Median value of a set collected in a Repeat Group. The use case if for measuring standing timber volumes. When you are measuring a stand you first measure the diameter of all stems in the plot. You then measure the height of a sub-set of those stems. In my use-case you measure the height of the maximum and median stem for every species present in the plot. I am able to calculate the maximum using the max() function, but there is no equivalent for median. I therefore tried adjusting the Xform code to include:

position(sort(${my_nodeset}), count(${my_nodeset}) div 2 + 1)

As a method of calculating the median. The form uploaded to ODK Central without issue but when I tried to use it with ODK Collect I got the following error message:

Error Occurred Error evaluating field ... The problem was located in Calculate expression for ... XPath evaluation: cannot handle function 'sort'

It seems that the XForm sort() function is not part of the ODK XForm specification.

I would like to add the XForm sort() function to enable the sorting of nodesets. This will allow me to calculate the median tree diameter in a plot.

This is not an area I'm familiar with but have you looked at the code repository for the ODK XForms spec? The readme includes a small section on proposing specification changes.

Thanks for describing your use case in detail, @Phil_Burapha!

Given that your underlying need is for computing a median, would a median function work for you? I don't see any such thing in core XPath/XQuery/XForm but I do see that MarkLogic has a custom median function: https://docs.marklogic.com/math:median

I can't find a reference to an XForm sort function, do you have a link? I see this article about an XLST-based alternative (wouldn't be supported by ODK tools). It looks like Orbeon Forms has a custom function.

Thanks for pointing to that spec proposal section, @danbjoseph! We don't do formal voting these days and should update that. This thread is a fine feature proposal and will be helpful for gauging level of interest/need.

Which is XPath 3.1 (!), and doesnt appear to be strictly a (new) XPath function per se, but rather a so-called 'higher order function'; something that appears to involve invoking the host language...

But I agree having something at least rudimentary to permit sorting multiple values from a nodeset (or selection?) in some form could potentially be useful for a lot of applications, beyond the basic current min() & max()

Yes - a median function would be perfect. But, I was also planning to make Upper Quartile and Lower Quartile calculations using a variation of my formula. Would it be possible to add also a Quartile function?