我之前提出这个问题的尝试显然太复杂了,请再试一次!我正在Xquery中进行搜索。在一个字段(标题)中,应该可以输入多个关键字。目前,只有一个关键字有效。当存在多个错误时,将显示错误ERROR XPTY0004:参数1的实际基数与函数签名中声明的基数不匹配:concat($ atomizable-values as xs:anyAtomicType ?, ...)xs:string? 。预期基数:零或一,得到2。
在我的xquery中,我尝试通过\ s标记关键字,然后分别进行匹配。我认为此方法可能是错误的,但我不确定要使用其他什么方法。我显然是一个初学者!!
这是要搜索的示例XML:
<files>
<file>
<identifier>
<institution>name1</institution>
<idno>signature</idno>
</identifier>
<title>Math is fun</title>
</file>
<file>
<identifier>
<institution>name1</institution>
<idno>signature1</idno>
</identifier>
<title>philosophy of math</title>
</file>
<file>
<identifier>
<institution>name2</institution>
<idno>signature2</idno>
</identifier>
<title>i like cupcakes</title>
</file>
</files>
这是Xquery,其中搜索字段标题的示例输入为“ math”,搜索字段机构的示例输入为“ name1”。这项工作有效,搜索输出为标题“数学很有趣”和“数学哲学”。如果将输入($ title)更改为“数学乐趣”,那是行不通的。然后,您会收到错误消息。所需的输出是标题“数学很有趣”。
xquery version "3.0";
let $institution := 'name1'
let $title := 'math' (:change to 'math fun' and doesn't work anymore, only a single word works:)
let $title-predicate :=
if ($title)
then
if (contains($title, '"'))
then concat("[contains(lower-case(title), '", replace($title, '["]', ''), "')]") (:This works fine:)
else
for $title2 in tokenize($title, '\s') (:HERE IS THE PROBLEM, this only works when the input is a single word, for instance 'math' not 'math fun':)
return
concat("[matches(lower-case(title), '", $title2, "')]")
else ()
let $institution-predicate := if ($institution) then concat('[lower-case(string-join(identifier/institution))', " = '", $institution, "']") else ()
let $eval-string := concat
("doc('/db/Unbenannt.xml')//file",
$institution-predicate,
$title-predicate
)
let $records := util:eval($eval-string)
let $test := count($records)
let $content :=
<inner_container>
<div>
<h2>Search Results</h2>
<ul>
{
for $record in $records
return
<li id="searchList">
<span>{$record//institution/text()}</span> <br/>
<span>{$record//title/text()}</span>
</li>
}
</ul>
</div>
</inner_container>
return
$content
您必须使用string-join()包装FLWOR表达式:
string-join(
for $title2 in tokenize($title, '\s')
return
concat("[matches(lower-case(title), '", $title2, "')]")
)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句