Skip to content

Instantly share code, notes, and snippets.

@pdaengeli
Last active July 26, 2017 21:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pdaengeli/fd9050284a96e5d0aacacf617a8e789c to your computer and use it in GitHub Desktop.
Save pdaengeli/fd9050284a96e5d0aacacf617a8e789c to your computer and use it in GitHub Desktop.
99 bottles of beer
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:fn="https://gist.github.com/pdaengeli/fd9050284a96e5d0aacacf617a8e789c"
exclude-result-prefixes="xs"
version="3.0">
<!-- 99 bottles of beer (http://www.99-bottles-of-beer.net), XSLT 3.0 -->
<!-- https://gist.github.com/pdaengeli/fd9050284a96e5d0aacacf617a8e789c -->
<xsl:output method="text"/>
<xsl:variable name="thirst" select="100"/>
<xsl:template match="/">
<xsl:for-each select="1 to $thirst">
<xsl:value-of select="fn:sing(current(),$thirst)"/>
</xsl:for-each>
</xsl:template>
<xsl:function name="fn:sing" expand-text="true">
<xsl:param name="current"/>
<xsl:param name="thirst"/>
<xsl:variable name="Σ" select="if ($thirst - $current gt 0)
then $thirst - $current else 'No more'"/>
<xsl:variable name="s" select="if (number($Σ) = 1) then '' else 's'"/>
<xsl:variable name="beer" select="concat('bottle',$s,' of beer')"/>
<xsl:variable name="wall" select="'on the wall'"/>
<xsl:choose>
<xsl:when test="number($Σ) gt 0">
{$Σ,$beer,$wall||', '||$Σ,$beer||'.
Take one down and pass it around, '||$Σ,$beer,$wall||'.'}
</xsl:when>
<xsl:otherwise>
{$Σ,$beer,$wall||', no more '||$beer||'.
Go to the store and buy some more, '||$thirst -1,$beer,$wall||'.'}
</xsl:otherwise>
</xsl:choose>
</xsl:function>
</xsl:stylesheet>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment