xsl-list
[Top] [All Lists]

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

2007-08-13 07:22:50
On 8/13/07, cknell(_at_)onebox(_dot_)com <cknell(_at_)onebox(_dot_)com> 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

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--

<Prev in Thread] Current Thread [Next in Thread>