Skip to content

Instantly share code, notes, and snippets.

Created February 18, 2013 12:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anonymous/4977069 to your computer and use it in GitHub Desktop.
Save anonymous/4977069 to your computer and use it in GitHub Desktop.
<data>
<products>
<entry>one</entry>
<entry>two</entry>
<entry>three</entry>
<entry>four</entry>
</products>
</data>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" indent="yes" />
<xsl:template match="/">
<hi>
<xsl:value-of select="data/products/entry[position() mod 4 = 1]" mode="group" />
</hi>
</xsl:template>
<xsl:template match="products/entry" mode="group">
<xsl:variable name="group">
<xsl:call-template name="num-to-word">
<xsl:with-param name="num-to-word" select="position()"/>
</xsl:call-template>
</xsl:variable>
<div class="group {$group}">
<xsl:apply-templates select=". | following-sibling::entry[position() &lt; 4]" mode="navigation"/>
</div>
</xsl:template>
<xsl:template match="products/entry | products/entry" mode="product">
<article class="product"></article>
</xsl:template>
<xsl:template name="num-to-word">
<xsl:param name="num"/>
<xsl:choose>
<xsl:when test="$num = 1">one</xsl:when>
<xsl:when test="$num = 2">two</xsl:when>
<xsl:when test="$num = 3">three</xsl:when>
<xsl:when test="$num = 4">four</xsl:when>
...
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment