xquery multi if-else语句

Eni*_*say 2 xml string xquery if-statement

使用xPath,我从html字段获取数据,这些数据可以是这种格式(包括括号):

数据|||| 我用来解释我的代码的符号

(bornPlace,bornDate-DeathDate)|||| (str,AB)=注意str也可能包含' - '

(bornPlace,bornDate)|||| (str,A)

(bornPlace)|||| (STR)

(bornDate-DeathDate)|||| (AB)

(bornDate)|||| (一个)

或者完全空着

我正在尝试使用多个if-else语句将每个元素检索到单独的变量中,但似乎它不喜欢多行命令(我想是这样).

我已经制作了一个不起作用的代码: - /(它表示期待返回,如果......则发现其他情况)

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('')
Run Code Online (Sandbox Code Playgroud)

如果还有更美妙的方式,我会接受它:D

在此先感谢您的帮助 :)

Mic*_*Kay 6

let子句不是表达式.你需要改变这种逻辑

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

这样

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

虽然在这种特殊情况下我会倾向于写:

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