Using max function with dates in xquery

<oXygen/> general issues.
DaronRyan
Posts: 4

Using max function with dates in xquery

Sat Nov 12, 2005 1:48 am

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.
alex_jitianu
Posts: 652

Wed Nov 16, 2005 11:37 am

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
DaronRyan
Posts: 4

That worked, thankyou

Wed Nov 16, 2005 12:06 pm

Hello Alex,

I quickly tried you suggestion and it worked.

Thank you,
Daron.

Return to “Other Issues”

Who is online

Users browsing this forum: No registered users and 0 guests