tags:

views:

86

answers:

2

The XML file name is specific but I need to build a dynamic path. I have tried using a variable to build the path but it didn't work:

<xsl:variable name="path">
   ...conditional code
</xsl:variable>     <xsl:value-of select="document('myXML.xml')/worksheets/$path"/> 

2.0 solutions ok.

+1  A: 

You need an extension function, XPath 2.0 does not support dynamic compilation/evaluation. Saxon has saxon:evaluate. Even if your processor does not support such function you might be able to implement it yourself as an extension function.

Max Toro
I can't seem to be able to get the saxon:evaluate() function to work with a document() function. I get a static error xPath xPression.<xsl:value-of select="document('myxml.xml')/saxon:evaluate($formPath)"/>
johkar
@johkar: Your XPath expression is wrong, you should do something like this: `saxon:evaluate(concat('document(\'myxml.xml\')/', $formPath))`
Max Toro
@johkar: Also, the saxon:evaluate function is avaliable on Professional and Enterprise editions.
Max Toro
Thanks, that did it.
johkar
+1  A: 

Evaluation of any dynamically-generated XPath expression is not supported by the XSLT 1.0 or XSLT 2.0 standards. It will be supported in XSLT 2.1.

If the dynamically-generated XPath expression is not too complex, the technique in this answer can be used successfully:

http://stackoverflow.com/questions/3015942/retrieving-xml-node-from-a-path-specified-in-an-attribute-value-of-another-node/3017752#3017752

Dimitre Novatchev
Dimitre's solution is very cool for simple expressions. I also added an example to that topic using EXLT's dynamic:evaluate function, which should be essentially the same as the saxon:evaluate solution that Max mentions in his answer: http://stackoverflow.com/questions/3015942/retrieving-xml-node-from-a-path-specified-in-an-attribute-value-of-another-node/3017752#answer-3017543
Owen S.