xsl-list
[Top] [All Lists]

Re: [xsl] Second of two consecutive call-template instructions appears to affect the first?

2006-03-15 03:43:20
Wendell Piez <wapiez(_at_)mulberrytech(_dot_)com> wrote:

Sebastian,

It appears that while your variables are in scope in the calling
template, they are no longer in scope in the templates called.

The solution is to declare them also in the called templates if the
execution context (such as the context node) provides access to the
values you need for them, or if not (and sometimes even if so), to
pass their values into the named templates as parameters.

In XSLT, variables are scoped within the context of their parent
elements. As the language is defined in such a way to be "side-effect
free", you can't declare a variable in one template and then refer to
it in another.

Cheers,
Wendell

Hi Wendell,

I hope your class went well.  You may remember that you were going to
quote me as saying "we're just not used to having things done for us
are we?"

Regarding the problem in hand, I just don't get it.  Why should this
work fine:

  <xsl:call-template name="does-image-flickr" />
  .
  .
  <xsl:template name="does-image-flickr">
  <xsl:choose><!-- photo is a flickr link -->
    <xsl:when test="/page/images/image[(_at_)name=$name]/flickr">
                                              ^^^^
                                   [variable 'name' is OK]

...and this cause an error:

  <xsl:call-template name="does-image-flickr" />
  <xsl:call-template name="write-caption">
    <xsl:with-param name="cpos"><xsl:value-of select="$cpos" /></xsl:with-param>
    <xsl:with-param name="caption"><xsl:value-of select="$caption" 
/></xsl:with-param>
  </xsl:call-template>
  .
  .
  <xsl:template name="does-image-flickr">
  <xsl:choose><!-- photo is a flickr link -->
    <xsl:when test="/page/images/image[(_at_)name=$name]/flickr">
                                              ^^^^
                                   [variable 'name' causes an error]

...where the only difference is the call to the 'write-caption'
template that comes _after_ the call to the 'does-image-flickr'
template. 

The fact that the call to 'does-image-flickr' works in the first
instance proves to me what I have been lead to believe, namely that
call-template differs from apply-templates in that the local scope is
unchanged as well as the current node list.  My experience whilst
writing this template has also served to confirm this.  I have several
exapmples where templates called using call-template make use of
params defined in the callee without explicitly passing the params
using with-params.

sdt




--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--