简体   繁体   中英

xquery efficient predicates versus for…where

I have a series of xml nodes that look like this:

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>

I have a search form that may or may not offer sequence parameters against work/@xml:id , work/author/@corresp , and work/affiliation/$corresp , for example:

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

or

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

I am trying to create an efficient query in Xquery 3.1 (eXist 4.7) to account for the different permutations of parameters, while outputting work/ . This has lead me to build an ugly series of nested if statements which trying to favour predicates over for...where like the following:

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

Is there a more efficient way to build an Xquery accounting for variable present/absent parameters?

Many thanks in advance.

I think for the predicates you just want @corresp=$authors or not(exists($authors)) and @corresp=$groups or not(exists($groups)) .

For the id call I think you need

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))]]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM