[xsl] 99 bottles of beer

Subject: [xsl] 99 bottles of beer
From: "Andrew Welch" <andrew.j.welch@xxxxxxxxx>
Date: Mon, 5 Feb 2007 11:29:47 +0000
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.

Current Thread