Page 1 of 1

Using max function with dates in xquery

Posted: Sat Nov 12, 2005 1:48 am
by DaronRyan
I am trying to find the maximum date in an xml file using xquery. I believe I shold be able to do so using the following.

Code: Select all

declare namespace q2 =  "http://daron.ryan.org/namespace/q2.html";
<younger_workers>
{
let $workers:= doc("q2.xml")//q2:workers
let $youngest :=xs:date(max($workers//q2:bdate))
return $youngest
}
</younger_workers>
however this error message occurs:

SystemID: null
Description: E Failure converting {1975-02-20} to a number

This code

Code: Select all

declare namespace q2 =  "http://daron.ryan.org/namespace/q2.html";
<younger_workers>
{
let $workers:= doc("q2.xml")//q2:workers
let $youngest :=xs:date(max($workers//q2:bdate))
let $copy := $youngest
return <blank></blank>
}
</younger_workers>
produces the same message. Deleting the line

Code: Select all

 let $copy := $youngest


gets rid of the error. I cannot use

Code: Select all

$youngest
variable in any way without causing the error. Can anyone explain what is wrong?

Regards,
Daron.

Posted: Wed Nov 16, 2005 11:37 am
by alex_jitianu
Hello,

All items in the argument of the max() function must be numeric or derived from a single base type.
So the items from "$workers//q2:bdate" are being converted to numeric type but something like {1975-02-20} cannot be converted to a number.

You can try the following code :

Code: Select all


declare namespace q2 =  "http://daron.ryan.org/namespace/q2.html";
<younger_workers>
{
let $workers:= doc("q2.xml")//q2:workers
let $dates :=$workers//q2:bdate
let $youngest :=max(for $date in $dates return xs:date($date))
return $youngest
}
</younger_workers>
Hope it helps,
Alex

That worked, thankyou

Posted: Wed Nov 16, 2005 12:06 pm
by DaronRyan
Hello Alex,

I quickly tried you suggestion and it worked.

Thank you,
Daron.