For loops vs. apply-templates

This question is a bit argumentative, but here is my take on it.

I can’t see a clear line when to use
loops and when to use templates.

I’d say that you shoud strive to avoid for-each as often as possible in favor of apply-templates.

Using for-each makes your program more complex by adding nesting levels and it’s also impossibe to re-use the code inside the for-each block. Using apply-templates will (when done right) generate more more flexible and modular XSLT.

On the other hand: If you write a stylesheet with limited complexity and re-usability or modualarization are not a concern, using for-each may be quicker and easier to follow (for a human reader/maintainer). So it’s partly a question of personal preference.

In your case, I would find this elegant:

<xsl:template match="ebook">
  <!-- ... -->
  <xsl:apply-templates select="authors/author" />
  <!-- ... -->
</xsl:template>

<xsl:template match="authors/author">
  <b>
    <xsl:value-of select="name"/>
    <xsl:if test="position() &lt; last()">,</xsl:if>
  </b>
</xsl:template>

To your other question

And another question is it normal to just say apply-templates when you know that there won’t be other children of the element where you are writing it.

When you know there will never be any children in this element, writing apply-templates is pointless.

When done right, XSLT has the ability to flexibly deal with varying input. If you expect there could be children at some point, an apply-templates won’t hurt.

A simple apply-templates without select is rather unspecific, both in terms of which (i.e.: all of them) and in what order (i.e.: input document order) nodes will be processed. So you could end up either processing nodes you never wanted to process or node you already have processed earlier.

Since one cannot write a sensible template for unknown nodes, I tend to avoid the unspecific apply-templates and just adapt my stylesheet when the input changes.

Leave a Comment