[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: Second of two consecutive call-template instructio

Subject: Re: Second of two consecutive call-template instructions appears to affect the first?
From: Wendell Piez <wapiez@xxxxxxxxxxxxxxxx>
Date: Tue, 14 Mar 2006 12:31:41 -0500
xsl test element not defined

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.


At 12:38 PM 3/14/2006, you wrote:
Hi all,

This is a section of a stylesheet I'm working on to convert a simple
custom markup to XHTML.  The section essentially deals with a line
such as:

<photo name="beach" ppos="right" cpos"below" caption="Wish you weren't here!" />

where 'ppos' indicates the position of the photo on the page, and
'cpos' indicates the presence of a caption as well as its position.
Details of the images themselves are included much lower down in the
XML source like so:

    <image name="beach">
      <desc>Sunny day on beach</desc>

If a 'cpos' attribute is specified, but no 'caption' attribute, then
the caption is the description found in <desc> element.  Captions are
optional and not all of the images have flickr addresses.

Basically, my problem is this.  If I comment out the four lines I've
marked with an asterix, the stylesheet processes the source without
any problems whatsoever (and does exactly what I want).  With these
four lines present however, I get the following error at the point

"The variable 'name' is not defined"

This seems very strange to me, and needless to say I can't figure out
why it is.  I am simply trying to wrap an <img> element in an anchor
if the image has a flickr address and then write the caption below the
image (in the containing <div>).

Any help much, much appreciated, as always.


* All the variables are defined by this point in the stylesheet *

<xsl:when test="$cpos!=''"><!-- there's a caption which requires a surrounding div -->
<div class="{$class}" style="{concat('width:',/page/images/image[@name=$name]/width,'px')}">
<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:otherwise><!-- or there isn't -->
<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[@name=$name]/flickr"> <-- "The variable 'name' is not defined"
<a href="{/page/images/image[@name=$name]/flickr}">
<xsl:call-template name="pass-image-params" />
<xsl:otherwise><!-- or it isn't-->
<xsl:call-template name="pass-image-params" /></xsl:otherwise>

<xsl:template name="pass-image-params">
<xsl:apply-templates select="/page/images/image[@name=$name]">
<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:with-param name="class"><xsl:value-of select="$class" /></xsl:with-param>

<!-- images -->
<xsl:template match="//image">
<xsl:param name="cpos" />
<xsl:param name="caption" />
<xsl:param name="class" />
<xsl:element name="img">
<xsl:attribute name="alt"><xsl:choose><!-- alt attribute defaults to desc -->
<xsl:when test="$caption!=''"><xsl:value-of select="$caption" /></xsl:when>
<xsl:otherwise><xsl:value-of select="desc" /></xsl:otherwise>
<!-- src attribute -->
<xsl:attribute name="src"><xsl:value-of select="file" /></xsl:attribute>
<!-- class attribute required if no caption and hence no containing div -->
<xsl:if test="$cpos=''">
<xsl:attribute name="class"><xsl:value-of select="$class" /></xsl:attribute>

<xsl:template name="write-caption">
<xsl:param name="cpos" />
<xsl:param name="caption" />
<xsl:if test="$cpos='below'"><br /></xsl:if>
<xsl:if test="$cpos!=''"><!-- caption-position specified -->
<xsl:choose><!-- but no caption specified, use desc as default -->
<xsl:when test="$caption!=''"><xsl:value-of select="$caption" /></xsl:when>
<xsl:otherwise><xsl:value-of select="desc" /></xsl:otherwise>

Wendell Piez                            mailto:wapiez@xxxxxxxxxxxxxxxx
Mulberry Technologies, Inc.                http://www.mulberrytech.com
17 West Jefferson Street                    Direct Phone: 301/315-9635
Suite 207                                          Phone: 301/315-9631
Rockville, MD  20850                                 Fax: 301/315-8285
  Mulberry Technologies: A Consultancy Specializing in SGML and XML

Current Thread


Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
First Name
Last Name
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.