469,271 Members | 1,014 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,271 developers. It's quick & easy.

XSL: Looping over non xml set

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 ?
Jul 20 '05 #1
3 11425
> 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,
--
Joris Gillis (http://www.ticalc.org/cgi-bin/acct-v...i?userid=38041)
Ceterum censeo XML omnibus esse utendum
Jul 20 '05 #2
Joris Gillis wrote:
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,


that's a smart solution!
Jul 20 '05 #3

"Joris Gillis" <ro**@pandora.be> writes:
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:


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
Jul 20 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by bearclaws | last post: by
11 posts views Thread by Rolf Barbakken | last post: by
4 posts views Thread by freefly_xml | last post: by
5 posts views Thread by the_jos | last post: by
2 posts views Thread by NISHIL | last post: by
reply views Thread by zamba | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.