Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 2nd, 2007, 03:41 PM
Registered User
 
Join Date: May 2007
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default format-number function problem

Hi all



i am kind of new to xslt and i am having problems with outputing one of my outputs. my xml file consists of invoices and i have to calculate the sub total for each invoice which i am able to do.



the problem is when i try to format the output to include the dollar currency. the output fails and gives NaN

i used the following peice of code to format my output:

<xsl:value-of select="format-number((($node/quantity*$node/unitPrice) - (($node/discount div 100) * $node/unitPrice *$node/quantity)) + $sum-of-rest,'$####0.00#')"/>

it gives me an output of Nan. But if i dont include the dollar sign. the proper output is given for example it outputs 300.00 but i would lke the output to beign soemthing like this $300.00.



so i dont know what exactky is wrong. i would really appreciate anyones help



kind regards



jeff


 
Old May 2nd, 2007, 03:56 PM
Friend of Wrox
 
Join Date: Jul 2006
Posts: 430
Thanks: 28
Thanked 5 Times in 5 Posts
Send a message via Yahoo to bonekrusher
Default

The $ sign in XSLT means that is a variable. For example

<xsl:variable name="myvar">Hello</xsl:variable>

to use:

<xsl:value-of select="$myvar"/>
 
Old May 2nd, 2007, 04:17 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

format-number() in XSLT 1.0 isn't very well specified. The spec says that the format picture is interpreted as in the DecimalFormat class in JDK 1.1 (there's actually a link to the Sun web site which is now broken). Many Java-based XSLT processors use the implementation of DecimalFormat in whatever version of Java they happen to be running on, which is of course incorrect, because it has changed since 1.1; and one of the things that has changed is the handling of currency signs. Non-Java-based processors do their best to emulate the Java 1.1 behaviour, but that's not easy either as many aspects of the behaviour are left unspecified in JDK 1.1 (though in some cases later releases have improved documentation).

It's hard to tell without pulling the JDK 1.1 spec out of archive storage exactly what the correct behaviour here is, and even if you did, it probably wouldn't give you a clear answer. Simplest solution is to take the "$" out of your picture, and add it into the output using concat().

This is all assuming you are using an XSLT 1.0 processor. XSLT 2.0 has it's own specification of format-number() with no dependency on the JDK specification.

bonekrusher's response is quite irrelevant, by the way. $ does introduce an XPath variable, but not when it's within a string literal.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old May 2nd, 2007, 05:30 PM
Friend of Wrox
 
Join Date: Jul 2006
Posts: 430
Thanks: 28
Thanked 5 Times in 5 Posts
Send a message via Yahoo to bonekrusher
Default

Thanks for the correction :)


 
Old May 3rd, 2007, 04:55 PM
Registered User
 
Join Date: May 2007
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

HI guys thanks for your inputs. oh Michael i tried concatenating the dollar sign with the output value but it still gives the NaN value. funny thing is that if i use the above format with the dollar sign to a a simple value such as that from an element it works fine. ii was wondering if there was any other way

 
Old May 3rd, 2007, 05:07 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

>it still gives the NaN value

In that case the dollar sign wasn't the problem.

What happens if you replace

<xsl:value-of select="format-number(X)"/>

with

<xsl:value-of select="X"/>

?

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old May 4th, 2007, 01:50 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Please produce a cut-down example that other people can run, one with no dependencies on other files, and preferably with all those confusing commented-out lines removed.

It's also useful to say what processor you are using, given that I explained earlier that the problem might be system-dependent. (It's even more useful to try it yourself on more than one processor and report whether they all give the same result...)

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old May 4th, 2007, 05:32 PM
Registered User
 
Join Date: May 2007
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

hi i am sorry for giving such a huge example. anyway i am using xslt version 1 and i have also tried using xslt version 2 but i still get the same output NaN. the code i have given below is a sample version.

the xslt code is:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                version="2.0">

<xsl:template match="/">
  <html>
    <head>
      <title>Invoice</title>
    </head>
    <body>
      <h1>Invoice</h1>


      <table border="1" style="text-align: center">
        <tr>
          <th>Description</th>
          <th>Quantity</th>
          <th>Unit price</th>
          <th>Subtotal</th>
        </tr>
        <xsl:for-each select="invoice/item">
          <tr>
            <td><xsl:value-of select="description"/></td>
            <td><xsl:value-of select="qty"/></td>
            <td><xsl:value-of select="unitPrice"/></td>
            <td><xsl:value-of select="qty * unitPrice"/></td>
          </tr>
        </xsl:for-each>
        <tr>
          <th colspan="3">Total</th>
          <th>

            <xsl:call-template name="add">
              <xsl:with-param name="node" select="invoice/item[1]"/>
            </xsl:call-template>
          </th>
        </tr>
      </table>
    </body>
  </html>
</xsl:template>

<!-- Recursive template. It will calculate total amount
     from the all following items -->
<xsl:template name="add">
  <xsl:param name="node" select="."/>
  <xsl:variable name="sum-of-rest">
    <xsl:choose>
      <xsl:when test="$node/following-sibling::item">
        <xsl:call-template name="add">
          <xsl:with-param name="node"
            select="$node/following-sibling::item[1]"/>
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>0</xsl:otherwise>
    </xsl:choose>
  </xsl:variable>
  <xsl:value-of select="format-number((($node/qty)*($node/unitPrice) + $sum-of-rest),'$##0.00')"/>
</xsl:template>

</xsl:stylesheet>


the xml file is as follows:

<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="invoice-noext.xsl"?>
<invoice>
  <item>
    <description>Pilsner Beer</description>
    <qty>6</qty>
    <unitPrice>1.69</unitPrice>
  </item>
  <item>
    <description>Sausage</description>
    <qty>3</qty>
    <unitPrice>0.59</unitPrice>
  </item>
  <item>
    <description>Portable Barbecue</description>
    <qty>1</qty>
    <unitPrice>23.99</unitPrice>
  </item>
  <item>
    <description>Charcoal</description>
    <qty>2</qty>
    <unitPrice>1.19</unitPrice>
  </item>
</invoice>

kind regards

Jeff

 
Old May 5th, 2007, 03:20 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Running this through Saxon 8.9, I get:

Validation error on line 48 of file:///c:/temp/test.xsl:
  Cannot convert string "$2.38" to a double

The line number isn't particularly helpful, but it shows where the problem lies: you are formatting the intermediate results of the calculation, including a "$" sign in the value, and then trying to convert the formatted results back to a double. Under XSLT 2.0 this fails (or should fail, if it doesn't then your processor isn't conformant). Under 1.0 it gives NaN.

Change the computation so it only applies the formatting (and rounding) to the final result, not to intermediate results.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference





Similar Threads
Thread Thread Starter Forum Replies Last Post
Format Number Dink Classic ASP Databases 2 January 23rd, 2007 01:40 AM
Number Format - Need Help! bridog39 Access 7 November 14th, 2005 02:50 PM
Format Number hcweb Classic ASP Basics 4 October 27th, 2004 11:23 AM
format number tgopal Javascript 2 April 30th, 2004 03:44 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.