RE: [xsl] RE:"*NEVER* use for-each"

Subject: RE: [xsl] RE:"*NEVER* use for-each"
From: "FINLEY, Mike" <Mike.FINLEY@xxxxxxxxxx>
Date: Wed, 28 Feb 2001 09:15:55 -0000
Ok, but a rule '*NEVER* ...' is not helpful without a 'because ...' or
'unless ...' clause. Anyway I learnt a little through looking at sort, but
am not convinced that templates are easier for what I was doing (I'd have to
sort out using modes as well, I think). 

For someone to tell me I'm wrong, here is the relevant portion of my XSL:

<xsl:template match="INDEX">
<H3>Lessons by title</H3>
<xsl:for-each select="REPORT"><xsl:sort select="TITLE"/>
<!-- generate table row for index -->
select="@file"/>.asp</xsl:attribute><xsl:value-of select="TITLE"/></A></TD>
	<TD><xsl:value-of select="CONTACT"/></TD>
<H3>Lessons by family</H3>
<xsl:for-each select="REPORT"><xsl:sort select="FAMILY"/><xsl:sort
<!-- generate table row for index -->
	<TD><xsl:value-of select="FAMILY"/></TD>
		<xsl:value-of select="TITLE"/></A>
	<TD><xsl:value-of select="CONTACT"/></TD>

and an example REPORT element (INDEX holds a sequence of REPORT elements)
<REPORT file="0001">
<TITLE>title text</TITLE><FAMILY>grouping</FAMILY><CONTACT>name</CONTACT>


-----Original Message-----
From: Chris Bayes [mailto:Chris@xxxxxxxxxxx]
Sent: 27 February 2001 17:41
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject: RE: [xsl] RE:"*NEVER* use for-each"

When a lot of people come to XSLT they see <xsl:for-each> and think "That's
what I need" when it most times isn't. They should be using templates. As
they start to develop their template they add <xsl:if> and <xsl:choose> all
over the place within the <xsl:for-each> to get round not using templates in
the first place. So to stop people making a bealine for <xsl:for-each> I
always say "Beginners rule #1 *NEVER* use for-each". Daniel's solution to
his own problem is

>ndex]" />

>Now I know might not be pretty, but it DOES return what I want. $Index will
>range from 1 to 9

Next he will be asking how he can increment his $Index variable which he
can't do because you can't increment variables without using recursion. As
he hasn't got the hang of templates yet explaining to him how to do
recursion is not going to be easy.
To quote Francis in another thread

>People on this list find themselves bailing out people who've sailed on
>to the same old rocks, and try to let others know that they're sailing
>in that direction, that why we make suggestions of this kind.

Ciao Chris

XML/XSL Portal

>As a beginner, who has just used for-each to solve a problem, why should I
>never use it?
>(OK, I admit having checked the <xsl:sort> description in Mike Kay's book I
>could probably have used another template, but why is that preferable to
>using for-each?)

 XSL-List info and archive:

This email is confidential and intended solely for the use of the 
individual to whom it is addressed. Any views or opinions presented are 
solely those of the author and do not necessarily represent those of 
If you are not the intended recipient, be advised that you have received this
email in error and that any use, dissemination, forwarding, printing, or 
copying of this email is strictly prohibited.

If you have received this email in error please notify the Sema UK
Helpdesk by telephone on +44 (0) 121 627 5600.

 XSL-List info and archive:

Current Thread