Re: [xsl] calling template with parameters

Subject: Re: [xsl] calling template with parameters
From: Jeni Tennison <mail@xxxxxxxxxxxxxxxx>
Date: Tue, 9 Jan 2001 12:48:57 +0000
Hi Roman,

> Why is the parameter in template "newslist" empty??

This is a classic problem in XSLT. When you pass a parameter to a
matching (as opposed to a named) template, you have to make sure that
the template is applied to nodes that match the match pattern of the
template expecting the parameter.

In your case, you're applying templates to the *root node* of your
news.xml document.  You haven't specified a template that matches the
root node, so the built-in template is used instead:

<xsl:template match="*|/">
  <xsl:apply-templates />

As you can see, this template doesn't expect any parameters, and it
doesn't pass any parameters on to any other templates; essentially the
parameter is ignored and discarded at this point.

You can do two things (in both I'm assuming that 'newslist' is the
document element of your news.xml document). First, you can define a
template that matches the root node and expects and passes on the

<xsl:template match="/">
  <xsl:param name="newscount" />
    <xsl:with-param name="newscount" select="$newscount" />

Second (better), you can select the nodes that you're actually want to
apply template to in the original xsl:apply-templates rather than
relying on the built-in templates to ripple down to it:

  <xsl:apply-templates select="document('news/news.xml')/newslist">
     <xsl:with-param name="newscount" select="$count" />

There's also the possibility that you could use a global variable to
store the count or something related to it (e.g. the 'shownews'
element); it would then be accessible within the second template
without you having to pass it as a parameter at all.

I hope that helps,


Jeni Tennison

 XSL-List info and archive:

Current Thread