[XQuery] how find the max value in an attribut

Hello,

Does Someone know how I find the maximal value of attribut in an element. I show you this xquery exemple:

declare namespace xs=‘http://www.w3.org/2001/XMLSchema
for $c in max(input()/Case[@year=xs:gYear(‘2000’) and @crit=‘BL’]/@number)
return $c

If I write this exemple, I have no answer, it take too long tim.
However, I have an index on year et crit attribut.

If I write this xquery without max() function, I hava an answer, but it is a list number and I want to have the maximum value of the number attribut.

Thank you very much for your help.

C

I’d try

declare namespace xs='http://www.w3.org/2001/XMLSchema'

max(
  for $c in input()/Case
    where $c/@year=xs:gYear('2000') and $c/@crit='BL'
    return $c/@number
 )

Thank you Curtis for your help, now It works very well ! :lol:

C

Try this:

declare namespace xf = “http://www.w3.org/2002/08/xquery-functions
declare namespace xs = “http://www.w3.org/2001/XMLSchema
let $max:= (
input()/Case[@year=xs:gYear(‘2000’) and @crit=‘BL’]
sort by (@number descending)
)[1]
return $max

Regards,

Joachim