# Validate against duplicate entries within nested repeats

**URL:** <https://forum.getodk.org/t/validate-against-duplicate-entries-within-nested-repeats/5840>\
**Category:** Development\
**Created:** [August 16, 2016, 1:57pm UTC](https://forum.getodk.org/t/validate-against-duplicate-entries-within-nested-repeats/5840 "2016-08-16T13:57:40Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![Mitch\_S](https://getodk.b-cdn.net/letter_avatar_proxy/v4/letter/m/3ec8ea/32.png) [@Mitch\_S](https://forum.getodk.org/u/Mitch_S)\
**Post date:** [August 16, 2016, 6:23pm UTC](https://forum.getodk.org/t/validate-against-duplicate-entries-within-nested-repeats/5840/2 "2016-08-16T18:23:12Z")

</div>

This question is more appropriate for the opendatakit@ list. Adding that  
list.

opendatakit-developers@ is for people modifying the software, not those  
writing forms and using the tools.

Here's an XLS file and an XML file.

The XML file has been generated from the XLS file then manually edited to  
use the names of the chosen crop and plot in the yield question.

The plot and crop are asked for on the same screen. I would generally  
recommend this since the constraint is applied on forward-swipe off of a  
screen. If you ask these questions on different screens, you might get odd  
behaviors.

The technique is to use the selected() predicate to detect whether an  
already-entered value matches the current crop\_type field's answer. If it  
does, the constraint is violated:

not(selected(/\* accumulation of already-entered values \*/, .))

multiple-select responses are just space-separated lists of values. We can  
construct such a list using the join command:

join(' ', /\* already-entered-values \*/)

giving us this constraint:

not(selected(join(' ', /\* already-entered-values \*/ ), .))

To get the already-entered values, you need a complicated XPath expression.  
See

> **[ODK Forms](https://docs.getodk.org/form-design-intro/)**
>
> Forms provide a structured way to collect and provide information. Typically forms are used by data collectors (also called enumerators) with the Collect app who ask questions of and share informat...

In this case, we are referencing the values within the existing filled-in  
form, so we want to refer to the crop\_type values in our form. Those have  
this path (the first element in the path is the filename of your .xls file):

/OnlyOneOfSet/plot/plot\_info/crop\_type

But if we just used this, we would get the current answer PLUS the answers  
for all choices of plot code (Plot A, Plot B, etc.).

We need to filter which repeat groups we include to construct the set of  
all crop\_type values that we care about.

To do that, we apply a filtering constraint on the repeat group:

/OnlyOneOfSet/plot[ /\* filtering constraint to select applicable repeats  
goes here \*/ ]/plot\_info/crop\_type

And the filtering constraint is evaluated where '.' refers to the  
currently-under-consideration plot repeat, and we need to use current()/ to  
refer to the current crop\_type value.

With that syntax:

current()/  
== crop\_type field currently being verified

current()/../  
== plot\_info field-list group containing that field

current()/../plot\_code  
== the plot code (Plot A, Plot B, etc.) corresponding to the crop\_type  
field currently being verified.

current()/../..  
== plot repeat instance of the crop\_type field currently being verified.

So we have two parts to the constraint:

./plot\_info/plot\_code=current()/../plot\_code  
== select repeats with plot\_code choices matching the plot\_code of the  
current repeat.

position(.) != position(current()/../..)  
== omit the current repeat from consideration.

[OnlyOneOfSet.xml](https://getodk.b-cdn.net/uploads/default/original/1X/c4db2ca41f26f7754b97455ba5a84871425b86e0.xml) (3.4 KB)

[OnlyOneOfSet.xls](https://getodk.b-cdn.net/uploads/default/original/1X/5bedb7e5a94b0a78f84c2c25ea5897074013fd6e.xls) (30 KB)

> **···**
>
> On Tue, Aug 16, 2016 at 6:57 AM, wrote:
> 
> > Hi everyone,
> > 
> > I have the following scenario:
> > 
> > I'm collecting data for a household, and each household can have a maximum  
> > of say, 3 plots of land. And for each of these three, they can grow a  
> > maximum of 2 crop types. So I want to collect data on crop yields for each  
> > plot and it's important to prevent a situation whereby a user enters a  
> > given crop more than once for a given plot, i.e, this is valid:
> > 
> > Plot A  
> > Crop Maize
> > 
> > Plot A  
> > Crop Beans
> > 
> > Plot B  
> > Crop Maize
> > 
> > Plot B  
> > Crop Beans
> > 
> > but not
> > 
> > Plot A  
> > Crop Maize
> > 
> > Plot A  
> > Crop Maize \<--- duplication
> > 
> > To achieve this kind of data collection I am using nested repeats of  
> > questions...can anyone advise on what kind of calculation/constraint would  
> > help me achieve the above validation? It seems as though I'd need to use  
> > loop logic to check through the values of the repeats and halt/flag on  
> > encountering a repeat but I can't imagine how I'd do that with the XLSX  
> > form?
> > 
> > Thanks in advance!
> > 
> > Hussein
> > 
> > --  
> > You received this message because you are subscribed to the Google Groups  
> > "ODK Developers" group.  
> > To unsubscribe from this group and stop receiving emails from it, send an  
> > email to [opendatakit-developers+unsubscribe@googlegroups.com](mailto:opendatakit-developers+unsubscribe@googlegroups.com).  
> > For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).
> 
> --  
> Mitch Sundt  
> Software Engineer  
> University of Washington  
> [mitchellsundt@gmail.com](mailto:mitchellsundt@gmail.com)

---

_[View the full topic](https://forum.getodk.org/t/validate-against-duplicate-entries-within-nested-repeats/5840)._
