Sort order in casedb XPath queries

64 views
Skip to first unread message

crei...@nd.edu

unread,
Nov 24, 2014, 9:44:03 AM11/24/14
to commcar...@googlegroups.com
Hi all,

In some of our forms, we have the need to pull data from different case lists, and we are using the casedb instance method found here: https://help.commcarehq.org/display/commcarepublic/Multiple+Choice+Questions+with+Cases+as+Choices

I am able to pull the case list based on some basic filtering, but my case the list is about 100 alphabetical case names, and the list is not showing up in alphabetic order. Is there a way to sort this list? Maybe a method when I make the instance tag? or maybe something in the itemset tag itself?

For reference, here is the relevant portion of my form. (I have also tried to change the value from @case_id to case_name to see if it was sorting based on the value, and it still comes back in random order)


        <instance id="casedb" src="jr://instance/casedb"></instance>

        <itemset nodeset="instance('casedb')/casedb/case[@case_type='household'][cluster=/data/ClusterID]">
               <label ref="case_name"></label>
               <value ref="@case_id"></value>
</itemset>


I know this isn't a Dimagi-supported feature but was hoping someone could point me in the right direction! Thanks!

P.S. Sorry for the double post... had a typo in the subject line :)

Clayton Sims

unread,
Nov 26, 2014, 3:05:04 PM11/26/14
to commcare-users
Hi!

Unfortunately this is a really tough issue for us to tackle for two reasons. 

A) Our XPath version by definition isn't capable of sorting (and even XPath 2.0 can only sort by awkward and tenuous iterators), so we can't sort at the nodeset level

B) The XForms specifications for itemsets don't specify any indication of how elements in the itemset should be sorted.

I don't remember whether it's a hard or soft expectation that an <itemset> honor the native ordering of the passed in nodeset, but we're open in theory to extending our itemset definition to including a sort element that would permit ordering as long as it doesn't violate the XForms specification.

The other option would be to allow specifying the return sort order in the casedb instance connector, but that's a much bigger lift.

-Clayton

--
You received this message because you are subscribed to the Google Groups "commcare-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to commcare-user...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Caleb Reinking

unread,
Dec 1, 2014, 11:37:22 AM12/1/14
to commcar...@googlegroups.com
Hi Clayton,

Thanks for the informative reply! I have found sorting via vanilla XForm (even 2.0) very difficult and convoluted as well.

When you say 'open in theory to extending our itemset definition to including a sort element that would permit ordering as long as it doesn't violate the XForms specification' could you possibly point me to the location in the code where this change may be made? I am a Django web developer and may be able to begin working on a pull request in some spare time :)

Thanks again,
Caleb

--
You received this message because you are subscribed to a topic in the Google Groups "commcare-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/commcare-users/drduHMxRdm4/unsubscribe.
To unsubscribe from this group and all its topics, send an email to commcare-user...@googlegroups.com.

Clayton Sims

unread,
Dec 9, 2014, 11:45:20 AM12/9/14
to commcare-users
Caleb

There are two places this change could be made theoretically:

1) Within the <itemset> you could provide a namespaced attribute which could optionally be used to provided a Sort Key for model. I'm not 100% sure whether this is spec compliant (the spec may imply that you have to display the items in document order
2) In the instance src="" connector you could in theory provide some information that would affect the actual static model generation. 

These changes would need to be made in the JavaRosa XForms library, our copy of which is hosted here:

-Clayton
Reply all
Reply to author
Forward
0 new messages