Xquery,计算后代中出现的值
Xquery, counting value occurrence in descendants
假设我们有文件 resturants.xml 看起来像
<restaurant>
<name>Benny’s hamburgers</name>
<chef>Benny</chef>
</restaurant>
<restaurant>
<name>The Restaurant</name>
<chef>Oliver</chef>
</restaurant>
<restaurant>
<name>Italian’s</name>
<chef>Oliver</chef>
</restaurant>
<restaurant>
<name>La pasta</name>
<chef>Oliver</chef>
</restaurant>
并且我们想要 return 在 2 家或更多餐厅工作的厨师的姓名以及这些餐厅的名称,XQuery 代码会是什么样子?
我试过使用
$xml = doc("resturants.xml")
for $restaurant in $xml/restaurant
where count($restaurant/chef) >= 2
return $restaurant/name
但这似乎 return 餐厅的名称至少有 2 名厨师同名。任何帮助将不胜感激!
XQuery 不会自动为您连接数据。此外,从另一个方向思考这类问题也很有帮助。由于要关联一个厨师关联多个餐厅,可以先获取唯一的厨师名,再关联关联的餐厅。
for $name in distinct-values($xml/restaurant/chef)
let $restaurants := $xml/restaurant[chef = $name]
where count($restaurants) >= 2
return
element chef {
element name { $name },
element restaurants {
$restaurants/name
}
}
假设我们有文件 resturants.xml 看起来像
<restaurant>
<name>Benny’s hamburgers</name>
<chef>Benny</chef>
</restaurant>
<restaurant>
<name>The Restaurant</name>
<chef>Oliver</chef>
</restaurant>
<restaurant>
<name>Italian’s</name>
<chef>Oliver</chef>
</restaurant>
<restaurant>
<name>La pasta</name>
<chef>Oliver</chef>
</restaurant>
并且我们想要 return 在 2 家或更多餐厅工作的厨师的姓名以及这些餐厅的名称,XQuery 代码会是什么样子?
我试过使用
$xml = doc("resturants.xml")
for $restaurant in $xml/restaurant
where count($restaurant/chef) >= 2
return $restaurant/name
但这似乎 return 餐厅的名称至少有 2 名厨师同名。任何帮助将不胜感激!
XQuery 不会自动为您连接数据。此外,从另一个方向思考这类问题也很有帮助。由于要关联一个厨师关联多个餐厅,可以先获取唯一的厨师名,再关联关联的餐厅。
for $name in distinct-values($xml/restaurant/chef)
let $restaurants := $xml/restaurant[chef = $name]
where count($restaurants) >= 2
return
element chef {
element name { $name },
element restaurants {
$restaurants/name
}
}