Xml XPath:收集同时聚合问题
我有一个结构如下的XML文件:Xml XPath:收集同时聚合问题,xml,xpath,xpath-2.0,Xml,Xpath,Xpath 2.0,我有一个结构如下的XML文件: <person height="170"/> <person height="155"/> <person height="192"/> ... <tree height="100"/> <tree height="300"/> <tree height="120"/> ... 我需要返回所有至少有3棵树且高度在此人身高+-10范围内的人员 例如,如果一个人的身高为155,则当至少有3棵
<person height="170"/>
<person height="155"/>
<person height="192"/>
...
<tree height="100"/>
<tree height="300"/>
<tree height="120"/>
...
我需要返回所有至少有3棵树且高度在此人身高+-10范围内的人员
例如,如果一个人的身高为155,则当至少有3棵树的高度在145到165之间时,该人将被返回
我尝试实施它,并取得了以下成果:
//person[count(//tree[@height >>>>>>is in range 10 to person's height<<<<<]) >= 3]
//person[count(//tree[@height>>>>>>>在10到人的高度范围内这是一个可以使用的$me in的地方。
习惯用法:
//person[count(for $me in . return //tree[
@height <= ($me/@height + 10) and @height >= ($me/@height - 10)]) >= 3]
//person[count(对于$me in.return//tree[
@高度=($me/@height-10)])>=3]
在这里,我使用一次迭代for
表达式来捕获当前的person
(第一级谓词的
),以便在第二级谓词中引用它(其中
是树
).我本以为会有一些语法糖来取代这个可怕的习惯用法,但它很管用,所以我不能抱怨:)@Mugen我总是觉得有点奇怪,for
是XPath的一部分,而let
只是XQuery。