In this example, I have a string $text and would like to call fn:replace on it four times to get the result "zyxw".
xquery version "1.0-ml";
let $text := "abcd"
let $patterns := ("a", "b", "c", "d")
let $replaces := ("z", "y", "x", "w")
for $i in (1 to fn:count($patterns))
let $p := $patterns[$i]
let $r := $replaces[$i]
let $text := fn:replace($text, $p, $r)
return $text
Instead, I get the sequence:
("zbcd", "aycd", "abxd", "abcw")
I get why that's happening. I'm just not sure how to accomplish what I'm trying to do in XQuery. The JavaScript equivalent of what I want is this:
'use strict';
let text = "abcd";
let patterns = ["a", "b", "c", "d"];
let replaces = ["z", "y", "x", "w"];
for(let i = 0, len = patterns.length; i < len; ++i){
text = fn.replace(text, patterns[i], replaces[i]);
}
text