Re: [xsl] Help calling templates with parameters

Subject: Re: [xsl] Help calling templates with parameters
From: "Anton Triest" <anton@xxxxxxxx>
Date: Wed, 22 Sep 2004 09:08:54 +0200
Charlie Consumer wrote:
> 
> I'm trying to refactor my XSL so that I can make a
> call to a template from several locations in my XSL. 
> However, I need to parameterize this template with a
> node set of where the data should come from.  I have
> two documents that I'm merging together to result in
> one document. I'm assigning parameter to a XPath to
> the node set which is correct before I call the
> template.  But, when I use it inside the template I
> get "Cannot use result tree fragment.  Error in XPath
> expression, Cannot use result tree fragment."  Here is
> an example of what I'm doing:
> 
> <xsl:for-each select="document($ruleFile)/rulelist/rule[@A='blah']">
>    <xsl:call-template name="insertRule">
>      <xsl:with-param name="currentRule">
>        <xsl:value-of select="current()"/>
>      </xsl:with-param>
>    </xsl:call-template>
> </xsl:for-each>

Hi Charlie,

<xsl:value-of select="current()"/> returns the text contents of
the current element, not the element itself. You can rewrite
the xsl:with-param like this:

<xsl:with-param name="currentRule" select="current()"/>

which has a totally different result, because here, you pass a
pointer to the 'real' current element. Your next template
should work, then.

But altogether, you can do it easily without any params (and much shorter),
by using apply-templates and match: 

  <xsl:variable name="rules" select="document($ruleFile)/rulelist/rule"/>

  <xsl:template match="/">
    <xsl:apply-templates select="$rules[@A='blah']"/>
  </xsl:template>

  <xsl:template match="rule">
    <rule id="{blah/@id}"/>
  </xsl:template>


Cheers,
Anton

 
> <xsl:template name="insertRule">
>    <xsl:param name="currentRule"
>    <xsl:element name="rule">
>       <xsl:attribute name="id">
>         <xsl:value-of select="$currentRule/blah/@id"/>
>         </xsl:attribute>
>    </xsl:element>
> </xsl:template>
>
> I'm using XMLSpy's debugger to step through the XSL,
> and once I enter the template the parameter isn't a
> node set like it was before I called the template, but
> a node fragment.  I don't know what a node fragment
> is.  Can anyone explain this?  And, what am I doing
> wrong here?  Sorry I'm new to XSL.  I tried reading
> the faq and archive, but I couldn't find anything
> about this in either location.
> 
> Charlie

Current Thread