RE: [xsl] How to specify a path to a node in a nodeset using a variable

Subject: RE: [xsl] How to specify a path to a node in a nodeset using a variable
From: "Shakeel Mahate" <shakeelmahate@xxxxxxxxxxx>
Date: Fri, 9 Mar 2001 10:48:21 -0500
From: "Michael Kay" <mhkay@xxxxxxxxxxxx>
>>   <xsl:variable name="path" select="string(@name)"/>
>>         <xsl:value-of select="$container/$path"/>

>Inventive, but it doesn't work. Variables can only hold values, they can't
>hold expressions or parts of expressions.

>Saxon (and I think several other products, now) has an evaluate() extension
>function that will do this for you.

xalan:evaluate($path)  or saxon:evaluate($path) are indeed a way to acheive
this but not completely.  Since, I cannot change the current nodeset to
which this evaluate() function is applied.  If you recall I need to
evaluate($path) to the $container nodeset.

I cannot use xsl:call-template since it does not change the current nodeset.

I cannot use xsl:apply-templates since I cannot  write templates for the
$container nodeset, since I have no preordained knowledge about the
structure or the contents of $container.

I do not want to write xsl:template match="." since it can be messy

So, why is there no parameter in evaluate which takes in a nodeset and a
string and then evaluates the string as an XPath expression on the nodeset.
I know that the java class Extension.evaluate is passed a nodeset as the
first argument by the XSLT processer, but it is the current nodeset.

So the only solution that I could use was:

<xsl:variable name="value">
  <xsl:for-each select="$container[1]">
      <xsl:value-of select="xalan:evaluate($path)"/>

Now this is a hack....

Somebody who reads my code will have a tough time figuring out that the
<xsl:for-each...> is there simply to change the current nodeset.

By specifying $container[1] I am reducing the number of iterations, is this
true Mike?

The code could have been more readable with this solution:
<xsl:variable name="value" select="xalan:evaluate($container, $path)/>

Another solution would be to change xsl:call-template to optionally change
the nodeset if one is supplied

<xsl:call-template name: "eval" apply-to: "$container">
   <xsl:param name: "path">$path</xsl:param>

apply-to attribute of call-template is optional and it changes the current

Any suggestions....


 XSL-List info and archive:

Current Thread