# Add XPath translate() function

**URL:** <https://forum.getodk.org/t/add-xpath-translate-function/17017>\
**Category:** Ideas\
**Created:** [January 1, 2019, 9:47pm UTC](https://forum.getodk.org/t/add-xpath-translate-function/17017 "2019-01-01T21:47:06Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Xiphware](https://getodk.b-cdn.net/user_avatar/forum.getodk.org/xiphware/32/2587_2.png) [@Xiphware](https://forum.getodk.org/u/Xiphware)\
**Post date:** [January 1, 2019, 9:47pm UTC](https://forum.getodk.org/t/add-xpath-translate-function/17017/1 "2019-01-01T21:47:06Z")

</div>

**What is the general goal of the feature?**  
Add ability to perform character substitution, and removal, in a string; eg convert comma separated list to space separated: "1,2,345" --\> "1 2 345".

**What are some example use cases for this feature?**  
The existing string manipulation functions in ODK support searching for substrings (eg `contains()`, `starts-with()`, `ends-with()`), or extracting portions of a string (eg `substr()`), but do not readily allow reformatting the text in a string; for example changing a delimiter character. The XPath `translate()` function should be a useful addition to the suite of ODK string manipulation functions. For example, changing a _comma_ delimited string list to a _space_, can enable the list to be susceptible to processing using `selected-at()`, `selected()`, and `count-selected()`. The `translate()` function also enables such things converting strings from lowercase to uppercase, by `translate(${mystring}, 'abcde...', 'ABCDE...')`, and removing specific characters from a string (by replacing them with nothing).

The [translate()](https://www.w3.org/TR/1999/REC-xpath-19991116/#function-translate) XPath function is well-defined and already supported by Enketo and libxml2. So this would bring javaRosa into better parity wrt other XForm/XPath processors.

**What can you contribute to making this feature a reality?**  
I have already implemented and tested for javaRosa. I can open a PR if adding this new feature is approved.

---

<div class="post-metadata">

**Author:** ![martijnr](https://getodk.b-cdn.net/user_avatar/forum.getodk.org/martijnr/32/1614_2.png) [@martijnr](https://forum.getodk.org/u/martijnr)\
**Post date:** [January 2, 2019, 10:19pm UTC](https://forum.getodk.org/t/add-xpath-translate-function/17017/2 "2019-01-02T22:19:54Z")

</div>

I'm in favour of this native XPath 1.0 function. Thanks for this proposal!

---

<div class="post-metadata">

**Author:** ![Xiphware](https://getodk.b-cdn.net/user_avatar/forum.getodk.org/xiphware/32/2587_2.png) [@Xiphware](https://forum.getodk.org/u/Xiphware)\
**Post date:** [January 3, 2019, 12:15am UTC](https://forum.getodk.org/t/add-xpath-translate-function/17017/3 "2019-01-03T00:15:38Z")

</div>

Finished code and unit test. PR opened.

> <https://github.com/getodk/javarosa/pull/395>
>
> Closes # https://forum.opendatakit.org/t/add-xpath-translate-function/17017
> 
> \#…### What has been done to verify that this works as intended?
> 
> tested under Android Studio simulator running ODK Collect v1.18.0 (dirty), using test examples described on http://www.xsltfunctions.com/xsl/fn\_translate.html (and those added to testEval testcases), and compared results running same against Enketo.
> 
> \#### Why is this the best possible solution? Were any other approaches considered?
> 
> Implements XPath standardized function (which is arguably best solution). Implements identical XPath behavior as exists already in Enketo and libxml2.
> 
> BTW, I also tried an implementation that avoided using a StringBuilder, and instead tried to exploit existing java String replace()/replaceAll() functions, but this ended up much messier, required multiple re-instantiations of String objects, and ultimately did not handle simultaneous interchanging translations correctly (eg 'x' --\> 'y' and 'y' --\> 'x').
> 
> \#### Are there any risks to merging this code? If so, what are they?
> 
> Introduces entirely new XPath function and does not touch or modify any existing javaRosa function or behavior. Can only be used by new forms that exploit this new function in their XPath expressions, so no risk to existing forms.

Implements the XPath spec definition:

> **Function: string translate(string, string, string)**
> 
> The translate function returns the first argument string with occurrences of characters in the second argument string replaced by the character at the corresponding position in the third argument string. For example, translate("bar","abc","ABC") returns the string BAr. If there is a character in the second argument string with no character at a corresponding position in the third argument string (because the second argument string is longer than the third argument string), then occurrences of that character in the first argument string are removed. For example, translate("--aaa--","abc-","ABC") returns "AAA". If a character occurs more than once in the second argument string, then the first occurrence determines the replacement character. If the third argument string is longer than the second argument string, then excess characters are ignored.

Tested using this simple form:

[translate.xls](https://getodk.b-cdn.net/uploads/default/original/2X/c/c0cecdbd97646ec72f13f90387ddb1c62bd42e47.xls) (5.5 KB)  
[translate.xml](https://getodk.b-cdn.net/uploads/default/original/2X/1/104bc1a8fdc085f3686c2c6553f9d8367137bdc3.xml) (2.0 KB)

Note: Enketo already supports this XPath function so - if you can avoid Validate (eg KoboToolbox) - you can already try out this form with the new function under Enketo.

---

<div class="post-metadata">

**Author:** ![Xiphware](https://getodk.b-cdn.net/user_avatar/forum.getodk.org/xiphware/32/2587_2.png) [@Xiphware](https://forum.getodk.org/u/Xiphware)\
**Post date:** [January 16, 2019, 8:15am UTC](https://forum.getodk.org/t/add-xpath-translate-function/17017/4 "2019-01-16T08:15:44Z")

</div>

Closing out this feature request as the javaRosa PR has been merged (thanks @ggalmazor). So this new XPath function should become available in a subsequent ODK Collect (and Validate) build. In the mean time it works presently under Enketo if you want to play around with the example forms.

I'll submit a corresponding PR to update the docs for this, and substring-before() and substring-after(), shortly.

---

<div class="post-metadata">

**Author:** ![Xiphware](https://getodk.b-cdn.net/user_avatar/forum.getodk.org/xiphware/32/2587_2.png) [@Xiphware](https://forum.getodk.org/u/Xiphware)\
**Post date:** [January 16, 2019, 8:16am UTC](https://forum.getodk.org/t/add-xpath-translate-function/17017/5 "2019-01-16T08:16:07Z")

</div>

Closed on January 16, 2019, 8:16am UTC


