在 xQuery 中使用函数声明

Use of function declaration in xQuery

我试图将一个元素列表传递给一个函数,然后我只想从该元素中提取一部分(取决于卡路里的数量)。

在这里你可以看到我的代码:

declare function local:calories($val as element(), $max as xs:integer) as element() {
let $doc := $val//food
where $doc/calories > $val
return $doc };

let $doc := doc("calorie.xml")/breakfast_menu
return local:calories($doc, 610)/name

这是 XML

<breakfast_menu>
<food>
  <name>Belgian Waffles</name>
  <price>.95</price>
  <description>Our famous Belgian Waffles with plenty of real maple syrup</description>
  <calories>650</calories>
</food>
<food>
  <name>French Toast</name>
  <price>.50</price>
  <description>Thick slices made from our homemade sourdough bread</description>
  <calories>600</calories>
</food>
<food>
  <name>Homestyle Breakfast</name>
  <price>.95</price>
  <description>Two eggs, bacon or sausage, toast, and our ever-popular hash browns</description>
  <calories>950</calories>
</food>
</breakfast_menu>

我想看的是:

<name>French Toast</name>
<name>Homestyle Breakfast</name>

任何人都可以告诉我我缺少什么吗?

有几个问题。 1) 要使用 where 子句,您需要有效的 FLWOR 语句 - 在这种情况下,您缺少 for。 2) 您的比较中使用了不正确的值:

for $doc in $val//food
where $doc/calories > $max
return $doc

但是,您也可以 select 仅使用 XPath 的相同节点:

$val//food[calories > $max]