簡體   English   中英

xquery可選的where語句

[英]xquery optional where statements

在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>

用戶可以針對xml:idaffiliationauthorlocation提交可選參數,並且它們可以是具有多個值(序列)的參數。

如果用戶要提交所有參數,則查詢可能類似於:

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

但是用戶可以將某些參數留空,從而有效地使每個where語句為可選。

我目前可以放在一起的唯一解決方案是擁有一系列if...then...else語句,這些語句負責參數的每個排列。

Xpath或Xquery中是否可以通過某種wildcard說明參數為空的方式? 在偽代碼中,其中*表示期望的通配符:

 where $c/affiliation[if ($params_affil) 
                      then @corresp=($params_affil)
                      else @corresp=* ] 

非常感謝。

使用形式的謂詞

[$params_affil=("", @corresp)]

如果$ params_affil是長度為零的字符串或等於@corresp ,則@corresp 如果未提供參數,則將零長度字符串(而不是空序列)設置為默認值。

或者,如果缺席參數的默認值為(),請使用

[empty($params_affil) or $params_affil=@corresp)]

如果這太重復了,請將邏輯放在用戶聲明的函數中。

我認為您可以隨時聲明並使用自己的函數作為謂詞表達式,例如

declare function local:check-item($item as node(), $values as item()*) as xs:boolean
{
    if (exists($values))
    then $item = $values
    else true()
};

....
where $c/affiliation[local:check-item(@corresp, $params_affil)]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM