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 April 26th, 2005, 02:20 PM
Registered User
 
Join Date: Apr 2005
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default format-number in a template

Hi,

I have a stylesheet that has many entries as follows:
<xsl:value-of select="format-number(number(curAmount1), '###,##0.00')" />
<xsl:value-of select="format-number(number(curAmount2), '###,##0.00')" />
<xsl:value-of select="format-number(number(curAmount3), '###,##0.00')" />

I would like to achive the following without having to do a change all in the xsl stylesheet
<xsl:value-of select="format-number(number(curAmountX), '### ##0,00;(### ##0,00)',' fmCurrency')"/>
where fmCurrency
<xsl:decimal-format name=" fmCurrency" decimal-separator="," grouping-separator=" "/>


So I would like to replace this string
###,##0.00
with
### ##0,00;(### ##0,00)','fmCurrency
by including an external stylesheet (<xsl:include …) using templates

I do not want to use templates with xsl:param as following
<xsl:template name="fmToCurrency">
    <xsl:param name="amt"/>
    <xsl:value-of select="format-number($amt , '# ##0,00;(# ##0,00)', ‘fmCurrency’)"/>
</xsl:template>
because I would have to change in the xsl all reference to currency with <xsl:call-template name=" fmToCurrency ">
<xsl:with-param name="amt" (so I might as well to a change all in the xsl)

any idea?

Thanks.

 
Old April 26th, 2005, 02:58 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Just define the format in a global variable (or even a global param)

<xsl:param name="format" select="'# ##0,00;(# ##0,00)'"/>

and pick it up using

  format-number($amt, $format, 'fmCurrency')

You can override the value of the variable or param in an importing stylesheet.

(or have I misunderstood something?)

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old April 26th, 2005, 04:11 PM
Registered User
 
Join Date: Apr 2005
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I am using a software that generates my xsl. The software generates an xsl with <xsl:value-of select="format-number(number(curAmount1), '###,##0.00')" />

If I do what you suggested, wouldn’t I have to do a change all in my xsl
With '###,##0.00' replaced by $format, 'fmCurrency ? If this is the case, then it would be the same as doing a change all from ###,##0.00 to ### ##0,00;(### ##0,00)','fmCurrency

What I’d like to do is have a template (include_currency_change ) xsl
containing this line
<xsl:decimal-format name=" fmCurrency" decimal-separator="," grouping-separator=" "/>
and something that would match ###,##0.00 from the generated xsl and convert it to the $format, 'fmCurrency that you have suggested.

So that everytime I generate the xsl with the software, all I have to do is include in that file my template to change all occurrence of currency instead of doing a change all.
<xsl:include href="include_currency_change.xsl"/>

Does this make any sense?

 
Old April 26th, 2005, 04:32 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

No, your idea won't work. A template within a stylesheet cannot modify the code of the stylesheet.

When you use a tool to generate XSLT (or any other language) you ave to accept that either (a) you are restricted to the subset of XSLT that the tool can generate, or (b) you are going to have to modify the generated XSLT code, giving consequent maintenance difficulties. I personally have a strong dislike of program generators for that reason.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old April 27th, 2005, 11:31 AM
Registered User
 
Join Date: Apr 2005
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I figure if you can change an xml to modify it's processing instructions and add element and attributes that you could do the same with a template in external xsl.

Right tools help build things faster. Not using the tool would be like cutting wood with hand saw instead of an electric one. I don't use notepad to write code because of time issue.

Thank you Mh global variable will work just fine!
My bad. Doing a change all to ### ##0,00;(### ##0,00)','fmCurrency is not the same as a change all to $format,'fmCurrency. In the sense that should the format change only one place in one file need to be changed.

Thank you.






Similar Threads
Thread Thread Starter Forum Replies Last Post
help me to change number format? haarishr Pro VB 6 4 June 20th, 2008 06:30 AM
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.