declare function local:converti($x as element()) { let $appo := "appo" (: I don't know why, but without something here I have an error when compiling! :) return if ($x/@unit="cup") then data($x/@amount*48) else if ($x/@unit="tablespoon") then data($x/@amount)*3 else data($x/@amount) }; declare function local:compute_amounts($ricetta as element()) { let $but := {$ricetta//ingredient[@name="butter"]} let $oil := {$ricetta//ingredient[@name="olive oil"]} return if (empty($but/*) and empty($oil/*)) then 0 else if (empty($oil/*)) then 1 else if (sum(for $n in $but/* return local:converti($n)) gt sum(for $m in $oil/* return local:converti($m))) then 1 else 0 }; { count( for $x in doc("linkedrecipes_mio3.xml")//ingredient where not($x/ingredient) return $x ) } { for $x in doc("linkedrecipes_mio3.xml")//recipe where $x/ingredient/ingredient return $x/title } { for $x in doc("linkedrecipes_mio3.xml")//recipe return if (local:compute_amounts($x)=1) then $x/title else () }