Skip to content

Instantly share code, notes, and snippets.

@nils-werner
Created May 22, 2010 17:31
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save nils-werner/410225 to your computer and use it in GitHub Desktop.
XSLT Loop
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
exclude-result-prefixes="xsl">
<xsl:template name="loop"> <!-- Loop -->
<xsl:param name="i" />
<xsl:param name="limit" />
<xsl:call-template name="loop-item">
<xsl:with-param name="i" select="$i"/>
<xsl:with-param name="limit" select="$limit"/>
</xsl:call-template>
<xsl:choose>
<xsl:when test="$i &lt; $limit">
<xsl:call-template name="loop">
<xsl:with-param name="i" select="$i + 1" />
<xsl:with-param name="limit" select="$limit" />
</xsl:call-template>
</xsl:when>
<xsl:when test="$i &gt; $limit">
<xsl:call-template name="loop">
<xsl:with-param name="i" select="$i - 1" />
<xsl:with-param name="limit" select="$limit" />
</xsl:call-template>
</xsl:when>
</xsl:choose>
</xsl:template>
<xsl:template name="loop-item"> <!-- Empty Loop Item, has to be overloaded -->
<xsl:param name="i" />
<xsl:param name="limit" />
</xsl:template>
</xsl:stylesheet>
<xsl:template match="data">
<xsl:apply-templates select="your-ds" />
</xsl:template>
<xsl:template match="your-ds">
<h2><xsl:value-of select="section" /></h2>
<xsl:call-template name="years" />
</xsl:template>
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
exclude-result-prefixes="xsl">
<xsl:import href="../utilities/loop.xsl"/>
<xsl:template name="years"> <!-- Years-wrapper for Loop -->
<xsl:param name="from" select="$this-year" />
<xsl:param name="to" select="'1990'" />
<xsl:call-template name="loop">
<xsl:with-param name="i" select="$from" />
<xsl:with-param name="limit" select="$to" />
</xsl:call-template>
</xsl:template>
<xsl:template name="years-item"> <!-- Years-Item, called by loop item -->
<xsl:param name="i" />
<xsl:param name="limit" />
<xsl:if test="entry[substring(datum, 1, 4) = $i]">
<h3><xsl:value-of select="$i" /></h3>
<ul>
<xsl:apply-templates select="entry[substring(datum, 1, 4) = $i]"/>
</ul>
</xsl:if>
</xsl:template>
<xsl:template name="loop-item"> <!-- Overload for Loop Item -->
<xsl:param name="i" />
<xsl:param name="limit" />
<xsl:call-template name="years-item">
<xsl:with-param name="i" select="$i" />
<xsl:with-param name="limit" select="$limit" />
</xsl:call-template>
</xsl:template>
</xsl:stylesheet>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment