Skip to content

Make xpath date parsing consistent for error cases #879

Description

@garethbowen

Expected Behaviour

When calling an xpath function that takes a date, there are multiple ways the input can be invalid, but the value returned should be consistent regardless of exactly how the string is invalid.

Observed Behaviour

Calling format-date sometimes throws an error and sometimes returns null. I think the correct approach is to throw in all cases, but we need to make sure this isn't going to do anything unexpected. This also impacts all other functions that take a date string.

Reproduction Steps

Form (XML or XLSX file):

<?xml version="1.0"?>
<h:html xmlns="http://www.w3.org/2002/xforms" xmlns:h="http://www.w3.org/1999/xhtml" xmlns:ev="http://www.w3.org/2001/xml-events" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:jr="http://openrosa.org/javarosa" xmlns:orx="http://openrosa.org/xforms" xmlns:odk="http://www.opendatakit.org/xforms">
    <h:head>
        <h:title>date formatting</h:title>
        <model odk:xforms-version="1.0.0">
            <instance>
                <data id="date_formatting" version="1">
                    <date/>
                    <meta>
                        <instanceID/>
                    </meta>
                </data>
            </instance>
            <bind nodeset="/data/date" type="string"/>
            <setvalue ref="/data/date" event="odk-instance-first-load" value="format-date('2026-12-12', '%Y-%b-%e')"/>
            <bind nodeset="/data/meta/instanceID" type="string" readonly="true()" jr:preload="uid"/>
        </model>
    </h:head>
    <h:body>
        <input ref="/data/date">
            <label>Date</label>
        </input>
    </h:body>
</h:html>

Steps:

  1. The form above works fine - notice the field is set to "2026-Dec-12"
  2. Change the first parameter to the format-date function to '2026-13-12' which has an invalid month - notice an error is thrown on load
  3. Change the first parameter to the format-date function to '26-12-12' which isn't a recognised format - notice the value is set to blank

Additional Context

Found during patching of #877

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

Projects

Status
In Progress

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions