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: Passing local variable from one template to another


Hi Alex,

> I need to pass value of the local variable defined in one template to another
> template.
> The following source does not work. What's wrong ? Thanks a lot in advance.

You always have to keep two things in mind when using variables:
1. variables can't be updated
2. variables are locally scoped

That means you have to restructure the logic of your stylesheet.
If you are familiar with functions in some procedural programming languages,
your example could be interpreted as follows:

> <?xml version="1.0"?>
> 
> <xsl:stylesheet     xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
> version="1.0">
> 
>  <xsl:output method="text"/>
> 
>  <xsl:template match="/">

func main() {  // pseudo syntax

>     <xsl:apply-templates select="A/B/C">
>     </xsl:apply-templates>

   abc();

>     <xsl:apply-templates select="A/B/D/E/F">
> 
>      <xsl:with-param name="XYZ" select="$XYZ" >
>      </xsl:with-param>
> 
>     </xsl:apply-templates>

   abcdef(xyz);
   // oops, xyz isn't defined

>  </xsl:template>

}

>  <xsl:template match="A/B/C">

func abc() {
 
>   <xsl:variable name="XYZ">
> 
>     <xsl:value-of select="K/L/M/N/O"/>
> 
>   </xsl:variable>

   xyz = value("klmno"); // haven't found another equivalent ...

>  </xsl:template>

}
// you created a local variable xyz, but you didn't use it

>  <xsl:template match="A/B/D/E/F">

func abcef() {

>    <xsl:text>BLAH-BLAH-BLAH-111</xsl:text>
> 
>    ......................................
> 
>    <xsl:value-of select="$XYZ" />

   print xyz;
   // oops, xyz isn't defined   

>    ......................................
> 
>    <xsl:text>BLAH-BLAH-BLAH-999</xsl:text>
> 
>  </xsl:template>

}
 
> </xsl:stylesheet>

Alright, this isn't a solution for your problem, but maybe it's helpful
to solve it yourself.

BTW: the equivalent of "func foo(bar) { ... }" would be
<xsl:template match="..." name="foo">
   <xsl:param name="bar" />
   ...
</xsl:template>

the equivalent of "x = foo(z)" then could be
<xsl:variable name="x">
   <xsl:call-template name="foo">  <!-- or xsl:apply-templates select=... -->
      <xsl:with-param name="bar" select="$z" />
   </xsl:call-template>
</xsl:variable>

Cheers,
Oliver


/-------------------------------------------------------------------\
|  ob|do        Dipl.Inf. Oliver Becker                             |
|  --+--        E-Mail: obecker@informatik.hu-berlin.de             |
|  op|qo        WWW:    http://www.informatik.hu-berlin.de/~obecker |
\-------------------------------------------------------------------/


 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]