Marklogic - 如何在Xquery中分配动态变量

时间:2016-03-28 14:09:40

标签: xquery marklogic marklogic-8

我尝试了下面提到的XQuery。

<script>
        $(document).ready(function(){

            var selectedgrade = document.getElementById("studgrade");
            var newgrade = selectedgrade.options[selectedgrade.selectedIndex].value;
            document.getElementById("studsection").disabled = true;

            if(newgrade < 7 || newgrade > 10){
                   document.getElementById("studsection").disabled = true;
               } else if(newgrade >= 7 && newgrade <= 10) {
                   document.getElementById("studsection").disabled = false;
               }

        });

        </script>
        <script type="text/javascript">

           function changeGrade() {

                //var selectedgrade = $grade.options[$grade.selectedIndex].value;
                var selectedgrade = document.getElementById("studgrade");
                document.getElementById("studgrade").value = selectedgrade;
                window.location.href = "<?php echo base_url().'schoolportal/addstudent'?>";

           }

        </script>

实际输出

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)

预期输出

    D:\Mongo\MJ
    D:\Mongo\1932
    D:\Mongo\Vol1
    D:\Mongo\Part1
    D:\Mongo\387.xml

请建议我,如何更改动态变量值。

1 个答案:

答案 0 :(得分:4)

XQuery是一种函数式编程语言,它意味着变量是不可变的。您不能简单地增加或附加到已定义的变量。通常,使用递归函数来构造结果。

这个例子(有更简洁的例子,我希望保持各个部分分开并且易于理解)递归地创建路径,每次执行时附加另一个级别。 $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)

在这种特定情况下,另一种方法是循环一个位置计数器并将这些部分连接到这个位置:

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], '\'))