RE: [xsl] XML to Table

Subject: RE: [xsl] XML to Table
From: "Andreas L. Delmelle" <a_l.delmelle@xxxxxxxxxx>
Date: Tue, 20 Apr 2004 18:49:33 +0200
> -----Original Message-----
> From: Veronica Sanchez [mailto:vsguiseris@xxxxxxxxxxx]
>

Hi,

> thanks!!! Both suggestions have been very useful to me, but now I
> have two
> xsl that make the transform quickly:
>
<snip />
> What is better, with or without templates?
>

AFAICT, it doesn't really matter (so, depends on what you would define as
'better'). WRT processing-speed, I guess the difference between the two
solutions is negligeable.

The main difference is in the modularity. In the first solution (using
xsl:for-each), all relevant code is put inside one and the same template,
which in turn will lead to code that is rather awkward to read, understand
or maintain. The second solution puts all different bits of code inside
separate templates, making it very easy to modify (or replace) one piece of
the processing, or to include/import the templates in other stylesheets.

The solution with the templates is certainly more in sync with the general
XSLT processing-model (apply-templates and let the processor walk the tree),
whereas the xsl:for-each approach leans more toward the 'classic'
programming-languages, where it's all about iterations (loops). So, in this
case, as said, it doesn't really matter, but if you're going to write more
XSL stylesheets (most likely more complex than this example) it's certainly
'better practice' to try to adhere as much as possible to the matching
templates approach.

As a general tip, keep in mind that from an XSLT point-of-view, xsl:for-each
should be the last possibility entering your mind:

<xsl:for-each select="nodes">
  <!-- transform -->
</xsl:for-each>

achieves the same result as

<xsl:template match="parent-node">
  <xsl:apply-templates select="nodes" />
</xsl:template>

<xsl:template match="nodes">
  <!-- transform -->
</xsl:template>

The cases for which this is not true, or even undesirable, are rare.


Hope this clarifies it somewhat...

Cheers,

Andreas

Current Thread