Re: [xsl] xsl:for or xsl:repeat

Subject: Re: [xsl] xsl:for or xsl:repeat
From: Wendell Piez <wapiez@xxxxxxxxxxxxxxxx>
Date: Wed, 24 May 2006 16:22:15 -0400
At 04:06 PM 5/24/2006, Jon wrote:
On 5/24/06, Wendell Piez <wapiez@xxxxxxxxxxxxxxxx> wrote:
At 01:21 PM 5/24/2006, I wrote:
>This is all true, but there are times when <xsl:for-each
>select="//node(position() &lt;= $number)"/> is just so far within
>the safety zone that a purist solution seems, well, limiting in its
>own way (since then we end up having to document the recursion, etc.).

Okay, I admit that it has sometimes been said one must really
document the position test as an artificial constraint on a count of
iterations, as it really is so bizarre....

Just a quick, somewhat stupid question that probably won't come up in any real practicality. What if there is the need to iterate more than there are nodes in the document? My brain is a little scattered at the moment, but am I correct in that this solution limits by the number of nodes in the document. Granted, it would be rare case for this to happen. Just curious.

It's not a stupid question; nor is it particularly quick. The quick answer is "you're hosed". The deeper answer is "but you're careful to avoid that aren't you?", which raises all kinds of interesting other questions, such as how can you do that and what form your edge cases might take.

Compared to a true native solution such as recursion, such a workaround is only a brute-force expediency. Yet those are neither very uncommon, nor necessarily pernicious in themselves, especially when they work. (They can still be bad for other reasons.)

As I said earlier, the true solution is to use XSLT in a way that reflects its strengths. Maybe it's not possible to provide, at runtime, secondary input like


instead of '2'. But if it is, then XSLT has a much more elegant solution to the problem of knowing how many times to iterate: for-each select="$signers/*".

If you know the names of the signers ahead:

  <signed>Geo Washington, Esq</signed>
  <signed>Dr B Franklin</signed>

you could even print their names on the form (though I'm not sure you'd want to) under the blanks where they are meant to sign. Knowing ahead of time what their names are might be only a bit more of a stretch than knowing how many there are.

Mind you, as was mentioned, this all disappears in XSLT/XPath 2.0, which can iterate over the sequence (1 to $n).


Wendell Piez                            mailto:wapiez@xxxxxxxxxxxxxxxx
Mulberry Technologies, Inc.      
17 West Jefferson Street                    Direct Phone: 301/315-9635
Suite 207                                          Phone: 301/315-9631
Rockville, MD  20850                                 Fax: 301/315-8285
  Mulberry Technologies: A Consultancy Specializing in SGML and XML

Current Thread