xsl-list
[Top] [All Lists]

[xsl] 99 bottles of beer

2007-02-05 04:30:16
After seeing M. David's post about the bottles of beer problem, I
thought about how to solve this problem using XSLT 2.0.  Here's what
came to mind first:

<xsl:stylesheet version="2.0"
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
        xmlns:xs="http://www.w3.org/2001/XMLSchema";
        xmlns:f="f"
        exclude-result-prefixes="xs">

        <xsl:function name="f:getBottleStr" as="xs:string">
                <xsl:param name="count" as="xs:integer"/>
                <xsl:sequence select="concat($count, ' bottle', if ($count = 1) 
then
'' else 's ')"/>
        </xsl:function>
        
        <xsl:template match="/" name="main">
                <xsl:for-each select="reverse(1 to 99)">
                        <xsl:value-of select="concat(f:getBottleStr(.), ' of 
beer on the
wall.&#xa;')"/>
                        <xsl:value-of select="concat(f:getBottleStr(.), ' of 
beer.&#xa;Take
one down, pass it around,&#xa;')"/>
                        <xsl:value-of select="concat(f:getBottleStr(. - 1), ' 
of beer on
the wall.&#xa;')"/>
                </xsl:for-each>
        </xsl:template>

</xsl:stylesheet>

This demonstrates a few nice 2.0 features: the "to" operator for 1 to
99, sequences of items, custom functions, if-then-else, strong typing
and of course it's "standalone" and doesn't require token input file.

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--

<Prev in Thread] Current Thread [Next in Thread>