Search code examples
openrefine

forNonBlank function in OpenRefine


I get an error when using forNonBlank in OpenRefine's Templating Export feature.

I have cells with multiple subjects that I want to capture in separate dcterms:subject xml elements. Example: Geology--Alberta--Coal Valley. // Geology, Structural. // Geology, Stratigraphic--Cretaceous.

I am using OpenRefine's Templating Export option to export to XML, similarly to the process described here.

This expression works fine:

{{forEach(cells["dcterms:subject"].value.split(" // "), v, "<dcterms:subject>" + v + "</dcterms:subject>\n")}}

I get:

<dcterms:subject>Geology--Alberta--Coal Valley.</dcterms:subject> <dcterms:subject>Geology, Structural.</dcterms:subject> <dcterms:subject>Geology, Stratigraphic--Cretaceous.</dcterms:subject>

But when using forNonBlank as in:

{{forNonBlank(cells["dcterms:subject"].value.split(" // "), v, "<dcterms:subject>" + v + "</dcterms:subject>\n", "")}}

I get:

<dcterms:subject>[Ljava.lang.String;@16657412</dcterms:subject>

Is there something wrong with my coding, or is this a bug?

Thanks for your help.


Solution

  • forNonBlank isn't an iterative function, so the function:

    forNonBlank(cells["dcterms:subject"].value.split(" // "), v, "" + v + "\n", "")

    Evaluates the array created through the split as to whether it is blank or not (the whole array, not each item in the array) and finding that it is not blank assigns the array to variable 'v'.

    Essentially 'forNonBlank' is doing something similar to combining 'if' and 'isNonBlank', not 'forEach' and 'isNonBlank'

    You've got several options for doing what you want, but you need to have an iterator in there somewhere. For example:

    forEach(cells["dcterms:subject"].value.split(" // "),v,forNonBlank(v,w, "" + w + "", "")).join("/n")