[xsl] Re: Re: Variable of variable

Subject: [xsl] Re: Re: Variable of variable
From: Dimitre Novatchev <dnovatchev@xxxxxxxxx>
Date: Mon, 9 Jul 2001 02:45:25 -0700 (PDT)
Paul Prescod wrote:
[snip]
> >...
> >         ${$col1}
> > but xalan doesn't accept these syntaxes !!!
> > 
> > Do you know how to access to a variable of a variable ?
> >...
> 
> I don't think it is possible. There are really very few programming
> languages where it *is* possible and even in those ones it isn't easy.
> Variable names are just labels for places that store information. They
> aren't first-class objects that you should be manipulating in your
> programs. There must be another way to solve the same problem. If you
> explain the underlying problem, we might be able to help you find a
> solution that doesn't involve "meta-variables." For instance here's one
> idea:
> 
> <xsl:choose>
>   <xsl:variable name="col-val">
>     <xsl:when test="$col='HOUR'"/>
>        <xsl:value-of select="$HOUR"/>
>     </xsl:when>
>     <xsl:when test="$col='DAY'"/>
>        <xsl:value-of select="$DAY"/>
>     </xsl:when>
> </xsl:choose>

Or, if the target variable is a global one and ***static (its contents not the
result of a transformation)***, then the following code will make accessible this
xsl:variable and its contents:

<xsl:variable name="vDynamic" select="document('')/*/xsl:variable[@name=$col]"/>


Or, if the static xsl:variable is not global, then the following XPath expression
locates all xsl:variable -s that are in scope of the current node:

//xsl:variable[generate-id(parent::*) = generate-id(current()) 
            or 
     count(.. | current()/ancestor::node()) = count(current()/ancestor::node() ) 
          and 
     not(@name=../descendant::node()/xsl:variable/@name ) 
          and 
     count(. | current()/following::xsl:variable) != count(current()/following::P )
              ]

so the target variable in scope will be:

//xsl:variable[generate-id(parent::*) = generate-id(current()) 
            or 
     count(.. | current()/ancestor::node()) = count(current()/ancestor::node() ) 
          and 
     not(@name=../descendant::node()/xsl:variable/@name ) 
          and 
     count(. | current()/following::xsl:variable) != count(current()/following::P )
              ]
             [@name = $col]


I completely agree, that accessing variables in this way is not a good practice.


Cheers,
Dimitre Novatchev.



__________________________________________________
Do You Yahoo!?
Get personalized email addresses from Yahoo! Mail
http://personal.mail.yahoo.com/

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


Current Thread