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]

Re: calling template with parameters


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 />
</xsl:template>

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
parameter:

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

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" />
  </xsl:apply-templates>

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

---
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]