RE: RE: [xsl] variable binding

Subject: RE: RE: [xsl] variable binding
From: cknell@xxxxxxxxxx
Date: Thu, 06 Nov 2003 09:29:13 -0500
Let's look at your template:

<xsl:template match="s//ng//*[1]" priority="5">
  <xsl:variable name="branch">right</xsl:variable>
  <xsl:copy>
    <xsl:attribute name="branch"><xsl:value-of select="$branch"/></xsl:attribute>
    <xsl:apply-templates select="@*|node()"/>
  </xsl:copy>
</xsl:template>

Inside this template you declare the variable first. This variable is then in-scope for all following siblings *of the variable*  and to any descendants of the following siblings.

$branch is in-scope for <xsl:apply-templates select="@*|node()"/>, but you don't refer to it in this element. Instead, you refer to it in a template that is a following-sibling of the variable's parent element that happens to be called by <xsl:apply-templates select="@*|node()"/>.

I am confused by " ... I am trying to find the leftmost daughter of the element ng." XPath has no concept of left or right. It has three (if we ignore the attribute axis) broad categories of axes: sibling axis, ancestor axis and descendant axis. 

Are you saying that you want to find an element that is a child of <ng>? XPath has a "child axis" that is a kind of descendant axis that includes only those elements that are one level down from the parent element. 

Please try to re-state your requirement using XPath terminology, or tell us which specific element in you example fits your requirement so we can figure out the correct XPath expression to select it.
-- 
Charles Knell
cknell@xxxxxxxxxx - email



-----Original Message-----
From:     Shipra Dingare <sdingar1@xxxxxxxxxxxx>
Sent:     Thu, 6 Nov 2003 11:21:49 +0000 (GMT)
To:       xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject:  RE: [xsl] variable binding


So then what does it mean to say that "variable bindings declared in 
templates should be visible to descendants and following siblings"? 
This is what it says in the specs.

Also, the whole reason I wanted to do this was because I am trying to find 
the leftmost daughter of the element ng.
Is there a way to do this?  I have really thought about it a lot but can't 
seem to find anything workable.
-Shipra



On Wed, 5 Nov 2003, Michael Kay wrote:

> > 
> > I'm having trouble with variable binding - basically, I read 
> > that variable 
> > bindings declared in templates should be visible to descendants and 
> > following siblings, but I'm getting not getting the behavior 
> > I thought I 
> > would with my stylesheet using Saxon 6.5.2...
> 
> You've misunderstood the rules. The scope of variables is static, not
> dynamic. It doesn't depend on your source document, and it doesn't
> depend on the way in which templates call each other. You're trying to
> reference a variable in one template that's declared in a different
> template. What you need to do is pass the value as a parameter from one
> template to the other, using xsl:with-param and xsl:param.
> 
> Michael Kay
> 
> 
> > 
> > so the relevant part of my file looks like this (it's a 
> > subbranch of s):
> >           <ng of='proteins' role='h'>
> >                 <nx head='targeting' role='h'>
> >                      <W index='92'>the</W>
> >                      <W role='h' index='93'>targeting</W>
> >                 </nx>
> >                      <W index='94'>of</W>
> >                 <nx head='proteins' role='k'>
> >                      <W index='95'>the</W>
> >                      <W index='96'>myosin</W>
> >                      <W role='h' index='97'>proteins</W>
> >                 </nx>
> > 	</ng>
> > 
> > and my stylesheet looks like this:
> > 
> > <xsl:template match="@* | * | comment() | processing-instruction() | 
> > node()">
> >  <xsl:copy>
> >   <xsl:apply-templates select="@*|node()"/>
> >  </xsl:copy>
> > </xsl:template>
> > 
> > <xsl:template match="s//ng//*[1]" priority="5">
> > 	<xsl:variable name="branch">right</xsl:variable>
> > 	<xsl:copy>
> > 	<xsl:attribute name="branch"><xsl:value-of 
> > select="$branch"/></xsl:attribute>
> > 	<xsl:apply-templates select="@*|node()"/>
> > 	</xsl:copy>
> > </xsl:template>
> > 
> > 
> > <xsl:template match="s//ng//*" priority="4">
> > 	<xsl:copy>
> > 	<xsl:attribute name="branch"><xsl:value-of 
> > select="$branch"/></xsl:attribute>
> > 	<xsl:apply-templates select="@*|node()"/>
> > 	</xsl:copy>
> > </xsl:template>
> > 
> > but when I try to run it, it tells me that the variable 
> > branch has not 
> > been declared, even though it should first have processed the first 
> > daughter of ng, where it has been declared. 
> > 
> > Best,
> > shipra
> > 
> > 
> >  XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
> > 
> 
> 
>  XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
> 
> 


 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