سؤال

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?

هل كانت مفيدة؟

المحلول

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.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top