简体   繁体   English

如何检查节点 $N 是否存在于 XQuery 的节点序列 $S 中?

[英]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我知道 XQuery 中采用 XPath 表达式的每个变量都可以由一组节点组成

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 ?我想知道是否有一个布尔函数来检查$z属于这些节点集,例如$x$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)如果不是,那么如果第一个变量(节点序列)包含第二个(节点或节点序列),我如何声明这样的函数来接受两个变量并返回 true

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//* .一个谓词表达式或一个函数,它选择$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.请注意这只是一个例子,我想要一个适用于任何两个变量的通用成员函数或谓词表达式。

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

Two approaches to test whether node $N is present in the sequence-of-nodes $S:测试节点 $N 是否存在于节点序列 $S 中的两种方法:

(a) exists($S intersect $N) (a) exists($S intersect $N)

The exists() is redundant if used in a boolean context, eg you can write if($S intersect $N)如果在布尔上下文中使用, exists()是多余的,例如你可以写if($S intersect $N)

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

(Similarly, in a boolean context you can write if($S[. is $N]) (同样,在布尔上下文中,您可以编写if($S[. is $N])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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