[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"

	<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:template match="/" name="main">
		<xsl:for-each select="reverse(1 to 99)">
			<xsl:value-of select="concat(f:getBottleStr(.), ' of beer on the
			<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;')"/>


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