【问题标题】:xquery call a maintenance function within another functionxquery 在另一个函数中调用维护函数
【发布时间】:2013-01-03 14:12:30
【问题描述】:

使用 MarkLogic Xquery,我有一个函数 (admin:add-collection-to-publication),它调用另一个维护函数 ( admin:check-collections-exists),它检查元素的存在,如果它不存在,则创建该特定元素。

我调用维护函数的方式是使用 let。这似乎是一种奇怪的方式,要做到这一点,它需要创建一个未使用的变量。我是否应该返回一个序列,调用admin:check-collections-exists 是序列中的第一项,然后后续处理是第二个元素?只是寻找标准优雅的方式来做到这一点。我的职能是:

declare function admin:add-collection-to-publication($pub-name, $collection-name)
{
(:does this publication have a COLLECTIONS element?:)
let $unnecessary-variable := admin:check-collections-exists($pub-name)
    (:now go and do what this function does:)
return "do some other stuff then return"

 };

 declare function admin:check-collections-exists($pub-name)
 {
if(fn:exists($pubs-node/pub:PUBLICATION[pub:NAME/text()=$pub-name]/pub:COLLECTIONS))
then
    "exists"
else
    xdmp:node-insert-child($pubs-node/pub:PUBLICATION[pub:NAME/text()=$pub-name],<pub:COLLECTIONS/>)
};

【问题讨论】:

  • 我经常使用func()[0](或者func()[4000000000],如果前者被优化掉)来计算一些东西并忽略它的结果
  • 我在 marklogic 文档中看到 xdmp:function 但如果您的函数带有参数,您似乎仍然需要返回。 xquery 版本“1.0-ml”;让 $function := xdmp:function(xs:QName("fn:concat")) 返回 xdmp:apply($function, "hello", "world") => hello world

标签: xquery marklogic


【解决方案1】:

使用序列是不可靠的。 MarkLogic 很可能会尝试并行评估序列项,这可能导致创建在“同时”时间甚至在其他工作之后发生。最好的方法确实是使用letlet 总是在 return 之前评估。请注意,虽然let 也可以并行评估,但优化器足够聪明,可以检测依赖关系。

就我个人而言,我经常使用未使用的变量。例如插入日志语句,在这种情况下,我有一个未使用的变量名,我每次都会重用:

let $log := xdmp:log($before)
let $result := do:something($before)
let $log := xdmp:log($result)

您也可以使用非常短的变量名,例如$_。或者你可以重新考虑给变量一个合理的名字,毕竟使用它,即使你知道它永远不会到达 else

let $exists := 
    if (collection-exists()) then
        create()
    else true()
return
    if ($exists) then
        "do stuff"
    else () (: never reached!! :)

HTH!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-09-17
    • 2013-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-07
    相关资源
    最近更新 更多