Search

I would like to output a list with a certain number of list items in xslt
for example dynamicly create 5 listitems in xslt:

<ul>
    <li>listitem1</li>
    <li>listitem2</li>
    <li>listitem3</li>
    <li>listitem4</li>
    <li>listitem5</li>
</ul>

How can i do this?

Not sure if I understand you correctly, but...

<xsl:template match="/">
    <ul>
        <xsl:call-template name="generate">
            <xsl:with-param name="to" select="5" />
        </xsl:call-template>
    </ul>
</xsl:template>

<xsl:template name="generate">
    <xsl:param name="count">1</xsl:param>
    <xsl:param name="to" select="10" />

    <li>item <xsl:value-of select="$count" /></li>

    <xsl:if test="$count &lt; $to">

        <xsl:call-template name="generate">
            <xsl:with-param name="count" select="$count + 1" />
            <xsl:with-param name="to" select="$to" />
        </xsl:call-template>
    </xsl:if>
</xsl:template>

Woah I forgot all about that, well remembered.

(Need to rebuild my blog and make this stuff more easy to read).

Alpacaaa's solution works perfect for my case. Although i am not sure if i understand it fully
looks like the if statement is called untill it reached the set value (5 in this case, mine is a variable value)
The 'to' parameter is now set to 10 but can be anything larger then 1, am i right?

Nick's solution most probably works fine too, but seems a bit more complex, so i just go with alpacaa's solution, thanks!

10 is default, but you can override it specifing the to param while calling the template.

Create an account or sign in to comment.

Symphony • Open Source XSLT CMS

Server Requirements

  • PHP 5.3-5.6 or 7.0-7.3
  • PHP's LibXML module, with the XSLT extension enabled (--with-xsl)
  • MySQL 5.5 or above
  • An Apache or Litespeed webserver
  • Apache's mod_rewrite module or equivalent

Compatible Hosts

Sign in

Login details