Page 1 of 1

XQuery doesn't execute

Posted: Wed Oct 05, 2022 7:19 am
by DeepMohinman
I have an XQuery problem:

Code: Select all

{
let $x := $document//AOSCAT_MetricDetail//table[@class="pretty-table"]
          //tr/td[8]/text()
let $sum_of_statements_per_method := sum(
          for $a at $i in $x where $a != 0.0 return $a 
    )
let $count_of_statements_per_method := count(
          for $a at $i in $x where $a != 0.0 return $a 
    )
return
if ($count_of_statements_per_method != 0) then
    return <average_statements_per_method>{ 
       $sum_of_statements_per_method 
       div 
       $count_of_statements_per_method 
    }</average_statements_per_method>   
else
    return <average_statements_per_method> 
       No data available 
    </average_statements_per_method>    
}
Executing this XQuery with Saxon9 yields the following Error:
Error on line 103 column 39 of transform_2.xq:
XPST0003: XQuery syntax error in #...verage_statements_per_method>{#:
expected "else", found ">"
Static error(s) in query
But i dont understand why. In the same File a have a very similar XQuery expression and there's no problem at all...

Is anyone able to point me to where my mistake is ?

EDIT: But why does this work ok, then? :

Code: Select all

{
let $content_check := $document//AOSCAT_MainReport//div[@class="reportpart"][4]/h2/text()
return
if($content_check="Abstraction analysis") then                                                              
let $abstraction := $document//AOSCAT_MainReport//div[@class="reportpart"][4]/table//tr[2]//td[5]
    return <abstraction_level>{ fn:number(fn:normalize-space($abstraction))*100 }</abstraction_level>
else 
    let $abstraction := 0
    return <abstraction_level>{ $abstraction }</abstraction_level>  
}

Re: XQuery doesn't execute

Posted: Wed Oct 05, 2022 11:02 am
by Martin Honnen
The syntax of an `if` expression in XQuery is simply e.g. `if (condition-expression) then expression1 else expression2`, there is no `return` involved, neither in the `then expression1` nor in the `else expression2`.