Common Repeat Group Error: Multiple Nodes Error
Â
Overview
Often while using Repeat Groups (especially when multiple repeat groups are nested ), users face an issue which states something like this "Cannot convert multiple nodes to a raw value. Refine path expression to match only one node.". This document aims at trying to find the reason for such errors and how to resolve such issues in a repeat group.
Reason for Multiple nodes error
Before understanding the reason for the Multiple Nodes error in Repeat Group, you need to understand what a node means for a repeat group.
What is a Node?
In CommCare, almost everything is an XML structure behind the user interface. Any question you add on a form, any new form, and even an application has an xml structure behind it. An XML structure in any form can consist of a smaller XML structure which is called a node. In simplicity, a form consisting of questions basically means the form's xml node consists of different question nodes. Similarly, the cases in case-db of users are also stored in nodes of cases.
Node in case of Case-DB
Â
The case-db node consists of nodes of cases. So when you use the statement instance('casedb')/casedb/case , you are telling CommCare to refer to all nodes of case-db. When you use instance('casedb')/casedb/case[@case_type = 'mother'][@status = 'open'], you are telling CommCare to refer to all the nodes of case-db whose case type is 'mother' and are still 'open' . Assuming that user has multiple cases of type 'mother' which are open, the output of this statement, instance('casedb')/casedb/case[@case_type = 'mother'][@status = 'open'], is always a set of nodes of cases (or this will result in Multiple Nodes)
Also, when you use instance('casedb')/casedb/case[@case_type = 'mother'][@status = 'open']/case_name, this will result in multiple nodes with just case_name
When you face the issue "Cannot convert multiple nodes to a raw value. Refine path expression to match only one node." in a repeat group, it means that in one of your question's calculations (or any configuration of the question) is expecting a single node but as per the logical statement you are using results in multiple nodes and CommCare is unable to resolve as to which node it should use do process your logical statement. For example, consider an application which registers a mother (case type : mother) and her children (case type : children) . Assuming that you have registered more than 2 mother cases and each mother case has 3 children cases created with a relation of parent-child between these two case types. There is a form which will now show the user the name of mother along with the list of children under each mother. For this, form uses a repeat group to iterate through the 'mother' case type nodes.
Now let's say you wanted to show the details first, before showing the details of the children of her children. For that you added a hidden variable type question to fetch the name of the mother whose case-id is currently in the loop. In that hidden variable's calculation, you added this calculation instance('casedb')/casedb/case[@case_type = 'mother'][@status = 'open']/case_name
Now , when you try to open the form, you will get the error "Cannot convert multiple nodes to a raw value. Refine path expression to match only one node" and the reason is clear that the calculation used for the hidden variable is resolving to a set of nodes of cases and not a single case as shown :
Resolution for Multiple nodes error
In this section, you will try to work on resolving the multiple nodes error which you saw in the last section.
Identification of erroneous calculation
First you need to identify the variable of the form which is throwing you the error.
If you see the error : "Calculation Error: Error in calculation for /data/mother_details/mother_iteration/item/mother_details_section/mother_name XPath nodeset has more than one node …… cannot convert multiple nodes to a raw value. Refine path expression to match only one node."
The highlighted text will guide you the exact location of variable where the error originates :
Fixing the error
Other Reasons
There could be multiple other reasons for Commcare throwing the multiple node error but the core of the reason remains the same i.e. somewhere some question calculation or a question which uses a calculation (which is further used in another question type like a label) is referring to a multiple node as a result of its calculation. In a couple of cases, this error also results from referring to a variable without using a correct reference to a question within a repeat group. If you use easy references with double-nested repeat groups the app can get confused pretty fast.
Troubleshooting
The first step in troubleshooting things like this is
Identify the question/calculation which is throwing the error.
Check for calculation conditions/display conditions/lookup-filters/default value of that question
If these calculations are based on an easy reference to another question, then it could be a possible issue. Usually removing any easy references and replacing them with relative paths to the question will fix the issue for you. Using easy reference is safe when a form is straight-forward and doesn't have complexities of nested repeat groups. But if the form has nested repeat groups, the safest bet is to use relative referencing in a repeat group.
Note: Most common source for this type of error is due to usage of easy reference in a lookup-table type question's filters OR a Calculation Condition involving reference to fixture data like locations or lookup-tables with filtering condition using easy referencing.Â
Using current() expression with the correct referencing path is the safe way to avoid any multiple node error in repeat groups. The details of using current() expression is covered here