xsl-list
[Top] [All Lists]

Re: a number of values

2002-12-28 15:17:42
Dionisio Ruiz de Zarate wrote:
I want to put 15 input values.
when i programing i make this:
for(int i=0;i<15;i++){
print (the input);
}

how can i using one xslt template to put 15 input whithout writte into the
xslt fiel the 15?

Such iteration is imperative programming. XSLT is a declarative, functional
programming language. You may find that your problem does not require 15
steps. The most common mistake that people make with this kind of iteration is
they use it when they don't need it, because they have bad habits they learned
in imperative programming languages :)

First review the examples given here:
http://www.dpawson.co.uk/xsl/sect2/N7450.html
(see "Retrieving next 10 elements" and "Splitting up tables")

If you cannot use those examples to solve your problem, ask here, but
show us your XML input and desired output.

If you find that you really must iterate a fixed number of times,
here are your options:

option 1:  Use recursion to iterate a fixed number of times
http://www.dpawson.co.uk/xsl/sect2/N4806.html
(the example above obtains the number of iterations from the XML)

option 2:  Select as many nodes as you need and process them using
xsl:for-each or xsl:apply-templates. Inside the for-each or template,
position() will give you the equivalent of i, if you really need it. You can
pass in the number of iterations through a top-level parameter. This works as
long as you know the document you're selecting from has the number of nodes
you need. Use document('') to use the stylesheet tree.

<xsl:stylesheet...>
  <xsl:param name="n" select="15"/> <!-- default: n=15 -->
  <xsl:template ...>
    <xsl:for-each select="document('')//node()[position() &lt; $n]">
      <p>
        <xsl:text>iteration #</xsl:text>
        <xsl:value-of select="position()"/>
        <xsl:text> of </xsl:text>
        <xsl:value-of select="last()"/>
      </p>
    </xsl:for-each>
...

option 3:  See if your XSLT processor offers a nonstandard or XSLT 2.0
function that implements a more natural "for" loop.


Mike

-- 
  Mike J. Brown   |  http://skew.org/~mike/resume/
  Denver, CO, USA |  http://skew.org/xml/

 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list



<Prev in Thread] Current Thread [Next in Thread>