How to do a “while”-like loop in XSLT?

允我心安 提交于 2019-12-06 05:53:47
Emiliano Poggi
<!-- recursive named template -->
<xsl:template name="while">

 <xsl:variable name="VALUE">
 <!-- your evaluation -->
 </xsl:variable>

 <!-- evaluate and recurse -->
 <xsl:if test="$VALUE=0">
    <xsl:call-template name="while"/>
 </xsl:if>

</xsl:template>

Like using a template and calling itself in the end?

Correct: tail recursion is the usual solution to this problem, as illustrated by empo. The better XSLT processors will optimize tail recursion so it doesn't comsume the stack. The worse ones will run out of stack space after 500 or so iterations, in which case you need to look for a different solution.

Note: try to avoid this kind of construct

<xsl:variable name="date">
    <xsl:value-of select="$date - 1" /> 
</xsl:variable>

when you could do

<xsl:variable name="date" select="$date - 1" /> 

It's unnecessarily verbose, and can also be very inefficient because the variable value is a tree rather than a simple string or number.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!