This is the mail archive of the xsl-list@mulberrytech.com mailing list .


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: <xsl:apply-templates> or <xsl:for-each> ??


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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]