如何最大限度地减少 XQuery 中 FOR 循环的使用?

How to minimize the use of FOR loop in XQuery?

我有如下代码,我想知道是否可以在不使用 FOR 循环的情况下获得相同的结果-

let $doc :=  cts:search(fn:doc(), 
                cts:element-value-query(......))

let $column-values := ("one,two,three....") (: Can be n number of values :)

let $tokenized-values := fn:tokenize($column-values, ",")

let $result := for $i in $doc
               let $temp := for $j in $tokenized-values
                              return fn:concat("$i//*:",$j)

                    return <root>{xdmp:value($temp)}</root>

return <result>{$result}</result>

预期结果如下-

<result>
  <root>
    <one>abc</one>
    <two>456</two>
    <three>675</three>
  </root>
  <root>
    <one>dfd</one>
    <two>235</two>
    <three>765</three>
  </root>
</result>

我得到了结果,但是如果我想尽量减少使用 FOR 循环,我怎样才能得到相同的结果。

有什么建议吗?

为了提高性能,您可以在要提取的所有列上放置一个范围索引,并使用 cts:element-value-tuples 代替 cts:search。这将只提取您想要的元素,而不是整个文档。

对于第二个 for 循环的替代语法,您可以使用此语法:

for $j in $tokenized-values
                          return fn:concat("$i//*:",$j)

$tokenized-values ! fn:concat("$i//*:", .)

虽然性能上差不多