Friday, February 10, 2012

Implementing incremental logic in XSLT

I faced difficult time to implement the incremental logic for xslt 1.0 today..My requirement was to printout consequent numbers upto a certain value.
Here is my sample segment xslt code for this..

  <xsl:element name="Numbers">
                              <xsl:for-each select="//flight_info">
                              <xsl:if test="position()= last()">
                                  <xsl:call-template name="incrementValue">
                                      <xsl:with-param name="i"  select="1"/>                                                                    
                                     <xsl:with-param name="pos" select="position()"/>
                                </xsl:call-template>
                                </xsl:if>                                     
                             </xsl:for-each>
     </xsl:element>


//This template does the trick, which contains the recursive call to the same template..

    <xsl:template name="incrementValue">
        <xsl:param name="i" />
        <xsl:param name="pos" />

        <xsl:if test="$i <= $pos">
            <xsl:value-of select="concat($i,' ')" />
            <xsl:call-template name="incrementValue">
                <xsl:with-param name="i" select="$i + 1" />
                <xsl:with-param name="pos" select="$pos" />
            </xsl:call-template>
        </xsl:if>
    </xsl:template>

No comments:

Post a Comment