Search

Someone showed me this site today and I thought it would interesting to share.

The challenge is to write the old 99 bottles rhyme in as little code as possible.

The song's simple lyrics are as follows:

Ninety-nine bottles of beer on the wall, Ninety-nine bottles of beer. Take one down, pass it around, Ninety-eight bottles of beer on the wall

The same verse is repeated, each time with one fewer bottle. The song is completed when the singer or singers reach zero.

So, could you come up a more elegant solution then then example below?

<?xml version="1.0"?>

<!-- 
     XSLT version of 99 Bottles of Beer
     author: Bernd Sokolowsky <ulmo@garozzo.franken.de>
     Erlangen/Germany, 11/2001
-->

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                version="1.0">

<xsl:variable name="newline">
<xsl:text>
</xsl:text>
</xsl:variable>

<xsl:template match="/">
    <xsl:call-template name="loop"/>
</xsl:template>

<xsl:template name="bottles">
    <xsl:param name="bottles"/>

    <xsl:choose>
        <xsl:when test="$bottles = 0">
            <xsl:text>no bottles</xsl:text>
        </xsl:when> 
        <xsl:when test="$bottles = 1">
            <xsl:text>1 bottle</xsl:text>
        </xsl:when> 
        <xsl:otherwise>
            <xsl:value-of select="$bottles"/>
            <xsl:text> bottles</xsl:text>
        </xsl:otherwise>
     </xsl:choose>

</xsl:template>

<xsl:template name="loop">
    <xsl:param name="n" select="99"/>

    <xsl:call-template name="bottles">
        <xsl:with-param name="bottles" select="$n"/>
    </xsl:call-template>

    <xsl:text> of beer on the wall,</xsl:text>
    <xsl:value-of select="$newline"/>

    <xsl:call-template name="bottles">
        <xsl:with-param name="bottles" select="$n"/>
    </xsl:call-template>

    <xsl:text> of beer,</xsl:text>
    <xsl:value-of select="$newline"/>

    <xsl:text>take one down, pass it around,</xsl:text>
    <xsl:value-of select="$newline"/>

    <xsl:call-template name="bottles">
        <xsl:with-param name="bottles" select="$n - 1"/>
    </xsl:call-template>

    <xsl:text> of beer on the wall.</xsl:text>
    <xsl:value-of select="$newline"/>

    <xsl:if test="$n &gt; 1">
        <xsl:value-of select="$newline"/>
        <xsl:call-template name="loop">
            <xsl:with-param name="n" select="$n - 1"/>
        </xsl:call-template>
    </xsl:if>

</xsl:template>
</xsl:stylesheet>

Let the games begin!

Take this:

<xsl:template match="/">
    <xsl:call-template name="loop" />

    <xsl:text>2 bottles of beer on the wall, 2 bottles of beer,</xsl:text><br />
    <xsl:text>Take one down and pass it around, 1 bottle of beer on the wall.</xsl:text><br /><br />

    <xsl:text>1 bottle of beer on the wall, 1 bottle of beer,</xsl:text><br />
    <xsl:text>Take one down and pass it around, no bottles of beer on the wall.</xsl:text><br /><br />

    <xsl:text>No more bottles of beer on the wall, no more bottles of beer.</xsl:text><br />
    <xsl:text>Go to the store and buy some more, 99 bottles of beer on the wall.</xsl:text><br /><br />
</xsl:template>

<xsl:template name="loop">
    <xsl:param name="n" select="99"/>

    <xsl:value-of select="concat($n ,' bottles of beer on the wall, ', $n ,' bottles of beer,')" /><br />
    <xsl:value-of select="concat('Take one down, pass it around, ', $n - 1 ,' bottles of beer on the wall.')" /><br /><br />

    <xsl:if test="$n &gt; 3">
        <xsl:call-template name="loop">
            <xsl:with-param name="n" select="$n - 1"/>
        </xsl:call-template>
    </xsl:if>
</xsl:template>

Haha, nice challenge :)

Looks like you've won hands down Vlad!

I thought this would be a good way to show how awesome XSL is in handling recursion etc.

Neat game indeed.

Awesome! Great idea. We need to have more of these.

Create an account or sign in to comment.

Symphony • Open Source XSLT CMS

Server Requirements

  • PHP 5.3-5.6 or 7.0-7.3
  • PHP's LibXML module, with the XSLT extension enabled (--with-xsl)
  • MySQL 5.5 or above
  • An Apache or Litespeed webserver
  • Apache's mod_rewrite module or equivalent

Compatible Hosts

Sign in

Login details