---
title: "Match element in list of species sampled"
url: "https://openforis.support/questions/3335/match-element-in-list-of-species-sampled"
tags: ["collect"]
status: "answered"
score: 1
answer_count: 4
asked: "2022-03-16T10:42:33"
last_activity: "2023-03-28T12:35:47"
source: "Open Foris Support - Q&A Forum"
license: "Copyright Open Foris, 2026. Some rights reserved under creative commons license."
---

# Match element in list of species sampled

Asked by janklein on 2022-03-16

Hello again,

In my survey, I repeatedly sample the same taxa. Hence, in order not to enter all information over and over again, I would like Open Foris to recognise whether a taxon had already been sampled within the sampling unit. 

So far, I unsuccessfully tried with the following code which is to be added into the "only when expression is verified" field:
true(idm:contains(parent()/a_in_ind/taxon,taxon))

Am I getting it wrong?

Thanks for the help in advance,
Jan


## Answer (score 1, by Stefano (OF) on 2022-03-16)

Link: https://openforis.support/questions/3335/match-element-in-list-of-species-sampled/3337

Dear Jan,  
First of all, when you are working with complex expressions using a composite attribute, like taxon, that has multiple fields, it would be better to extract the code value into a calculated attribute, that you could call `species_code`, with an expression like this:  

    species/@code 

Then you could verify, into another calculated attribute, hidden in data entry, that the same species has been already sampled in another calculated attribute (e.g. `species_already_sampled`) with an expression like this:  

    count(parent()/individuals[species_code = $context/species_code]) > 1  

that means that there is at least one row in your table having the same species code of the current row. The expression `$context/species_code` is a shortcut for `$this/parent()/species_code`.  
After that, you can use the `species_already_sampled` value into a Relevant when condition, to make a field relevant and trigger the apply of the default value expression on it.  

Let us know if this will help you to solve your problem.  
Many thanks,  
Open Foris Team


## Answer (score 0, by janklein on 2022-03-17)

Link: https://openforis.support/questions/3335/match-element-in-list-of-species-sampled/3339

Dear Stefano,

thank you very much for your quick reply. The workaround works fine. 

Just one last question: How can I use species_already_sampled as a condition? It seems like it outputs a boolean (true/false). When trying to code "true(species_already_sampled)", OF runs into an error. I also tried "species_already_sampled = 'true'", but the code doesn't work.

Thanks again,

Jan


## Answer (score 0, by Stefano (OF) on 2022-03-17)

Link: https://openforis.support/questions/3335/match-element-in-list-of-species-sampled/3340

Dear Jan,  
You can use write just `species_already_sampled` in the Relevant when expression, to make an attribute relevant in that case and apply a default value on it, for example. You don't need to cast the result of the expression into a boolean value, since it's already boolean.  
Many thanks,  
Open Foris Team


## Answer (score 0, by janklein on 2023-03-28)

Link: https://openforis.support/questions/3335/match-element-in-list-of-species-sampled/3593

Hello again,

After having implemented the workaround described above it appears to be working for the first but not for the following subplot entities.

Each plot is subdivided into 8 entities (sub-plots). Each sub-plot contains the following three attributes relevant to the question:
 - **taxon**: taxa from species list
 - **taxon.code**: taxon/@code as integer
 - **taxon.already.sampled**: text attribute calculated based on the following expression: 

`string(count(parent()/subplot1[taxon.code = $context/taxon.code]) > 1)`

In the first entity (subplot 1), the field taxon.already.sampled correctly recognises when a taxon has already been sampled (true) and when it has not yet been sampled (false).

The problem evolves in the second (and the following) subplot. We would like OF to recognise if a taxon had already been sampled in any subplot (not only the current one). Thus, I added a second term to incorporate the former entities in the attribute taxon.already.sampled, resulting in the following expression: 

`string(count(parent()/subplot1[taxon.code = $context/taxon.code]) > 1) or string(count(parent()/subplot2[taxon.code = $context/taxon.code]) > 1)`

This code, though, does not work correctly. The field taxon.already.sampled remains ‘true’ for all newly added taxa, regardless of whether or not they had already been sampled within the current or a former subplot. The validation rules based on this attribute are thus not correctly warning the user if a specimen needs to be collected or not.

It seems that the expression in subplot2 fails to extract from subplot1.

Which expression should I use in the taxon.already.sampled field to check for correspondences within all subplot entities?

Thank you very much in advance,
Jan
