Xquery ( XDMP-ARGTYPE error ) 函数输入的预期类型与实际类型不同
Xquery ( XDMP-ARGTYPE error ) the expected type of a function input is different from the actual type
我正在尝试使用此功能从 MarkLogic 8 中的文本中删除停用词:
declare function rec:remove-stop-words($string, $stop_words) {
(: This is a recursive function. :)
if(not(empty($stop_words))) then
rec:remove-stop-words(
replace($string, $stop_words[1], '', 'i'),
(: This passes along the stop words after
the one just evaluated. :)
$stop_words[position() > 1]
)
else normalize-space($string)
};
在这里我称之为
for $r in /rec:Record
return
rec:remove-stop-words(data($r/rec:Abstract), $stop_words}
它给我以下错误
XDMP-ARGTYPE: (err:XPTY0004) fn:replace((xs:untypedAtomic("
chapter
utilized asymmetry of n..."), xs:untypedAtomic("
book
interrelationship between ...")), "a", "", "i") -- arg1 is not of type xs:string?
该函数需要 string
类型,但实际类型是 untypedAtomic
。我不知道该怎么办!
注意:((问题不在函数中,因为我尝试将它用于不同的文本并且效果很好))。
我尝试通过以下方式将 untypedAtomic
转换为 string
的代码:
return
<info>{rec:remove-stop-words(data(xs:string($r/rec:Abstract)), $stop_words)}</info>
但是我得到了这个错误:
XDMP-ARGTYPE: (err:XPTY0004) fn:replace(("
chapter
utilized asymmetry of n...", "
book
interrelationship between ..."), "a", "", "i") -- arg1 is not of type xs:string
看起来你发送的是一个节点而不是一个字符串。尝试 $r/rec:Abstract/text()
或 $r/rec:Abstract/string()
尝试使用此代码 -
for $r in /rec:Record
return
rec:remove-stop-words(fn:string($r/rec:Abstract), $stop_words}
问题是,当您遍历 /rec:Record
并将 $r/rec:Abstract
作为输入传递时,至少有一个记录返回多个 rec:Abstract
。 rec:remove-stop-words
的函数签名允许将一系列值作为 $string
的输入,但您调用 fn:replace
的函数体仅处理单个值的输入,因此它抛出参数异常(给定 xs:string+
并期待 xs:string?
).
您可以在调用函数之前通过遍历 rec:Abstract
来处理序列:
for $r in /rec:Record
for $a in $r/rec:Abstract
return
rec:remove-stop-words($a, $stop_words)
如果你使用更严格的函数签名,它可以帮助避免这样的问题,或者至少使它们更容易调试。例如,如果您将函数定义为仅允许第一个参数有一个输入:
rec:remove-stop-words($string as xs:string, $stop_words as xs:string*)
...
这将在 $string
传递一个序列时抛出类似的异常,但在调用堆栈的较高位置,这有助于使这些类型的错误更加明显。
我正在尝试使用此功能从 MarkLogic 8 中的文本中删除停用词:
declare function rec:remove-stop-words($string, $stop_words) {
(: This is a recursive function. :)
if(not(empty($stop_words))) then
rec:remove-stop-words(
replace($string, $stop_words[1], '', 'i'),
(: This passes along the stop words after
the one just evaluated. :)
$stop_words[position() > 1]
)
else normalize-space($string)
};
在这里我称之为
for $r in /rec:Record
return
rec:remove-stop-words(data($r/rec:Abstract), $stop_words}
它给我以下错误
XDMP-ARGTYPE: (err:XPTY0004) fn:replace((xs:untypedAtomic(" chapter utilized asymmetry of n..."), xs:untypedAtomic(" book interrelationship between ...")), "a", "", "i") -- arg1 is not of type xs:string?
该函数需要 string
类型,但实际类型是 untypedAtomic
。我不知道该怎么办!
注意:((问题不在函数中,因为我尝试将它用于不同的文本并且效果很好))。
我尝试通过以下方式将 untypedAtomic
转换为 string
的代码:
return
<info>{rec:remove-stop-words(data(xs:string($r/rec:Abstract)), $stop_words)}</info>
但是我得到了这个错误:
XDMP-ARGTYPE: (err:XPTY0004) fn:replace((" chapter utilized asymmetry of n...", " book interrelationship between ..."), "a", "", "i") -- arg1 is not of type xs:string
看起来你发送的是一个节点而不是一个字符串。尝试 $r/rec:Abstract/text()
或 $r/rec:Abstract/string()
尝试使用此代码 -
for $r in /rec:Record
return
rec:remove-stop-words(fn:string($r/rec:Abstract), $stop_words}
问题是,当您遍历 /rec:Record
并将 $r/rec:Abstract
作为输入传递时,至少有一个记录返回多个 rec:Abstract
。 rec:remove-stop-words
的函数签名允许将一系列值作为 $string
的输入,但您调用 fn:replace
的函数体仅处理单个值的输入,因此它抛出参数异常(给定 xs:string+
并期待 xs:string?
).
您可以在调用函数之前通过遍历 rec:Abstract
来处理序列:
for $r in /rec:Record
for $a in $r/rec:Abstract
return
rec:remove-stop-words($a, $stop_words)
如果你使用更严格的函数签名,它可以帮助避免这样的问题,或者至少使它们更容易调试。例如,如果您将函数定义为仅允许第一个参数有一个输入:
rec:remove-stop-words($string as xs:string, $stop_words as xs:string*)
...
这将在 $string
传递一个序列时抛出类似的异常,但在调用堆栈的较高位置,这有助于使这些类型的错误更加明显。