Solving FizzBuzz in XSLT 1.0

Enable the Subscriptions block here!
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 :)