我是xquery的新手,并在此寻找您的快速帮助。非常感谢您查看我的问题。
我有一个要求,我需要用给定字符串中的实际值替换多个占位符,我尝试使用XQuery函数下面的实现。
此功能的问题在于,每次获取单独的字符串时,它都会替换所有占位符,例如首先执行for循环替换第一个占位符,第二个迭代替换第二个占位符(但是实际输入字符串),我正在寻找一个解决方案,我可以将第一次迭代传递给第二次迭代的输入。
declare function functx:replace-place-holders-with-values (
$MainString as xs:string? ,
$MappingList as node(),
$ActualValueList as node() ) as xs:string*
{
let $MessageList:=
for $BalMapping at $counter
in $MappingList//map:GetUSSDBalanceMappingRow
let $BalanceBucket := $ActualValueList//bal:BalanceItems
/bal:BalanceItem/bal:SubBalanceList
/bal:SubBalanceItem
[bal:SubBalanceType/text() =
$BalMapping//map:BalanceParamaterFlag/text()]
return <MessageList>
{
replace($BalanceString ,
($BalMapping/map:BalanceParameter/text()),
concat(
$BalanceBucket/bal:SubBalanceValue/text(),
' ',
$BalanceBucket/bal:SubBalanceUnit/text()))
}
</MessageList>
return $MessageList/text()
};
e.g。
Your Total Balance is #BAL#, Saving Bal #SAV_BAL#, FD Balance #FD_BAL#, for more details loging to example.com
Your Total Balance is 205, Saving Bal #SAV_BAL#, FD Balance #FD_BAL#, for more details loging to example.com
Your Total Balance is #BAL#, Saving Bal 105, FD Balance #FD_BAL#, for more details loging to example.com
Your Total Balance is #BAL#, Saving Bal #SAV_BAL#, FD Balance 100, for more details loging to example.com
Your Total Balance is 205, Saving Bal 105, FD Balance 100, for more details loging to example.com
我很乐意提供更多详细信息,如果您需要更多详细信息,请与我们联系。 如果您有任何其他解决方案可以使用XQuery / XSLT实现此目的,请随意忽略上述查询。
期待很快收到回复。
答案 0 :(得分:0)
您需要使用递归。像这样:
declare function local:r($in s xs:string, $markers as xs:string*, $replacements as xs:string*) {
if (empty($markers))
then $in
else local:r(replace($in, head($markers), head($replacements)),
tail($markers), tail($replacements))
}
(头部和尾部是XQ30函数,在XQ10中自己实现为$ seq [1]并分别删除($ seq,1))