XSL: Looping over non xml set

T

Tjerk Wolterink

Hello,

I want to create m elements in this form:

<element index="n"/>

Where n = 1 2 .. m

Can is do this with an xsl:for loop??
And if so how

For example for m = 4

<xsl:variable name="m" select="4"/>
<xsl:for-each select="1 to m">
<element index="{position()}"/>
</foreach>

Output:

<element index="1"/>
<element index="2"/>
<element index="3"/>
<element index="4"/>

Is this possible with xslt 1.0 ?
 
J

Joris Gillis

Hello,
I want to create m elements in this form:

<element index="n"/>

Where n = 1 2 .. m

Can is do this with an xsl:for loop??
And if so how

For example for m = 4

<xsl:variable name="m" select="4"/>
<xsl:for-each select="1 to m">
<element index="{position()}"/>
</foreach>

Output:

<element index="1"/>
<element index="2"/>
<element index="3"/>
<element index="4"/>

Is this possible with xslt 1.0 ?
That's only possible in version 2.0
In XSLT 1.0, you must use a named attribute approach:

<xsl:call-template name="loop">
<xsl:with-param name="count">4</xsl:with-param>
</xsl:call-template>

<xsl:template name="loop">
<xsl:param name="count"/>
<xsl:param name="iteration">1</xsl:param>
<element index="{$iteration}"/>

<xsl:if test="$iteration &lt; $count">
<xsl:call-template name="loop">
<xsl:with-param name="count" select="$count"/>
<xsl:with-param name="iteration" select="$iteration +1"/>
</xsl:call-template>
</xsl:if>

</xsl:template>

regards,
 
T

Tjerk Wolterink

Joris said:
That's only possible in version 2.0
In XSLT 1.0, you must use a named attribute approach:

<xsl:call-template name="loop">
<xsl:with-param name="count">4</xsl:with-param>
</xsl:call-template>

<xsl:template name="loop">
<xsl:param name="count"/>
<xsl:param name="iteration">1</xsl:param>
<element index="{$iteration}"/>

<xsl:if test="$iteration &lt; $count">
<xsl:call-template name="loop">
<xsl:with-param name="count" select="$count"/>
<xsl:with-param name="iteration" select="$iteration +1"/>
</xsl:call-template>
</xsl:if>

</xsl:template>

regards,

that's a smart solution!
 
D

David Carlisle

Joris Gillis said:
That's only possible in version 2.0
In XSLT 1.0, you must use a named attribute approach:

Or so long as you know your soource document has enough nodes,

<xsl:for-each select="(//node())[position() &lt;=$m]">
<element index="{position()}"/>
</foreach>

David
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top