RE: Re: [xsl] Coding aroung a "Cannot convert zero-length string to an integer" error

Subject: RE: Re: [xsl] Coding aroung a "Cannot convert zero-length string to an integer" error
From: cknell@xxxxxxxxxx
Date: Mon, 13 Aug 2007 11:11:59 -0400
> As you're returning an atomic type use xsl:sequence instead of
> xsl:value-of, otherwise you're doing the unnecessary creating a text
> node and then serializing it.

I don't know how may times I've read something along these lines on this list, but just passed over it. Seeing it in a context meaningful to me enabled me to make sense of xsl:sequence as a type.

> Also no need for the the translate as the function days-from-duration() exists.

That's a good one, too. I did take me some time to find the current and correct URL for the fn: prefix.

-- 
Charles Knell
cknell@xxxxxxxxxx - email



-----Original Message-----
From:     Andrew Welch <andrew.j.welch@xxxxxxxxx>
Sent:     Mon, 13 Aug 2007 15:22:31 +0100
To:       xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject:  Re: [xsl] Coding aroung a "Cannot convert zero-length string to an integer" error

On 8/13/07, cknell@xxxxxxxxxx <cknell@xxxxxxxxxx> wrote:
> <xsl:function name="ck:excel-serial-date" as="xs:string?">
>   <xsl:param name="input-date" as="xs:string?"/>
>   <xsl:value-of select="if($input-date = '') then '' else xs:string(xs:integer(translate(xs:string(xs:date($input-date)-xs:date('1900-01-01')),'PD','')))"/>
> </xsl:function>
>
> This is a function I wrote (am writing?) to compute an integer that represents the a date to Excel 2000.
>
> There may or may not be a value in the input document that corresponds to a cell in the output. In that case, I want the function to return an empty string.
>
> So you can see here that I tell the function to expect a string, and that an empty string (zero-length string) is acceptable input.
>
> I also tell it that it should return a string, and that a zero-length string is acceptable output.
>
> In the body of the function, I state that if the input is a zero-length string, it should return a zero-length string, otherwise it is to compute the number of days between January 1, 1900 and the input date, convert the result to a string, and return that.
>
> Anyway, that's what I thought I was doing, but when I attempt a transformation, I get the following error message:
>
> "Fatal Error! Cannot convert zero-length string to an integer"

I can't recreate that but it suggests you're doing xs:integer('')
after the translate, which means somehow you're calling translate on
the string 'PD' - which processor are you using and which input causes
the error? (it doesn't look like a Saxon error message)

Either way I would separate your single function into multiple functions:

<xsl:param name="startDate" select="xs:date('1900-01-01')" as="xs:date"/>

<xsl:function name="ck:excel-serial-date" as="xs:anyAtomicType?">
 <xsl:param name="input-date" as="xs:string?"/>
 <xsl:sequence select="if (not($input-date)) then ''
 else days-from-duration(ck:subtract-date(xs:date($input-date)))"/>
</xsl:function>

<xsl:function name="ck:subtract-date" as="xs:duration">
 <xsl:param name="date" as="xs:date"/>
 <xsl:sequence select="$date - $startDate"/>
</xsl:function>

Apart from all the usual benefits of separation, it enables one to be
strongly typed and less susceptible to errors.

As you're returning an atomic type use xsl:sequence instead of
xsl:value-of, otherwise you're doing the unnecessary creating a text
node and then serializing it.

Also no need for the the translate as the function days-from-duration() exists.

If the functions aren't what you need can you post a set of input
dates that cause the problem?

cheers
andrew
-- 
http://andrewjwelch.com

Current Thread