0
<xsl:value-of select="$MyVar"/>

works but

<xsl:value-of select="MyDataPfath/$MyVar"/>

do not work.

What is wrong in my code?

Juergen M.
  • 67
  • 2
  • 7

3 Answers3

0

From the look of it, what you are trying to achieve is 'dynamic evaluation'. XSLT does not support the dynamic evaluation of XPath by default, so you will need to make use of an extension function.

Depending on your XSLT processor, you might want to look at EXSLT extensions. In particular the dynamic module at http://www.exslt.org/dyn/index.html. This would allow to do something like this

<xsl:value-of select="dyn:evaluate('MyDataPfath/$MyVar')"/> 

However, in your case, perhaps the $MyVar contains just a single element name. In which case you could change your command to the following, which would work without any extension functions

<xsl:value-of select="MyDataPfath/*[local-name() = $MyVar]"/>
Tim C
  • 70,053
  • 14
  • 74
  • 93
0

Your code didn't fail, it did exactly what the specification says it should do. Which was different from what you were hoping/imagining that it might do.

Your hopes/imagination were based on a fundamental misunderstanding of the nature of variables in XPath. XPath variables are not macros. They don't work by textual substitution; they represent values. If the variable $E contains the string "X", then MyPath/$E means the same as MyPath/"X", which is illegal in XPath 1.0, and in XPath 2.0 returns as many instances of the string "X" as there are nodes in MyPath.

You probably intended MyPath/*[name()=$E]

Michael Kay
  • 156,231
  • 11
  • 92
  • 164
0

it is not possible to get the value by using syntax 'MyDataPfath/$MyVar' in . it will not recognize the proper path. suppose $MyVar has value 'Hi'. so it will be represented as 'MyDataPfath/"Hi"', this is not valid path, which you want to retrieve from the XML. to remove this limitation, You can use name() or local-name() function, that can be used as follows: or