Custom XPath function for BusinessWorks to concatenate N times
Facts - Tibco
Thursday, 19 January 2012 20:47

This note has been tested with java 6 and Tibco BusinessWorks 5.8.

Suppose the field value is xyz. And suppose you have to concatenate this field three times with delimiter "|", then the desired output is "xyz|xyz|xyz".

For this kind of mapping you have to make a list from a single field. In the example above the field value "xyz" should first be transformed to a list {"xyz", "xyz", "xyz"}. Then the list can be concatenated in the mapper using XPath. Unfortunately in BW it is not possible to create a sequence from a single field using just XPath and/or the BW mapper. Instead you can create the list by putting a mapping task in an iteration group iterating N times and use the option "Accumulate Output". So the accumulated output of the iteration group contains the list. Once you have the list you can use the following tibco/XPath mapping to create the desired output:

tib:concat-sequence-format($list, "|", false())

Note that concat-sequence-format is not standard XPath. This is an XPath function added by Tibco.

Creating the list with an iteration group is a rather ugly and inefficient solution for such a simple mapping. An alternative way to accomplish this mapping is by using a custom XPath function. See the java code below.

package custom.xpath;
 
public class StringFunctions {
 
  public StringFunctions() {}
 
  public static String concatNtimes(int n, String token, String delim)
  {
    String result = "";
    for (int i=0; i<n; i++) {
      result += token +delim;
    }
    if (n>0) result = result.substring(0, result.length() -delim.length());
    return result;
  }
 
  public static final String[][] HELP_STRINGS = {
    {
    "concatNtimes",
    "Concatenate the input string N times separated by a delimiter",
    "N=3, input=\"xyx\", delimiter=\"|\": concatNtimes(3, \"xyz\", \"|\")", 
    "xyz|xyz|xyz"
    }
  };
 
  public static void main(String args[])
  {
    System.out.println(StringFunctions.concatNtimes(3, "xyz", "|"));
  }
}