I'm trying to pass to a function a list of element and from that element I want to extract only a part (depending on the number of calories).
Here you can see my code:
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
And this is the XML
<breakfast_menu>
<food>
<name>Belgian Waffles</name>
<price>$5.95</price>
<description>Our famous Belgian Waffles with plenty of real maple syrup</description>
<calories>650</calories>
</food>
<food>
<name>French Toast</name>
<price>$4.50</price>
<description>Thick slices made from our homemade sourdough bread</description>
<calories>600</calories>
</food>
<food>
<name>Homestyle Breakfast</name>
<price>$6.95</price>
<description>Two eggs, bacon or sausage, toast, and our ever-popular hash browns</description>
<calories>950</calories>
</food>
</breakfast_menu>
What I'd like to see is:
<name>French Toast</name>
<name>Homestyle Breakfast</name>
Can anybody suggest me what I am missing?
There are a couple problems. 1) To use a where
clause you need a valid FLWOR statement - in this case you are missing a for
. 2) The incorrect value is used in your comparison:
for $doc in $val//food
where $doc/calories > $max
return $doc
However, you can also select the same nodes using only XPath:
$val//food[calories > $max]