0

I have a variable:

<xf:var name="xpath" value="/my/xpath/expression"/>

and I want to use /my/xpath/expression in the ref attribute of <xf:input>:

<xf:input ref="/my/xpath/expression"/>

Here I use a hardcoded XPath expression, but I want instead to use the XPath expression stored in the $xpath variable instead, something like this :

<xf:input ref="$xpath"/>

How can this be achieved?

ebruchez
  • 7,760
  • 6
  • 29
  • 41
user2539823
  • 103
  • 12
  • ``? Or do you want a string literal? Then try: `` – CodeManX Apr 25 '14 at 12:17
  • First solution is not working: unexpected token '{' in xpath expresion. Second solution, as I mentioned in my post, determine to hardcode xpath, but I have xpath in variable $xpath ($xpath="/my/xpath/expression") – user2539823 Apr 25 '14 at 12:42
  • Or is it ``? Also have a look at the binding parameter, may that one accepts variables. – CodeManX Apr 25 '14 at 13:13
  • @CoDEmanX, thanks for the suggestions on this one. I provided an answer below. AVTs are not supported on XForms attributes which are *already* XPath expressions, such as `ref`. Also, `` was supported but XForms 2.0 standardizes on `` – ebruchez May 01 '14 at 21:26

1 Answers1

3

What you probably mean is that the value of $xpath is a string, literal or not, which you then want to evaluate. It's different to say:

<xf:var name="xpath" value="/my/xpath/expression"/>

and:

<xf:var name="xpath" value="'/my/xpath/expression'"/>

In the first case, the variable $xpath contains an XPath expression which is evaluated when the variable needs its value. The result will be an XPath type, such as a string, element, or in general any sequence of XPath items (item()*). If your expression is really say /path/to/foo/bar, then the result will a sequence of zero or more element nodes with name bar.

In the second case, notice the quotes '. This means that the value of $xpath is a string.

If you want then another XForms construct to do something with that string other than playing with it as a string, you will have to evaluate that expression dynamically.

There is a function for that, saxon:evaluate(). So you can write:

<xf:input ref="saxon:evaluate($xpath)"/>

You must make sure the saxon namespace prefix is in scope, with:

xmlns:saxon="http://saxon.sf.net/"

There is a bit more to it, namely in what context the expression runs. In most cases, it should work.

ebruchez
  • 7,760
  • 6
  • 29
  • 41