|
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?
|
|
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> </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> </xsl:text> </xsl:template>
</xsl:stylesheet>
output
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".
|
|
Rank: Newbie
Joined: 8/27/2010 Posts: 2
|
What happens if you set bar="" rather than eliminate it?
|
|
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.
|
|
guest |