Re: [xsl] Is xsl:for-each "syntactic sugar"?

Subject: Re: [xsl] Is xsl:for-each "syntactic sugar"?
From: David Carlisle <davidc@xxxxxxxxx>
Date: Fri, 07 May 2010 13:46:39 +0100
On 07/05/2010 13:28, Mukul Gandhi wrote:
  As you said, many XSLT programming situations could be
equivanetly handled by for-each&  apply-templates.

Not "many", all uses of for-each can be equivalently handled by apply-templates.




..
<xsl:variable name="a">...</xsl:variable>
..
<xsl:variable name="z">...</xsl:variable>
...
<xsl:for-each select="foo">
  body
</xsl:for-each>

is



.
<xsl:variable name="a">...</xsl:variable>
..
<xsl:variable name="z">...</xsl:variable>
...
<xsl:apply-templates select="foo" mode="unique-d">
  <xsl:with-param name="a" select="$a"/>
  <xsl:with-param name="z" select="$z"/>
</xsl:apply-templates>


...


<xsl:template match="/|node()" mode="unique-id">
  <xsl:param name="a"/>
  <xsl:param name="z"/>
  body
</xsl:template>

so xsl:for-each is just really a minor syntactic variation on apply-templates for a mode with only one template, in that you can inline the template body into the call. If the body uses variable references then you need to use parameters in the apply-template version to keep them in scope, but it's a purely syntactic transformation.



David


________________________________________________________________________ The Numerical Algorithms Group Ltd is a company registered in England and Wales with company number 1249803. The registered office is: Wilkinson House, Jordan Hill Road, Oxford OX2 8DR, United Kingdom.

This e-mail has been scanned for all viruses by Star. The service is
powered by MessageLabs. ________________________________________________________________________


Current Thread