IMPORTANT:
this is not a Support Forum! Experienced users might answer from time to time questions posted here. If you need a professional and reliable answer, or if you want to report a bug, please contact Altova Support instead.

format-number error Options · View
begge
Posted: Friday, August 27, 2010 7:29:04 PM
Rank: Newbie

Joined: 8/27/2010
Posts: 2
I am converting from msxml xslt 1.0 to using Altova xslt 2.0 and I have run into a problem with the format-number function.

I have the following xsl code that is causing the error
Code:
<xsl:value-of select="format-number(@Gross, '$#,##0.00', 'blankifempty')"/>



The error is:
EOleException: Error in XPath 2.0 expression at xsl:value-of - select
Cast failed, invalid lexical value - xs:double '' - format-number.



This happens when @Gross is empty. Is there a normal fix for this other than writing a xsl:if to check for blanks?

Martin Honnen
Posted: Saturday, August 28, 2010 10:40:56 AM
Rank: Advanced Member

Joined: 6/10/2007
Posts: 36
I can't reproduce the problem, using "AltovaXML Version 2010 rel. 3 sp1".
XML sample input
Code:
<root>
  <foo bar="2.4"/>
  <foo/>
</root>

Both
Code:

<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0">
 
  <xsl:output method="text"/>
 
  <xsl:template match="foo">
    <xsl:value-of select="format-number(@bar, '$#,##0.00')"/>
    <xsl:text>&#10;</xsl:text>
  </xsl:template>

</xsl:stylesheet>

and the same, only with version="2.0",
Code:
<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="2.0">
 
  <xsl:output method="text"/>
 
  <xsl:template match="foo">
    <xsl:value-of select="format-number(@bar, '$#,##0.00')"/>
    <xsl:text>&#10;</xsl:text>
  </xsl:template>

</xsl:stylesheet>

output
Code:
$2.40
NaN

which is also what http://www.w3.org/TR/xslt20/#format-number suggests, which says "If the supplied value of the $value argument is an empty sequence, the function behaves as if the supplied value were the xs:double value NaN".

begge
Posted: Tuesday, August 31, 2010 12:32:43 PM
Rank: Newbie

Joined: 8/27/2010
Posts: 2
What happens if you set bar="" rather than eliminate it?
Martin Honnen
Posted: Wednesday, September 1, 2010 12:34:39 PM
Rank: Advanced Member

Joined: 6/10/2007
Posts: 36
If the attribute is present but its value is the empty string then I think with version="2.0" there should indeed an error be raised, with version="1.0" and an XSLT 2.0 processor implementing backwards compatibility mode then result should be "NaN". However Altova still raises an error. Saxon 9 does not.
You might want to code
Code:
format-number(if (@bar castable as xs:double) then @bar else number(''), '$#,##0.00')

I think that gives the behaviour you want with an XSLT 2.0 processor, independent of the XSLT version attribute in the stylesheet.
Users browsing this topic
guest

Forum Jump
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

Use of the Altova User Forum(s) is governed by the Altova Terms of Use.