module namespace uoa = "http://www.tempurl.co.nz/xquery";
declare function uoa:convertString($inputString as xs:string, $translationSet as element()*) as xs:string {
if (count($translationSet) = 0) then (: base case :)
$inputString
else (: recursion :)
let $fromValue := data($translationSet[1]/translate_from)
let $toValue := data($translationSet[1]/translate_to)
let $updatedStr := fn:replace($inputString,$fromValue,$toValue)
return uoa:convertString($updatedStr,remove($translationSet,1))
};
Declaring a translation set can be done with an XML file like translation.xml:
A BB C DD
Then invoking this can be achieved with the following xquery:
import module namespace uoa="http://www.tempurl.co.nz/xquery" at "uoa.xq";
let $inputString := "AABBCCDD"
let $translationSet := doc("translation.xml")/translation_set/translation
let $outputString := uoa:convertString($inputString,$translationSet)
return
{$inputString}
This takes the input string and recursively iterates through the translation set (note that xquery is very functional and immutable variables mean that loops won't work!).
No comments:
Post a Comment