RE: [xsl] variable declaration

Subject: RE: [xsl] variable declaration
From: Stuart Brown <sbrown@xxxxxxxxxxx>
Date: Mon, 18 Nov 2002 16:30:44 -0000
Hi Laura,

Firstly, you can't use the <xsl:variable> element as a direct child of
<xsl:for-each>: it can only be used as a child of <xsl:stylesheet> or
<xsl:template>.  However, you can get round this by calling a named template
within a for-each. Secondly, there are a couple of oddities in your XSL. If
this has been copied directly from your stylesheet (and not retyped) you in
all probability have a typo in the for-each predicate, which compares a
myschedID element with a mychedID (no s). In addition, once you are in the
for-each element, the current element of the transformation changes to be
the relevant node, so the value you are trying to supply to the key()
function is looking for ANOTHER myschedule element which is a child of the
myschedule element you have for-eached onto, when I assume you just want a
myschedID child of the myschedule

Finally, if this is all your stylesheet is, and there is no additional
processing, I don't quite get why you are using variables at all, when you
might as well just operate straight from the XPath. However, assuming these
variables will be reused, you probably want something like:

<xsl:for-each
select="xalan:nodeset($new-schedule)/schedule/myschedule[not(myschedID =
preceding-sibling::myschedule/myschedID)]">
 <xsl:call-template name="getTimes"/>
</xsl:for-each>

<xsl:template name="getTimes">
 <xsl:variable name="schedule-on-times"
select="key('schedTimes',myschedID)"/>
 <xsl:for-each select="$schedule-on-times">
  <xsl:call-template name="doTimes"/>
 </xsl:for-each>
</xsl:template>

<xsl:template name="doTimes">
 <xsl:variable name="my-endtime">
  <xsl:call-template name="end-time-format">
   <xsl:with-param name="endtime-param" select="time"/>
  </xsl:call-template>
 </xsl:variable>
 <xsl:value-of select="$my-endtime"/>
</xsl:template>

However, if this is all there is to this portion of your transform, why use
variables at all? Why not just give:

<xsl:for-each
select="key('schedTimes',xalan:nodeset($new-schedule)/schedule/myschedule[no
t(myschedID = preceding-sibling::myschedule/myschedID)]/myschedID)">
 <xsl:call-template name="end-time-format">
  <xsl:with-param name="endtime-param" select="time"/>
 </xsl:call-template>
</xsl:for-each>


> -----Original Message-----
> From: Laura Jenkins [mailto:xsl_list@xxxxxxxxxxx]
> Sent: 18 November 2002 15:38
> To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> Subject: [xsl] variable declaration
> 
> 
> hello people,
> I have a strange problem. I have the following structure in 
> my XSL file.
> 
>   <xsl:for-each
> select="xalan:nodeset($new-schedule)/schedule/myschedule[not(m
> yschedID =
> preceding-sibling::myschedule/mychedID)]">
> 
>    <xsl:variable name="schedule-on-times"
> select="key('schedTimes',myschedule/myschedID)"/>
>    <xsl:for-each select="$schedule-on-times">
> 
>     <xsl:variable name="my-endtime">
>       <xsl:call-template name="end-time-format">
>        <xsl:with-param name="endtime-param" select="time"/>
>       </xsl:call-template>
>      </xsl:variable>
>      <xsl:value-of select="$my-endtime"/>
>       </xsl:for-each>
>    </xsl:for-each>
> 
> The XSL is like above.
> This works perfectly fine in win2000 server.
> but it fails on unix. The way it fails is again quite strange.it gives
> invocation target exception. when this is template is called.
> I then took the code from the template "end-time-format" and 
> pasted this in
> the main code. and took off the variable declaration from the 
> code and used
> it straight. and it seems to be working fine that way. So my 
> conclusion was
> that it is failing because of this kind of a variable 
> declaration?? That
> sounds quite strange to me.I only wanted to make the code 
> neat by deviding
> it into templates.
> Would this be a bug in XALAN that it behaves weird with UNIX?
> anyone came across similar situation???
> Thanks Very Much
> 
>  XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
> 

 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread