Solving FizzBuzz in XSLT 1.0

The problem:
Write a program that prints the numbers from 1 to 100. But for multiples of three print “Fizz” instead of the number and for the multiples of five print “Buzz”. For numbers which are multiples of both three and five print “FizzBuzz”.

While many have showcased how they would go about it in their language of choice, each one of them seems to have overlooked the fact that each of their solutions is about as dynamic, and therefore reusable, as static cling. Once the "cling" has gone: Forget it, it ain't coming back without a recharge. So then this leads me to the following,

In the real world, things change. For example, data. So then what happens when the values for the problem change? Well with a hard-coded (read static) solution, you have to recharge by rewriting your code to accommodate. With a soft-coded (read dynamic) solution, you don't.

Given the fact that XSLT 2.0 has only recently surfaced as a W3C recommendation, and therefore the code to provide this same functionality would seem a bit foreign to most (though it would be quite a bit smaller and more efficient) to showcase my point I'll utilize XSLT 1.0. Of course, to a lot of folks, XSLT 1.0 will probably still look a bit foreign, but none-the-less, given there are more XSLT 1.0 processors out there, and given that XSLT 1.0 has been around for nearly seven years, theres a better chance this code can be -- you know -- reused, and as such, it seems to make more sense to write this in XSLT 1.0.

As such,

The XML,


<?xml version="1.0" encoding="UTF-8"?>
<fizzbuzz>
    <range>1-100</range>
    <test>
        <mod value="3" test="0">Fizz</mod>
        <mod value="5" test="0">Buzz</mod>
    </test>
</fizzbuzz>

The XSLT,


<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    
    <xsl:output method="text"/>
    
    <xsl:template match="/fizzbuzz">
        <xsl:call-template name="fizzbuzz">
            <xsl:with-param name="range.start" select="substring-before(range, '-')"/>
            <xsl:with-param name="range.end" select="substring-after(range, '-')"/>
            <xsl:with-param name="test" select="test/mod"/>
        </xsl:call-template>
    </xsl:template>
    
    <xsl:template name="fizzbuzz">
        <xsl:param name="range.start"/>
        <xsl:param name="range.end"/>
        <xsl:param name="test"/>
        <xsl:param name="count" select="$range.start"/>
        <xsl:if test="$count &lt;= $range.end">
            <xsl:variable name="output">
                <xsl:apply-templates select="$test">
                    <xsl:with-param name="count" select="$count"/>
                </xsl:apply-templates>
            </xsl:variable>
            <xsl:choose>
                <xsl:when test="string-length($output) > 0">
                    <xsl:value-of select="$output"/>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="$count"/>
                </xsl:otherwise>
            </xsl:choose>
            <xsl:text>
</xsl:text>
            <xsl:call-template name="fizzbuzz">
                <xsl:with-param name="count" select="$count + 1"/>
                <xsl:with-param name="range.end" select="$range.end"/>
                <xsl:with-param name="test" select="$test"/>
            </xsl:call-template>
        </xsl:if>
    </xsl:template>
    
    <xsl:template match="mod">
        <xsl:param name="count"/>
        <xsl:if test="$count mod @value = @test">
            <xsl:value-of select="."/>
        </xsl:if>
    </xsl:template>
    
</xsl:stylesheet>

The Result?


1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz
16
17
Fizz
19
Buzz
Fizz
22
23
Fizz
Buzz
26
Fizz
28
29
FizzBuzz
31
32
Fizz
34
Buzz
Fizz
37
38
Fizz
Buzz
41
Fizz
43
44
FizzBuzz
46
47
Fizz
49
Buzz
Fizz
52
53
Fizz
Buzz
56
Fizz
58
59
FizzBuzz
61
62
Fizz
64
Buzz
Fizz
67
68
Fizz
Buzz
71
Fizz
73
74
FizzBuzz
76
77
Fizz
79
Buzz
Fizz
82
83
Fizz
Buzz
86
Fizz
88
89
FizzBuzz
91
92
Fizz
94
Buzz
Fizz
97
98
Fizz
Buzz

Now wasn't that fun! If your response was,

1: "Yes! Let's do it again!" My response is: Fantastic! Stay tuned...

2: "Quit smoking the crack, you phreak!" > My response is: Bite me! ;-)

Whether response 1 or response 2: Enjoy the rest of your dev-days! More fun to follow...

Bye for now :)

Overkill?

Besides being an extremely verbose way to do this, how is it different from defining variables for 'Fizz', 'Buzz', 1 and 100 in a separate file (pick your language) and then using those variables in the actual code?

I can see it being quite a bit smaller, cleaner and more efficient than doing this with XML and XSLT.

Is your usage here the appropriate thing to do with XML and XSLT or is there a better example along the same vein that has better real world application?

I'd be interested in seeing the XSLT 2.0 version just to see how much cleaner it would be!

Ask and ye shall receive :)

http://dev.aol.com/blog/mdavidpeterson/2007/03/14/fizz-buzz-in-xslt-2.0

So to you're point: Overkill?

In XSLT 1.0, you're right... It's incredibly verbose. In fact, one of the primary complaints in regards to XSLT 1.0 is that it requires quite a bit of code to do things that would otherwise take quite a bit less code. In XSLT 2.0, as you will see, that problem is now fixed.

As to the point of the exercise, you are correct: This isn't something that couldn't have been solved in any number of languages using dynamically fed variables. In fact, if you hard coded this in XSLT 1.0, it would have been all of about 10 lines of code. The verboseness is a direct side effect of making things more agile. The point to all of this, however, is not "look how cool XSLT is" and instead "if we are going to be solving problems, lets solve them properly, using real world scenarios" as opposed to "here's a pretend problem, give me your pretend answer" which is obviously a bit of an overstatement, but in many ways that's exactly what problems such as this represent.

I should also point out that this post is a lead-in to another, something in which will make quite a bit more sense as to why I used a very simplified example (the problem, not necessarily the solution) to showcase the idea of using dynamic code processing to process dynamic data.

Stay tuned for that, and in the mean time, please see the above link for how this would be solved in XSLT 2.0 using a much friendlier, less verbose solution.

--
/M:D

M. David Peterson
http://mdavid.name | http://www.oreillynet.com/pub/au/2354 | http://dev.aol.com/blog/3155

quite cool!

I think it's quite cool. In fact, cool enough to get me to pull out my old XSLT text books and start thinking about creating XSL transforms for data output by some of the AOL APIs. That could be very interesting!

Once again, we are on the same wave length...

I'll let the subject line speak for itself, and let you fill in the blank from here as to what I mean using my above response to xchilly and what you know in regards to the other items I am working on. ;-)

--
/M:D

M. David Peterson
http://mdavid.name | http://www.oreillynet.com/pub/au/2354 | http://dev.aol.com/blog/3155