Re: Passing Variable into a child template

Subject: Re: Passing Variable into a child template
From: Jeni Tennison <jeni@xxxxxxxxxxxxxxxx>
Date: Sat, 22 Jul 2000 10:56:48 +0100
Sia,

>I have also tried apply-templates unsuccessfully:
><xsl:variable name="server-in-parent">
>  <xsl:apply-templates select="MenuItem">
>    <xsl:with-param name="thisServer" select="ServerName"/>
>  </xsl:apply-templates>
></xsl:variable>

You are almost there! :)  I'm not sure where you got the idea of putting
the xsl:apply-templates in the xsl:variable, but you don't need to, and
almost certainly don't want to.

Instead, put it in the template you have that matches MenuList:

<xsl:template match="MenuList">
  <xsl:apply-templates select="MenuItem">
    <xsl:with-param name="thisServer" select="ServerName"/>
  </xsl:apply-templates>
</xsl:template>

When this template is processed, it applies templates to all its MenuItem
children.  The templates that are applied are passed the parameter
$thisServer, with the value being the value of the 'ServerName' child
element of the MenuList (the name of the server).  Your MenuItem template:

<xsl:template match="MenuItem">
  <xsl:param name="theServer"/>
   ...
  <xsl:attribute name="src">
     http://<xsl:value-of select="$theServer"/>
  </xsl:attribute>
   ...
</xsl:template>

picks up the $theServer parameter and uses it.

Having said that (and with the proviso that you may have been simplifying a
more general problem that you have, so this may not be relevant), generally
if you can access some information from within a template, there is no need
to pass that information into the template using a parameter.  In your
case, from within the MenuItem template (when the current node is a
MenuItem), you can find out the server name using the XPath:

  ../ServerName

(i.e. the value of the 'ServerName' child element of the parent of the
current node).  So your template could instead look like:

<xsl:template match="MenuItem">
   ...
  <xsl:attribute name="src">
     http://<xsl:value-of select="../ServerName"/>
  </xsl:attribute>
   ...
</xsl:template>

I think I'm right in saying that parameters generally buy more if you have
a very generic template that relies on parameters to give specific
solutions, or if you've calculated something to use in the calling template
anyway, and want to pass that on.

I hope this helps,

Jeni

Dr Jeni Tennison
Epistemics Ltd * Strelley Hall * Nottingham * NG8 6PE
tel: 0115 906 1301 * fax: 0115 906 1304 * email: jeni.tennison@xxxxxxxxxxxxxxxx


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread