---
title: "XPath validations,expressions, default values and checks...need help!"
url: "https://openforis.support/questions/526/xpath-validationsexpressions-default-values-and-checksneed-help"
tags: ["collect", "collectmobile"]
status: "answered"
score: 0
answer_count: 2
asked: "2016-05-11T20:24:25"
last_activity: "2016-05-12T11:51:14"
source: "Open Foris Support - Q&A Forum"
license: "Copyright Open Foris, 2026. Some rights reserved under creative commons license."
---

# XPath validations,expressions, default values and checks...need help!

Asked by collector on 2016-05-11

Dear all,

Working hard in the Collect Survey Designer ( found a v[ideo to design Collect Earth surveys in youtube][1] ) and I am trying to design a couple of new surveys.

I have managed to get a few **XPath** validation expressions working but I must admit I am a bit lost... could anyone point me in the right direction of what should I learn, or what are kind of general XPath functions one can use???

**Validations, default values and the possibility to show/hide attributes** in the interface depending on other values is a strong feature, but documentation is weak...

Thanks!!


  [1]: https://www.youtube.com/watch?v=j2ruGSzJg0Y


## Answer (score 2, by javier on 2016-05-12)

Link: https://openforis.support/questions/526/xpath-validationsexpressions-default-values-and-checksneed-help/529

Hi...I have written a document with some XPath examples, based on two inheritance hierarchies among the entities: one more complex (cluster->plot) and one simpler (only plot). See below:

Link to document : [HERE][1] 

**EXAMPLES OF CODE IN OPEN FORIS Collect**

**General hierarchical structures (either cluster or plot-based)**

Comparison checks (i.e., start_date, start_time, end_time, tree_no, azimuth, tree distance,dbh, total_height and bole_height):

Example attribute (number type): Path: /plot/details/elevation

Severity		Error

Message		Elevation must be between 0
and 4000 m 0	<=	elevation		<=	4000

Example attribute (date type): Path: /cluster/start_date 

Severity		Error

Message		You have written the starting date in the future!

start_date	<=	idm:currentDate()

Example attribute (time type): Path: /cluster/start_time

Severity		Warning

Message		Working hours for the survey should be from 06:00 to 19:00!

0600	<	start_time	<=	1900

Example attribute (time type): Path: /cluster/end_time

Severity		Error

Message		End time should be always after start time

start_time	<=	end_time	 

(Here we assume that start_time and end_time are under the same entity, i.e., cluster, or plot)

Example attribute (number type): Path: /cluster/plot/tree/tree_no

Severity		Error

Message		tree number must be >0

0	<	tree_no	

**Example attribute (number type): Path: /cluster/plot/tree/azimuth**

Severity		Error

Message		angle must be between 0 (inclusive) and 360 (exclusive)

0	<=	azimuth	<	360

**Example attribute (number type, refers to tree distance): Path: /cluster/plot/tree/distance**

Severity		Error

Message		tree distance must be positive

0	<=	distance

Severity		Error

Message		tree outside of plot. Plot radius is 12 m.

distance	<=	12	

**Example attribute (number type): Path: /cluster/plot/tree/dbh**

Severity		Warning

Message		Unusually large dbh

dbh	<	200

Severity		Error

Message		dbh must be positive

0	<=	dbh

Severity		Error

Message		tree must be at least 20 cm if distance>2 m

20	<=	dbh	Apply when	distance>2

Severity		Error

Message		tree must be at least 5 cm in any case

5	<=	dbh	


**Example attribute (number type): Path: /cluster/plot/tree/total_height**

Severity		Warning

Message		Unusually tall tree

total_height	<	80

Severity		Error

Message		Impossibly tall tree

total_height	<	150

Severity		Error

Message		height must be positive

0	<=	total_height


**Example attribute (number type): Path: /cluster/plot/tree/bole_height**

Severity		Error

Message		bole height must be positive

0	<=	bole_height

Severity		Error

Message		bole height must be less or equal than total height

bole_height	<=	total_height

**Default values (i.e., start_date, start_time and tree_no):**

Example attribute (date type): Path: /cluster/start_date

Calculated value expressions:		Expression:	idm:currentDate()

**Example attribute (time type): Path: /cluster/start_time**

Calculated value expressions:		Expression:	idm:currentTime()

Example attribute (number type): Path: /plot/tree/tree_id

**Calculated value expressions**:		
Expression:	math:max(parent()/tree/tree_id) + 1 

(it will automatically increase the number every time a new tree is recorded in the plot)
Surveys with Plot as sampling unit or root entity and tree as sub-entity

**Distance checks (i.e., plot position):**

Example attribute: Path: /plot/details/position

Severity		Error

Message		You are more than 50 m from plot location

Destination point	idm:samplingPointCoordinate(parent()/plot_id)

Max distance (m)	50

Severity		Warning

Message		You are more than 20 m from plot location


**Destination point**	

idm:samplingPointCoordinate( parent()/plot_id)

Max distance (m)	20

**Autocompletion** (click Calculated in the General tab fot the variable) in optional variables from sample design file (i.e., province as a column after design point coordinates in the sampling_design imported file):

Example attribute (text type): Path: /plot/details/province

Calculated value expressions:		
Expression:	idm:samplingPointData('province', parent()/plot_id)

Requirednes of variable if expression is satisfied (i.e., tree_height only required every three third tree):

Example attribute (text type): Path: /plot/tree/tree_height
Required only when expression is verified:	tree_id mod 3 = 0


**Surveys with Cluster as sampling unit or root entity and plot and tree as sub-entities** 

**Distance checks (i.e., plot position):**

Example attribute: Path: /cluster/plot/details/position
Severity		Error
Message		You are more than 100 m from plot location
Destination point	idm:samplingPointCoordinate(parent()/parent()/cluster_no, parent()/plot_no)
Max distance (m)	100
Severity		Warning
Message		You are more than 20 m from plot location
Destination point	idm:samplingPointCoordinate(parent()/parent()/cluster_no, parent()/plot_no)
Max distance (m)	20

**Autocompletion(click Calculated in the General tab fot the variable)**  in optional variables from sample design file (i.e., province as a column after design point coordinates in the sampling_design imported file):

Example attribute (text type): Path: /cluster/location/province

Calculated value expressions:		
Expression:	idm:samplingPointData('province', parent()/cluster_no)

Example attribute (text type): Path: /cluster/plot/details/province

Calculated value expressions:		
Expression:	idm:samplingPointData('province', parent()/plot_no)
Autocompletion (click Calculated in the General tab fot the variable) in tree-related variable (i.e., tree count):

Example attribute (number type): Path: /cluster/plot/tree_count

Calculated value expressions:		Expression:	math:max(parent()/plot/tree/tree_no)

**Relevance of variable if expression is satisfied** (i.e., tree_count, plot position, and tree entity):

Example attribute (number type): Path: /cluster/plot/tree_count

Relevant only when expression is verified:	parent()/plot/details/accessibility=0

Example attribute (coordinates type): Path: /cluster/plot/details/position

Relevant only when expression is verified:	accessibility=0 
(here we don’t need a hierarchical path since position and accessibility belong to the same hierarchy, i.e: parent()/plot/details/position and parent()/plot/details/accessibility)

Example entity (multiple table layout): Path: /cluster/plot/tree

Relevant only when expression is verified:	details/accessibility=0


  [1]: https://www.dropbox.com/s/554wqn6o3ztm24h/EXAMPLES%20OF%20CODE%20IN%20OPEN%20FORIS%20Collect.docx?dl=0


## Answer (score 1, by collectearth on 2016-05-11)

Link: https://openforis.support/questions/526/xpath-validationsexpressions-default-values-and-checksneed-help/528

This is a common question, as the XPath is a language that can be a bit complex for non-programmers, but don't panic, it is really not that hard (and actually quite logical)

You can start by looking into the Collect documentation here : 

[Open Foris Wiki : XPath documentation][1] 

[W3Schools also has some more in-depth tutorial in XPath][2]


  [1]: http://www.openforis.org/OFwiki/index.php/Inventory_Data_Metamodel
  [2]: http://www.w3schools.com/xsl/xpath_intro.asp
