tags:

views:

394

answers:

3

Consider the following code which checks for an empty date:

declare xqse function tns:isEmptyDate($dob as xs:date) as xs:boolean {
    if(empty($dob)) then {
    }
}

When executed, why do I get the below error in ALDSP:

weblogic.xml.query.exceptions.XQueryDynamicException: {err}XP0021: "": can not cast to {http://www.w3.org/2001/XMLSchema}date: error: date: Invalid date value: wrong type:

A: 

I've only tinkered with XQuery, but I suspect that the error is being generated because the function signature ($dob as xs:date) requires a date type and you're passing something else e.g. an empty value.

Kev
A: 

OK, I've had a look at this and I think you'll be able to run your query by defining your function as

declare function tns:isEmptyDate($dob as xs:date?) as xs:boolean

Note the ? after the type - it means that the argument may be the empty sequence.

I tested this out in Oxygen, using Saxon-B... sorry, I don't have access to the software you're using.

Here's my function definition.

declare function tns:isEmptyDate($dob as xs:date?) as xs:boolean {
    let $empty := if (empty($dob))
        then true()
        else false()
    return $empty
};

Running against this file:

<?xml version="1.0" encoding="UTF-8"?>
<datetime>2002-09-24</datetime>

returns true, and running against this file:

<?xml version="1.0" encoding="UTF-8"?>
<dontmatch>2002-09-24</dontmatch>

returns false.

Running the same function without the questionmark, on the second document, errors with:

Severity: error. Description: An empty sequence is not allowed as the first argument of tns:isEmptyDate()

Brabster
A: 

Hi,

Try this with a question mark or star after the type declaration.

E.g.,

$dob as xs:date means $dob is a sequence of 1 item (of type xs:date)

$dob as xs:date? means $dob is a sequence of 1 or none items (each of which are of type xs:date)

$dob as xs:date* means $dob is a sequence of none or more items (each of which are of type xs:date)