|
Subject:
|
format-number in a template
|
|
Posted By:
|
htran
|
Post Date:
|
4/26/2005 2:20:58 PM
|
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.
|
|
Reply By:
|
mhkay
|
Reply Date:
|
4/26/2005 2:58:25 PM
|
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
|
|
Reply By:
|
htran
|
Reply Date:
|
4/26/2005 4:11:03 PM
|
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?
|
|
Reply By:
|
mhkay
|
Reply Date:
|
4/26/2005 4:32:31 PM
|
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
|
|
Reply By:
|
htran
|
Reply Date:
|
4/27/2005 11:31:37 AM
|
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.
|