我有这两个xml文件
cars.xml
<cars>
<manufacturer>
<model modelID="1">
</model>
<model modelID="2">
</model>
<model modelID="3">
</model>
</manufacturer>
<manufacturer>
<model modelID="4">
</model>
<model modelID="5">
</model>
<model modelID="6">
</model>
</manufacturer>
</cars>
和一个名为price.xml的文件:
<price>
<model modelID="1">
<price>1000</price>
</model>
<model modelID="2">
<price>3000</price>
</model>
<model modelID="3">
<price>2000</price>
</model>
<model modelID="4">
<price>2000</price>
</model>
<model modelID="5">
<price>100</price>
</model>
<model modelID="6">
<price>5000</price>
</model>
</price>
我想要执行的查询是对于cars.xml中的每个制造商,我想返回其最昂贵模型的modelID,但是我无法弄明白。
我试过的是:
for $manf in doc("cars.xml")//manufacturer
let $p := doc("price.xml")
where $manf/model/@modelID = $p/model/@modelID
and $p/model/price = (for $m in doc("cars.xml")//manufacturer
return max(for $pr in doc("price.xml")
where $m/model/@modelID = $pr/model/@modelID
return data($pr/model/price)))
return data($manf/model/@modelID)
我不知道我是否接近正确,但基本上我需要做的是为每个制造商,使用它的modelID以某种方式找到最昂贵的模型。
答案 0 :(得分:1)
let $p := doc("price.xml")
for $manf in doc("cars.xml")//manufacturer
let $models := $p//model[@modelID = $manf/model/@modelID]
let $max := max($models/price)
return $models[price = $max]