Re: [xsl] Formatting of numbers, dynamically

Subject: Re: [xsl] Formatting of numbers, dynamically
From: David Carlisle <davidc@xxxxxxxxx>
Date: Mon, 11 Mar 2002 22:57:47 GMT

>   My question is how I can dynamically build
>   the formatting string.

format-number() is just an xpath function so you can put any xpath
expression in its arguments, so long as the second argument evaluates to
a string of the right form, so
format-number($node,concat('#,##0.',$foo))
will do the right thing if $foo is set to '00'  or '' or '000' or
whatever you want depending, perhaps you want  the second arg to be
substring('00000000',1,@minor_unit)
for example.

> I was reading this morning in the March 2002 issue of MSDN magazine about
> how XSLT is a "Functional programming language",

well it's a declarative side effect free language. Unfortunately the
XSLT WG appear to have (again) passed on the chance to make it into a
functional language (by making functions first class objects) in teh
draft for XSLT2.

> for(i=0;i<=@minor_unit;i++){ ... };
If you said what ... was someone could show you how to do it
(perhaps I just did, I'm not sure:-) It is possible to write an exact
analogue of the above, as an imperative loop construct is really just 
a syntactic (and unnatural) shorthand for the equivalent recursive
function call, but often you can use the built in facilities of XSLT to
avoid explictly writing the recursion.
The weirdness above with i changing value only seems natural if you have
been corrupted by imperative programming. A more natural way to describe
the algorithm would be

if @minor_unit=0 stop
else
 do ... then do it again with @minor_unit one less.

this is the standard recursive function call expression of a loop, which
you'd do in any declarative programming language, XSLT included.
I've avoided the XSLT syntax though as it's in the archives and 
the XSLT syntax for parameter passing is horribly verbose, even by XML
standards. 

David

_____________________________________________________________________
This message has been checked for all known viruses by Star Internet
delivered through the MessageLabs Virus Scanning Service. For further
information visit http://www.star.net.uk/stats.asp or alternatively call
Star Internet for details on the Virus Scanning Service.

 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread