如何检查多个相同元素的列表是否具有与属性相关的另一个列表的所有元素

时间:2013-11-23 03:49:49

标签: xml xquery

如何检查“人”是否拥有“汽车标签”中属性状态为ON的所有“车辆”?

<deluxe person="John"    brand="Mercedes">
<deluxe person="John"    brand="Lamborgini">
<deluxe person="John"    brand="Seat">
<deluxe person="Tyson"   brand="Volva">
<deluxe person="Miles"   brand="Wolkswagen">

这是车辆列表:

<vehicule>
<car status="on">
<brand>Mercedes</brand>
</car>

<car status="off">
<brand>Peugeot</brand>
</car>

<car status="on">
<brand>Lamborgini</brand>
</car>

<car status="on">
<brand>Seat</brand>
</car>
</vehicules>

结果是它返回元素<deluxe person ="John">

自24小时以来我一直坚持这一点。

2 个答案:

答案 0 :(得分:0)

我确信有更有效的方法可以做到这一点......并不完全确定你在问什么,但我怀疑这会对你有帮助。

let $v :=
<vehicule>
<car status="on">
<brand>Mercedes</brand>
</car>

<car status="off">
<brand>Peugeot</brand>
</car>

<car status="on">
<brand>Lamborgini</brand>
</car>

<car status="on">
<brand>Seat</brand>
</car>
</vehicule> 
let $p := <d>
<deluxe person="John"    brand="Mercedes"/>
<deluxe person="John"    brand="Lamborgini"/>
<deluxe person="John"    brand="Seat"/>
<deluxe person="Tyson"   brand="Volva"/>
<deluxe person="Miles"   brand="Wolkswagen"/>
</d>

let $all := for $i in $v/car[@status="on"]/brand/string() order by $i return $i


for $person in distinct-values($p/deluxe/@person)

  let $brands := for $i in $p/deluxe[@person=$person]/@brand/string() order by $i return $i

  return if ($brands eq $all) then
      $person
  else  
      ()

答案 1 :(得分:0)

如果你能够使用quantified expressions,你可以隐含地进行内循环;并且查询相当自然。

let $vehicules :=
  <vehicule><!-- snip --></vehicule> 
let $persons :=
  <d><!-- snip --></d>

for $person in distinct-values($persons//@person)
where
  every $car in $vehicules/car[@status='on']
  satisfies $car/brand=$persons/deluxe[@person=$person]/@brand
return $person