book
Article ID: KB0090487
calendar_today
Updated On:
Description
Resolution:
When you want to search in a string for a substring X, and replace X by another string Y, when there might be multiple occurrences of X in the string, you can use the XPath formula introduced in this example. If the string X and Y are both single characters, you can use the translate() function. This example is more suited to multi-character strings.
If the string X is single character, the Xpath formula to do find and replace would be :
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
tib:concat-sequence-format(tib:tokenize-allow-empty(<originalstring>, X), <replacewithstring>, 0)
2nd argument of tib:tokenize-allow-empty ,provide the string to be found in the original string.
2nd argument of tib:concat-sequence-format ,provide the replace with string.
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
If both X and Y are multi-character strings, for each, you want to find all "foo" in the original string and replace them by "bar", using the following Xpath formula:
tib:concat-sequence(for $var in tib:tokenize(<originalstring>, "f") return if (tib:left($var, 2)="oo") then concat("bar", substring-after($var, "oo")) else concat("f", $var))
We can build the above XPath formula in 3 steps:
a. tokenize the input XML string, using "f" as the delimiter: tib:tokenize(<originalstring>, "f")
b. with XPath functions "for $var in << set >> return << value >>" and "if (<< test >>) then << then >> else << else >>", process each output string depending on whether it starts with "oo" or anything else: for $var in for $var in tib:tokenize(<originalstring>, "f") return if (tib:left($var, 2)="oo") then concat("bar", substring-after($var, "oo")) else concat("f", $var)
c. concatenate the output strings.
Please note that the above formula works only if the 1st character of <originalstring> is "f", otherwise you add "f" to the original string. To get around that, you use substring-after function to pass the substring since the 1st "f" as <originalstring> in the above formula
>>>>>>>>>>>>>>>>>>>>>>>
KEYWORDS:STRING,FIND,REPLACE,XPATH,FORMULA
Issue/Introduction
If you want to find and replace a string in XPATH1.0, you can use translate() for single character find and replace. Formulticharacter find and replace,you can use these steps.