Subject: Re: [xsl] <xsl:apply-templates> or <xsl:for-each> ?? From: Jeni Tennison <jeni@xxxxxxxxxxxxxxxx> Date: Sun, 24 Mar 2002 14:29:49 +0000 |
Tom P wrote: > Besides what Mike Kay said, xsl:for-each changes the context and > xsl:apply-templates does not. Sometimes you have to change the > context. For example, suppose you use a second xml file as a lookup > table and want to apply a key to it. You have to change the context > to be the root of the lookup table so you can apply the key, but you > don't want to change the overall context outside the lookup. That's > a good place to use xsl:for-each. Well, you could say that xsl:apply-templates "changes the context", because the code that's run when you use xsl:apply-templates can have a different current node to the current node to the xsl:apply-templates instruction itself. Taking the key-lookup example, you could do: <xsl:for-each select="document('doc.xml')"> <xsl:value-of select="key('key', $value)" /> </xsl:for-each> but you could equally do: <xsl:apply-templates select="document('doc.xml')" mode="lookup"> <xsl:with-param name="value" select="$value" /> </xsl:apply-templates> and then have: <xsl:template match="/" mode="lookup"> <xsl:param name="value" /> <xsl:value-of select="key('key', $value)" /> </xsl:template> The real advantage of xsl:for-each in this kind of situation is that it's a lot shorter! But that might stop being the case if the stuff that you're doing with the key lookup is more complicated and repeated several times in different places throughout the code. Cheers, Jeni --- Jeni Tennison http://www.jenitennison.com/ XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
Current Thread |
---|
|
<- Previous | Index | Next -> |
---|---|---|
Re: [xsl] <xsl:apply-templates> or , Thomas B. Passin | Thread | RE: [xsl] <xsl:apply-templates> or , Jarno . Elovirta |
Re: [xsl] one line of xml to indent, Peter Davis | Date | Re: [xsl] How can I merge two DTD b, Jeni Tennison |
Month |