[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message] Re: FizzBuzz in XSLT 1.0. Help with a 2.0/FXSL solutio
On 3/14/07, M. David Peterson <m.david@xxxxxxxxxx> wrote:
Hey All, I have been quite busy these days, so here's an answer coming somewhat late. The other reason for the delay is that the FXSL infrastructure was not complete for solving a problem of this kind. I had to implement a number of new functions (especially the f:lift<N>() family of functions), which I had planned to do long ago. Right now, FXSL produces the desired result with the following one-liner (err... it is a single expression, to be more precise):
f:choice(pChoice1, pChoice2, pChoice1Val, pActualVal) evaluates to pChoice1 if pActualVal = pChoice1Val and evaluates to pChoice2 otherwise. f:id(x) = x f:const(x,y) = x
The first two functions produce "fizz" or (respectively) "buzz" whenever their argument is divisible by 3 or (respectively) 5. Both functions produce Nothing (the empty string) otherwise. The third function specified for f:multiMap() is a little bit more involved. It is a "lift" of the function f:choice(), which allows its arguments to be functions, that provide the arguments to f:choice(). This is necessary, because when the number is neither divisible by 3 or 5 we are required to produce not a fixed result, but this number itself, which is done by the function f:id(). Let me thank M.David Peterson for this excellent and challenging problem. Here is the complete transformation. To run it one needs to sync from the FXSL CVS for the latest changes:
<xsl:output omit-xml-declaration="yes" indent="yes"/> <xsl:template match="/"> <xsl:variable name="vDelimElem" as="element()"> <map/> </xsl:variable> <multiMap> <xsl:sequence select= "f:multiMap($vDelimElem, (f:compose(f:choice('fizz', '', 0), f:flip(f:mod(),3)), f:compose(f:choice('buzz', '', 0), f:flip(f:mod(),5)), f:lift4(f:choice(), f:const(''), f:id(), f:const((0,3,5,6,9,10,12)), f:flip(f:mod(),15) ) ), 1 to 100)" /> </multiMap> </xsl:template> </xsl:stylesheet> And the result: <multiMap> <map/>1 <map/>2 <map/>fizz <map/>4 <map/>buzz <map/>fizz <map/>7 <map/>8 <map/>fizz <map/>buzz <map/>11 <map/>fizz <map/>13 <map/>14 <map/>fizz buzz <map/>16 <map/>17 <map/>fizz <map/>19 <map/>buzz <map/>fizz <map/>22 <map/>23 <map/>fizz <map/>buzz <map/>26 <map/>fizz <map/>28 <map/>29 <map/>fizz buzz <map/>31 <map/>32 <map/>fizz <map/>34 <map/>buzz <map/>fizz <map/>37 <map/>38 <map/>fizz <map/>buzz <map/>41 <map/>fizz <map/>43 <map/>44 <map/>fizz buzz <map/>46 <map/>47 <map/>fizz <map/>49 <map/>buzz <map/>fizz <map/>52 <map/>53 <map/>fizz <map/>buzz <map/>56 <map/>fizz <map/>58 <map/>59 <map/>fizz buzz <map/>61 <map/>62 <map/>fizz <map/>64 <map/>buzz <map/>fizz <map/>67 <map/>68 <map/>fizz <map/>buzz <map/>71 <map/>fizz <map/>73 <map/>74 <map/>fizz buzz <map/>76 <map/>77 <map/>fizz <map/>79 <map/>buzz <map/>fizz <map/>82 <map/>83 <map/>fizz <map/>buzz <map/>86 <map/>fizz <map/>88 <map/>89 <map/>fizz buzz <map/>91 <map/>92 <map/>fizz <map/>94 <map/>buzz <map/>fizz <map/>97 <map/>98 <map/>fizz <map/>buzz <map/> </multiMap>
|
PURCHASE STYLUS STUDIO ONLINE TODAY!Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced! Download The World's Best XML IDE!Accelerate XML development with our award-winning XML IDE - Download a free trial today! Subscribe in XML format
|