xsl-list
[Top] [All Lists]

Re: Selecting from a node-set using variable path

2003-02-18 13:30:42
gary fong wrote:
This works:
 <xsl:variable name="t1" select="$myNodeSet//a/b/c"/>

This doesn't:
 <xsl:variable name="t2" select="$myNodeSet/$myPath"/>

Ah, this comes up regularly. It's something like doing
  String a="1 + 1";
  int b=1+a;
in Java and expecting this is not only doesn't cause the
compiler to barf but that b is 3 afterwards.

How do I filter down my node-set based upon some variable
location path?
For simple cases there are pure XPath solutions, however,
in your case you'll have to use an extension function to
invoke a runtime XPath interpreter, for example
 <xsl:variable name="myPath">/a/b/c</xsl:variable>
 <xsl:variable name="t3"
   select="xx:evaluate(concat('$myNodeset/',$myPath))"/>
The inner concat evaluates to the string
 $myNodeset//a/b/c
which in turn is evaluated as XPath expression.

Um, well, IIRC MSXML does not have exactly such an extension
function, but you can use a bit of JScript code to emulate
this. The EXSLT project at sourceforge should have such code.

J.Pietschmann


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