xquery multi if-else statement

if-statement, string, xml, xquery

Solution

A let clause is not an expression. You need to change this kind of logic

if (contains($temp2,'-'))
    then
        let $bornDate := substring-before($temp2, '-')
        let $deathDate := substring-after($temp2, '-')
    else
        let $bornDate := $temp2
        let $deathDate := data('')

by this

let $hyphenated := contains($temp2, '-')
let $bornDate := if ($hyphenated) then substring-before($temp2, '-') else $temp2
let $deathDate := if ($hyphenated) then substring-after($temp2, '-') else ''
return ...

Though in this particular case I would be inclined to write:

let $tokens := tokenize($temp2, '-')
let $bornDate := $tokens[1]
let $deathDate := string($tokens[2])
return ...

Problem

Using xPath, I'm getting data from html field, this data can be in this format (including parentheses): DATA |||| symbols i'm using to explain my code (bornPlace, bornDate-DeathDate) |||| (str, A-B) = note that str may also contain '-' (bornPlace, bornDate) |||| (str, A) (bornPlace) |||| (str) (bornDate-DeathDate) |||| (A-B) (bornDate) |||| (A) Or completely empty I'm trying to retrieve each element into separate variable using multiple if-else statements, but it seems that it doesnt like multi-line commands (I think so). I already made a code wich is not working :-/ (it says expecting return, found else if ....) ``` let $temp1 := data(normalize-space(substring-before(substring-after(//div/div[2]/h2/text(), '('), ')'))) if (contains($temp1,',')) (: (str, A-B) or (str, A) :) then let $bornPlace := substring-before($temp1, ',') let $temp2 := substring-after($temp1, ',') if (contains($temp2,'-')) then let $bornDate := substring-before($temp2, '-') let $deathDate := substring-after($temp2, '-') else let $bornDate := $temp2 let $deathDate := data('') else if (contains($temp1,'-')) then (: (s-t-r) or (A-B) :) let $temp2 := normalize-space(substring-before($temp1, '-')) if (number($temp2)=$temp2) (: it's a number :) then let $bornDate := temp2 let $deathDate := normalize-space(substring-after($temp2, '-')) let $bornPlace := data('') else let $bornPlace := $temp1 let $bornDate := data('') let $deathDate := data('') else (: (str) or (A) :) if (number($temp1)=$temp1) (: it's a number :) then let $bornDate := temp1 let $deathDate := data('') let $bornPlace := data('') else let $bornPlace := $temp1 let $bornDate := data('') let $deathDate := data('') ``` Also if there's a more beautiful way to do that, i'll take it :D Thanks in advance for your help :)

Original source