首页 > 解决方案 > xquery 高效谓词与 for...where

问题描述

我有一系列看起来像这样的 xml 节点:

let $x := 
 <works>
   <work xml:id="W1">
      <author corresp="AU08"/>
      <group corresp="GR03"/>
   </work>
   <work xml:id="W2">
      <author corresp="AU09"/>
      <group corresp="GR10"/>
   </work>
   <work xml:id="W3">
      <author corresp="AU08"/>
      <group corresp="GR05"/>
   </work>
    ....
 </works>

我有一个搜索表单,它可能会或可能不会提供针对work/@xml:idwork/author/@corresp和的序列参数work/affiliation/$corresp,例如:

let $xmlids := ("W1")
let $authors := ()
let $groups := ("GR05","GR08")

或者

let $xmlids := ()
let $authors := ("AU01","AU08")
let $groups := ("GR05")

我正在尝试在 Xquery 3.1 (eXist 4.7) 中创建一个有效的查询,以考虑参数的不同排列,同时输出work/. 这导致我构建了一系列丑陋的嵌套if语句,它们试图支持谓词,for ...where如下所示:

if (count($xmlids) gt 0 and count($authors) gt 0 and count($groups) gt 0)
   then 
      $x/id($xmlids)/author[@corresp=$authors]/parent::work/group[@corresp=$groups]/parent::work
else if (count($xmlids) gt 0 and count($authors) gt 0 and count($groups) eq 0)
   then 
      $x/id($xmlids)/author[@corresp=$authors]/parent::work
else if ...

有没有更有效的方法来构建一个 Xquery 来考虑变量存在/不存在的参数?

提前谢谢了。

标签: xpathxquery

解决方案


我认为对于您想要的谓词@corresp=$authors or not(exists($authors))@corresp=$groups or not(exists($groups)).

对于id我认为你需要的电话

let $work-items := if ($x/id($xmlids)) then $x/id($xmlids) else $x/work
return $work-items[author[@corresp=$authors or not(exists($authors))] and group[@corresp=$groups or not(exists($groups))]]

推荐阅读