[
Lists Home |
Date Index |
Thread Index
]
[Elliotte Rusty Harold]
>
> That's a bit of an exaggeration. For instance, consider this CS101 for
loop:
>
> for (int i=1; i < 10; i++) {
> System.out.println(i);
> }
>
> Here's the same loop in XSLT:
>
> <xsl:template name="CS101">
> <xsl:param name="index" select="1"/>
> <xsl:if test="index <= 10">
> <xsl:value-of select="$index"/>
> <xsl:call-template name="CS101">
> <xsl:param name="index" select="$index + 1"/>
> </xsl:call-template>
> </xsl:if>
> </xsl:template>
>
This is a good example of both sides of this tale. For Rusty's example
contains two errors, but the're not easy to spot. the correct version is
<xsl:template name="CS101">
<xsl:param name="index" select="1"/>
<xsl:if test="$index <= 10">
<xsl:value-of select="$index"/>
<xsl:call-template name="CS101">
<xsl:with-param name="index" select="$index + 1"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
1) Line 3 - "index" should be "$index"
2) Line 6 - "xsl:param" should be "xsl:with-param"
It's harder to spot the errors in the xslt version of the loop. On the
other hand, once you get used to it, creating loops like that is pretty
easy.
You can get more compact and readable,if there are enough elements of a
particular type in the root document or in the stylesheet to support your
loop iteration count (you could create another document that just holds a
bunch of elements for this purpose):
<!-- in a template -->
<xsl:apply-templates
select='document("")/xsl:stylesheet/xsl:template[11>position()]'
mode='loop'/>
<!--=======-->
<xsl:template match='xsl:template' mode='loop'>
<xsl:value-of select='position()'/>
</xsl:template>
|