Blog
Recursion in XSLT
2008-04-09 23:52:48 by Martynas Jusevičius
XSLT, which is great for web templating, is a functional language and has no loops or mutable variables. These constructs have to be replaced with recursion and parameters. For example, if you would like to loop until a certain number and print the counter, you would have to use recursion.
Recursion in XSLT is implemented using <xsl:apply-templates> or <xsl:call-template> together with <xsl:with-param> and <xsl:param>.
The general logic is to call or apply a template with some initial parameter value, get the necessary work done inside of the template, and let it call itself with a modified parameter. Considering the loop example, it would mean calling the template with 0 as initial counter parameter, printing it out in the template, and calling the template recursively with counter increased by one with the condition that it still is smaller than the target number.
As a complete example we provide a template for converting line breaks in a string into HTML <br/> elements:
<xsl:template name="process-line-breaks">
<xsl:param name="text"/>
<xsl:variable name="break" select="' '"/> <!-- entity for line break character, might differ according to platform -->
<xsl:choose>
<xsl:when test="contains($text, $break)">
<xsl:value-of select="substring-before($text, $break)"/>
<br/>
<xsl:call-template name="process-line-breaks">
<xsl:with-param name="text" select="substring-after($text, $break)"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$text"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>The template takes a text string as a parameter and tries to locate a line break in it. If that suceeds, the string part before the break is the output, followed by a <br/>, and the template is applied recursively on the string part after the break. If there are no more breaks in the string parameter, the remaining string becomes the output and the template terminates. In each step the template is left with a smaller and smaller remainder of the original string, while the preceding part becomes template output with line breaks replaced.
The template can be applied on a text content like this:
<xsl:template match="text()" mode="process-line-breaks">
<xsl:call-template name="process-line-breaks">
<xsl:with-param name="text" select="."/>
</xsl:call-template>
</xsl:template>Comments (11)
No loops?
http://www.w3schools.com/xsl/xsl_for_each.asp
2008-04-10 11:40:08 by richard miller
Try doing the above with for-each
2008-04-10 12:12:14 by Thierry
Stupid me !
I was trying to achieve this just yesterday, and did not even thought of this.
I am designing a new site with xml+xsl, and when outputting text saved from the db, could not reproduce the page breaks. The <br/> becoming <br/>
Thank you for your illuminating post !
2008-04-10 16:07:54 by J McGowan
And don't forget about xsl:function.
In my experience problems like the one above are suited well to the use of xsl:function instead of a named template.
The overhead for calling an xsl:function is much nicer than that of a named template with parameters.
J,
2008-04-10 21:41:48 by Martynas
I guess there even more nice features in 2.0, but so far I'm stuck with XSLT 1.0 :)
citrix exam
I really lke Your post
cissp certification
Amazing post ....
a+ certification
WOW..I really like your post
security+
Amazi post
ugg boots
i like<a href="http://www.buyuggsite.com ">ugg boots</a>,<a href="http://www.buyuggsite.com/ugg-argyle-knit-5879-boots-c-14.html ">uggs</a>,<a href="http://www.buyuggsite.com ">ugg australia</a>,<a href="http://www.buyuggsite.com ">ugg classic</a>,<a href="http://www.buyuggsite.com/ugg-argyle-knit-5879-boots-c-14.html ">ugg boots uk</a>,especlially like<a href="http://www.buyuggsite.com ">ugg classic tall</a><a href="http://www.buyuggsite.com/ugg-bailey-button-5803-boots-c-35.html ">tall ugg boots</a><a href="http://www.buyuggsite.com/ugg-bailey-button-5803-boots-c-35.html ">short ugg boots</a><a href="http://www.buyuggsite.com/ugg-classic-cardy-5819-boots-c-8.html ">uggs sale</a><a href="http://www.buyuggsite.com ">buy ugg</a><a href="http://www.buyuggsite.com/ugg-classic-cardy-5819-boots-c-8.html ">buy ugg boots</a><a href="http://www.buyuggsite.com/ugg-classic-crochet-5833-boots-c-1.html ">discount ugg</a><a href="http://www.buyuggsite.com/ugg-classic-crochet-5833-boots-c-1.html ">buy uggs</a><a href="http://www.buyuggsite.com/ugg-classic-mini-5854-boots-c-10.html ">ugg boots online</a><a href="http://www.buyuggsite.com/ugg-classic-mini-5854-boots-c-10.html ">discount uggs</a><a href="http://www.buyuggsite.com/ugg-classic-short-5825-boots-c-9.html ">buy cheap ugg boots</a>
replica watches
nice post

2008-04-10 03:52:51 by Daniel O'Connor