[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

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


Subject: RE: [xsl] Coding aroung a "Cannot convert zero-length string to an integer" error
From: "Angela Williams" <Angela.Williams@xxxxxxxxxxxxxxxxxx>
Date: Mon, 13 Aug 2007 09:31:07 -0500

I've found problems with using the not($stringIsEmpty) construct where I
get a false true result, I suspect due to the presence of a node-set
that is empty as opposed to an empty string.

I've found if (not(string-length($input-date) eq 0 ) to be much more
reliable, if more verbose.  I'm using Saxon 8.9 and XSLT 2.0.

Are there other considerations for choosing one solution vs. the other?


Thanks!
Angela

-----Original Message-----
From: Andrew Welch [mailto:andrew.j.welch@xxxxxxxxx]
Sent: Monday, August 13, 2007 9:23 AM
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
Keywords