How do I choose when to use xsl:for-each and when to use xsl:apply-templates?
The way xsl:for-each and xsl:apply-templates select nodes for processing is identical. The way these instructions find the templates to process the selected nodes is different. With xsl:for-each, the template to use is fixed. It is the template that is contained in the body of the xsl:for-each element. With xsl:apply-templates, the XSLT processor finds the template to be used for each selected node by matching that node against the template rules in the stylesheet. Finding a template by matching requires more time than using the contained template. However, matching allows for more flexibility. Also, matching lets you avoid repeating templates that might be used in more than one place in a stylesheet. Named templates are another option for invoking a template from more than one place in a stylesheet, when you know which template you want. It is a common mistake to use (and bear the overhead of) matching when it is not needed. But it allows you to do powerful things.