Ahmad
Ahmad

Reputation: 9658

How to check whether node $N is present in the sequence-of-nodes $S in XQuery?

I know each variable in XQuery which takes an XPath expression can consist of a set of nodes

let $x := //cities[@population > '50000']//*

Or

let $y := //something/following::*

I would like to know is there a boolean function to check if $z belongs to these sets of nodes, for example $x or $y?

If no, then how can I declare such function to take two variables and return true if the first variable (sequence of nodes ) include the second (a node or sequence of nodes)

for a particular purpose I did the following test:

declare function local:inside($y,$x) as xs:boolean
{
  let $r :=  ? (: want to know if $y include $x or not :)
  return $r
};
let $db := doc('products.xq')
let $items := $db/item//*
let $pricesInItems := $db//price[$items//.]   (: $db//price[local:inside($items, .)] :)
                    (: ^ it doesn't work, by the way :)
return $pricesInItems 

for this database:

<page>
  <products> 
      <item foo="f1"> 
          <price>100 </price> 
      </item>  
        <item foo="f2"> 
          <price>200 </price> 
      </item>  
  </products>
  <price>300 </price>
</page>

A predicate expression or a function which selects all prices where are inside $db/item//*. Please note this is just an example, I would like to have a general membership function or predicate expression which works for any two variables.

Upvotes: 2

Views: 2906

Answers (2)

Michael Kay
Michael Kay

Reputation: 163342

Two approaches to test whether node $N is present in the sequence-of-nodes $S:

(a) exists($S intersect $N)

The exists() is redundant if used in a boolean context, e.g. you can write if($S intersect $N)

(b) exists($S[. is $N])

(Similarly, in a boolean context you can write if($S[. is $N])

Upvotes: 1

wst
wst

Reputation: 11771

If you use the = operator, it will use set-based logic. For example:

let $set := (2, 4, 6, 8, 10)
return ((6 = $set), (7 = $set))

=> (true(), false())

The same applies for comparisons across two sets:

(1, 2, 3, 4) = (4, 5, 6)
=> true()

Upvotes: 3

Related Questions