[英]Marklogic - How to assign dynamic variable in Xquery
I have tried below mentioned XQuery . 我已经在下面尝试过XQuery了。
declare variable $path as xs:string :="D:\Mongo\";
let $uri :="/MJ/1932/Vol1/Part1/387.xml"
let $x := fn:normalize-space(fn:replace($uri,"/"," "))
for $i in fn:tokenize($x, " ")
let $j := fn:concat($path,$i)
return($j)
Actual output 实际产量
D:\Mongo\MJ
D:\Mongo\1932
D:\Mongo\Vol1
D:\Mongo\Part1
D:\Mongo\387.xml
Expected output 预期产量
D:\Mongo\MJ
D:\Mongo\MJ\1932
D:\Mongo\MJ\1932\Vol1
D:\Mongo\MJ\1932\Vol1\Part1
D:\Mongo\MJ\1932\Vol1\Part1\387.xml
Please Suggest me , how to change the dynamically variable value. 请建议我,如何更改动态变量值。
XQuery is a functional programming language, which implies variables are immutable. XQuery是一种功能编程语言,它暗示变量是不可变的。 You cannot simply increment or append to a defined variable.
您不能简单地增加或追加到已定义的变量。 Typically, a recursive function is used instead to construct the result.
通常,使用递归函数代替构造结果。
This examples (there are more concise ones, I wanted to keep the individual parts split apart and simple to understand) recursively creates the path, appending another level each time executed. 该示例(有一些更简洁的示例,我想让各个部分分开并易于理解)以递归方式创建路径,每次执行时都附加一个级别。 The
$path
prefix is appended separately to not mix up the different tasks. $path
前缀是单独添加的,以免混淆不同的任务。
declare variable $path as xs:string :="D:\Mongo\";
declare variable $uri as xs:string := "/MJ/1932/Vol1/Part1/387.xml";
declare function local:add-path($parts as xs:string*) as xs:string* {
let $head := $parts[1]
let $tail := $parts[position() > 1]
return
if ($head)
then (
$head,
for $path in local:add-path($tail)
return string-join(($head, $path), "\")
)
else ()
};
for $uri in local:add-path(fn:tokenize(fn:normalize-space(fn:replace($uri,"/"," ")), " "))
return concat($path, $uri)
In this specific case, an alternative would be to loop over a position counter and join the parts up to this position: 在这种特定情况下,一种替代方法是在位置计数器上循环并将零件连接到该位置:
declare variable $path as xs:string :="D:\Mongo\";
declare variable $uri as xs:string := "/MJ/1932/Vol1/Part1/387.xml";
let $parts := fn:tokenize(fn:normalize-space(fn:replace($uri,"/"," ")), " ")
for $i in (1 to count($parts))
return concat($path, string-join($parts[position() <= $i], '\'))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.