在 XQuery 1.0 中使用 for 创建列表

Tim*_*mmy 2 xml xquery

我想在这个例子中使用 for like 创建一个元素列表:

for $ingr in distinct-values(//Ingredient/Name)
let $res := (
  for $res2 in //Restaurant
  return if( some $ingr2 in $res2/Dish/Ingredient/Name satisfies $ingr2 eq $ingr )
    then( $res2 )
    else()
  )
return $ingr, $res
Run Code Online (Sandbox Code Playgroud)

我想获得的是:For each ingredient return the list of restaurants that use it。我真的不明白问题出在哪里?

在这里你可以看到一个例子:http : //www.xpathtester.com/xquery/0c8f9699df404020afd07ff9f9517a46

一直在说: ERROR - Variable $res has not been declared

har*_*r07 5

你的表情被读作(for ... let ... return $ingr), ($res). 这意味着$res最后被视为与之前的 FLWOR 表达式不同的表达式,因此会出现未声明的变量错误。

您可以简单地使用括号来改变这个不需要的分组:

for $ingr in distinct-values(//Ingredient/Name)
let $res := (
  for $res2 in //Restaurant
  return if( some $ingr2 in $res2/Dish/Ingredient/Name satisfies $ingr2 eq $ingr )
    then( $res2 )
    else()
  )
return ($ingr, $res)
Run Code Online (Sandbox Code Playgroud)

xpathtester demo