Subject: Re: Processing based on number - alternatives to recursion?
From: "Vyacheslav Sedov" <vyacheslav.sedov@xxxxxxxxx>
Date: Tue, 4 Mar 2008 19:14:04 +0300
|
xslt 2.0
<xsl:for-each select="1 to .">
...
</xsl:for-each>
On Tue, Mar 4, 2008 at 7:10 PM, Michael Ludwig <mlu@xxxxxxxxxxxxx> wrote:
> I have a numeric value in my XML and want to use it to control
> some processing, say counting up until the number is reached, and
> outputting a line in the process.
>
> $ cat xtimes.xml
> <Urmel>10</Urmel>
>
> I use recursion and everything works fine. Is recursion the
> preferred way to use a number to steer processing in XSL 1.0?
>
> $ cat xtimes.xsl
> <xsl:transform version="1.0"
> xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
> <xsl:output method="text"/>
> <xsl:template match="Urmel"><!-- match element -->
> <xsl:call-template name="rec">
> <xsl:with-param name="iter" select="1"/>
> </xsl:call-template>
> </xsl:template>
> <xsl:template name="rec"><!-- recursive template -->
> <xsl:param name="iter"/>
> <xsl:if test="not( $iter > . )"><!-- Greater than me? -->
> <xsl:value-of select="concat( $iter, ' ' )"/>
> <xsl:call-template name="rec"><!-- Then recurse. -->
> <xsl:with-param name="iter" select="$iter + 1"/>
> </xsl:call-template>
> </xsl:if>
> </xsl:template>
> </xsl:transform>
>
> In order to use xsl:for-each, I'd have to dispose of something
> generating a node-set based on my number, wouldn't I?
>
> Is there anything like that in XSL 1.0 or 2.0?
>
> Would that somehow be better?
>
> Or is this misguided optimization thinking in thinking that lots of
> template invocations are bad?
>
> Michael
|