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 September 15th, 2004, 05:11 AM
Authorized User
 
Join Date: Sep 2004
Posts: 36
Thanks: 0
Thanked 0 Times in 0 Posts
Default Splitting numbers in XSL

I have a requirement to split a number using XSL into two parts. The integer part and decimal part.

If my initial XML element was <NUMBER>12.99</NUMBER>
I need my new elements to be:

<NUMBERINT>12</NUMBERINT>
<NUMBERDEC>99</NUMBERDEC>

I tried using the following XSL but thought there must be a better way.

<NUMBERINT>
    <xsl:for-each select="//NUMBER">
         <xsl:choose>
             <xsl:value-of select="substring(format-number(//NUMBER*100, '#.##'), 1, string-length(format-number(//NUMBER*100, '#.##'))-3)" />
         </xsl:choose>
    </xsl:for-each>
</NUMBERINT>
<NUMBERDEC>
   <xsl:for-each select="//NUMBER">
        <xsl:choose>
            <xsl:value-of select="substring(format-number(//NUMBER*100, '#.##'), string-length(format-number(//NUMBER*100, '#.##'))-1, string-length(format-number(//NUMBER*100, '#.##'))-3)" />
        </xsl:choose>
   </xsl:for-each>
</NUMBERDEC>

If anyone has any ideas i'd really appreciate your knowledge.

Cheers,
Francis


 
Old September 15th, 2004, 05:25 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Firstly, your use of "//" is quite wrong. //NUMBER selects all the numbers in the document. You don't want to process all the numbers while generating one <NUMBERINT> result element, let alone doing it again in an inner loop.

Also, your use of xsl:choose is wrong. The only things allowed inside xsl:choose are xsl:when and xsl:otherwise.

There are two ways you can break the number up: using string manipulation or using arithmetic. For example:

<xsl:template match="NUMBER">
<NUMBERINT>
  <xsl:value-of select="substring-before(., '.')"/>
</NUMBERINT>
<NUMBERDEC>
  <xsl:value-of select="substring-after(., '.')"/>
</NUMBERDEC>
</xsl:template>

or if you prefer:

<xsl:template match="NUMBER">
<NUMBERINT>
  <xsl:value-of select="floor(.)"/>
</NUMBERINT>
<NUMBERDEC>
  <xsl:value-of select=". mod 1"/>
</NUMBERDEC>
</xsl:template>

These two solutions might not do quite the same thing if the numbers are negative.

Michael Kay


Michael Kay
http://saxon.sf.net/
 
Old September 15th, 2004, 05:34 AM
Authorized User
 
Join Date: Sep 2004
Posts: 36
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you Mike.

That is much neater.






Similar Threads
Thread Thread Starter Forum Replies Last Post
XSL Fo: Forcing page numbers ashishb2281 XSLT 0 August 1st, 2008 08:43 AM
How to remove numbers in XML files using xsl srkumar XSLT 1 April 15th, 2008 06:43 AM
adding up 2 numbers in xsl umair.aziz XSLT 3 July 14th, 2006 09:24 AM
regarding adding numbers in xsl umair.aziz XSLT 1 June 24th, 2006 12:12 PM
XSL Transform with xsl string NOT xsl file skin XSLT 0 June 16th, 2003 07:30 AM





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