View Single Post
 
Old October 2nd, 2006, 08:54 AM
mhkay's Avatar
mhkay mhkay is offline
Wrox Author
Points: 18,487, Level: 59
Points: 18,487, Level: 59 Points: 18,487, Level: 59 Points: 18,487, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Yes, it's a good idea to try to write stylesheets that don't use xsl:for-each at all. WHen you've achieved that, you will understand xsl:apply-templates, and you will then be able to judge when it's appropriate to use apply-templates and when it's better to use for-each. The danger for beginners is using for-each because it feels more familiar.

I would write this code:

<xsl:for-each select="n1:phone">
                                    <xsl:for-each select="n1:mobile">
                                        <xsdLocal:MobilePhone>
                                                <xsl:call-template name="format_phone">
                                                    <xsl:with-param name="in_phone" select="."/>
                                                </xsl:call-template>
                                        </xsdLocal:MobilePhone>
                                    </xsl:for-each>
                                </xsl:for-each>

as

<xsl:apply-templates select="n1:phone"/>

<xsl:template match="n1:phone">
  <xsl:apply-templates select="n1:mobile"/>
</xsl:template>

<xsl:template match="n1:mobile">
  <xsdLocal:MobilePhone">
    ... code from your named template format-phone ...
  </xslLocal:MobilePhone>
</xsl:template>

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference