在 Xquery 3.1 中,我正在处理表单中的可变参数以搜索匹配的 XML 文档。XML 文档如下所示:
<listBibl xml:id="TC0001" type="collection">
<bibl>
<title type="collection">Bonum universale de apibus</title>
<affiliation corresp="dominican"/>
<author nymRef="thomas_cantipratensis"/>
<location corresp="flanders"/>
<othercontent>....</othercontent>
</bibl>
</listBibl>
Run Code Online (Sandbox Code Playgroud)
用户可以针对、、和提交可选参数xml:id,它们可以是具有多个值(序列)的参数。affiliationauthorlocation
如果用户要提交所有参数,则查询可能如下所示:
for $c in $mycollection//listBibl[@xml:id=($params_id)]
where $c/affiliation[@corresp=($params_affil)]
and $c/author[@nymRef=($params_author)]
and $c/location[@corresp=($params_location)]
return $c
Run Code Online (Sandbox Code Playgroud)
但是用户可以将某些参数留空,从而有效地使每个where语句都是可选的。
我目前可以组合在一起的唯一解决方案是使用一系列if...then...else语句来解释参数的每个排列。
在 Xpath 或 Xquery 中是否有任何方法来解释参数为空的wildcard情况?在伪代码中, where*代表一个想要的通配符:
where $c/affiliation[if ($params_affil)
then @corresp=($params_affil)
else @corresp=* ]
Run Code Online (Sandbox Code Playgroud)
非常感谢。
使用形式的谓词
[$params_affil=("", @corresp)]
Run Code Online (Sandbox Code Playgroud)
如果 $params_affil 是零长度字符串或等于 ,则匹配@corresp。如果未提供参数,则将零长度字符串(而不是空序列)设为默认值。
或者,如果缺少参数的默认值是 (),请使用
[empty($params_affil) or $params_affil=@corresp)]
Run Code Online (Sandbox Code Playgroud)
如果这太重复了,请将逻辑放在用户声明的函数中。