问题
I have tried below mentioned 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.
回答1:
XQuery is a functional programming language, which implies variables are immutable. 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.
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], '\'))
来源:https://stackoverflow.com/questions/36264080/marklogic-how-to-assign-dynamic-variable-in-xquery