Important Notice: Our web hosting provider recently started charging us for additional visits, which was unexpected. In response, we're seeking donations. Depending on the situation, we may explore different monetization options for our Community and Expert Contributors. It's crucial to provide more returns for their expertise and offer more Expert Validated Answers or AI Validated Answers. Learn more about our hosting issue here.

How do I choose when to use xsl:for-each and when to use xsl:apply-templates?

apply-templates XSL
0
Posted

How do I choose when to use xsl:for-each and when to use xsl:apply-templates?

0

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.

Related Questions

What is your question?

*Sadly, we had to bring back ads too. Hopefully more targeted.

Experts123