XML file is a list of <n> tags each contains a single number, sorted in ascending order by number inside <n> tags. Need to find if three of them satisfies x^2 + y^2 = z^2
Here is my code:
declare function local:square($k as xs:decimal?)
as xs:decimal? {
let $sq := ($k * $k)
return $sq
};
declare function local:square_sum($a as xs:decimal?,$b as xs:decimal?)
as xs:decimal? {
let $sqsum := (local:square($a) + local:square($b))
return $sqsum
};
<result>{
let $doc := doc("emp.xml")/list
let $x := 0
let $y := 0
for $z in //n
let $zconv := xs:decimal($z/text())
let $lhs := local:square( $zconv )
let $rhs := local:square_sum($x, $y)
if ($lhs = $rhs) then (
return <t>
<n>{$x}</n>
<n>{$y}</n>
<n>{$zconv}</n>
</t>
)
}</result>
why is it not running, instead prompting me with an error saying:
expected "return", found "if("
Please, help sort this out, also check if casting to decimal is okay? for $zconv
Based on suggestion Changed code to this:
for $z in //n
let $zconv := xs:decimal($z/text())
let $lhs := local:square( $zconv )
let $rhs := local:square_sum($x, $y)
return if ($lhs = $rhs)
then ( <t>
<n>{$x}</n>
<n>{$y}</n>
<n>{$zconv}</n>
</t>
)
else (
$x := $y
$y := $zconv
)
now it says:
expected ")", found ":="
Need to update $x and $y then rerun the loop for next n-value because Need to satisfy the the given condition.
$x := $yis only allowed when you're declaring variable $x (e.g., in aletclause). You'll have to think of a different way to solve the problem, one that doesn't involve updating variables. - Michael Dyck