Marklogic 将空节点传递给 xquery 函数

Marklogic pass empty node to xquery function

这是我尝试在 MarkLogic XQuery 管理器中开发一个函数的简单简化。我尝试编写的函数必须能够接收空节点作为输入。我一直试图通过 () 来表示 "empty node" 并且它似乎只是崩溃而没有任何痕迹。

例如,显示的简单示例预计只是 return 数字“1”,但事实并非如此。如果我改为传递一个小的非空 XML 文档,那么这个简单的例子就可以了。

请问我在传递空节点时的推理有什么问题?

declare function local:x ($i as node()) as xs:string*

{  let  $x := "1"

   return $x
};

local:x ( () );

你的问题是你的函数只需要一个 node() 而不是 empty-sequence()(这是你通过这样调用你的函数所提供的:local:x( () )

无法将空序列转换为节点。 如果你想提供一个需要 零个或一个 节点的函数,你可以这样做:

declare function local:x($i as node()?) as xs:string* {
  let $x := "1"
  return $x
  (:
    Also instead of doing the above you could also simply return the string directly by simply typing it out:
    "1"
  :)
};

这里问号是重点:

Some functions accept a single value or the empty sequence as an argument and some may return a single value or the empty sequence. This is indicated in the function signature by following the parameter or return type name with a question mark: "?", indicating that either a single value or the empty sequence must appear.

(摘自W3C

您应该注意的一件事是,空序列与例如 相同。空文本节点!

let $emptySeq := () (:This actually has no value at all:)
let $emptyText := text {} (:This simply is an empty node, but it is still a node!:)
return (fn:empty($emptySeq), fn:empty($emptyText))

这将 return (true, false)