Re: [xsl] Problems to interpret an attribute value as an Xpath expression

Subject: Re: [xsl] Problems to interpret an attribute value as an Xpath expression
From: Jeni Tennison <jeni@xxxxxxxxxxxxxxxx>
Date: Thu, 10 Jan 2002 13:39:11 +0000
Hi Roman,

[Removed www-forms@xxxxxx from the CC list because we're not allowed
to cross-post on this list.]

> I'd like to use the value of a ref attribute as an XPath expression
> in my stylesheet. I want the processor to jump to the specified node
> and get the value from there. I used a "normal" <xsl:value-of
> select="@ref"/> ,but the processor didn't interpret it as an XPath
> expression but as a string.
>
> Does anyone know a method to tell the processor to use a source
> document's attribute value as an XPath expression in the stylesheet?

You can't do it in XSLT 1.0. Some processors support an extension
evaluate() function, so that you can do, for example:

  saxon:evaluate(concat('/xform:model/xform:instance', @ref))

Another alternative that might be feasible would be to construct a
path to the nodes that you want to refer to and use that as a key
value. This is difficult because key values have to be set using XPath
expressions, but it's doable if you're using a processor that supports
user-defined extension functions written in XSLT. For example, you
could do:

<xsl:key name="instance-data" match="xform:instance//*"
         use="my:path()" />

Where my:path was defined with something along the lines of:

<func:function name="my:path">
  <func:result select="concat(my:path(..), '/', name())" />
</func:function>

You could then use:

  key('instance-data', concat('xform:model/xform:instance', @ref))

to get hold of the relevant node from the instance data tree.

A third alternative might be feasible if the processor you're using
supports XPointer fragment identifiers in URLs. You might be able to
do something like:

  document(concat('#xpointer(/xform:model/xform:instance', @ref, ')'),
           /)

to get hold of the node that you want.

[This was one of the use cases that I pointed out for an evaluate()
 function in XPath 2.0.]

Cheers,

Jeni

---
Jeni Tennison
http://www.jenitennison.com/


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


Current Thread