Dynamic XPath evaluation is not possible in pure XSLT 1.0 or 2.0.
There are at least three ways to do this in a “hybrid” solution:
I. Use the EXSLT function dyn:evaluate()
Unfortunately, very few XSLT 1.0 processors implement dyn:evaluate()
.
II. Process the XML document with XSLT and generate a new XSLT file that contains the XPath expressions — then execute the newly-generated transformation.
Very few people do this and, in my opinion, this is more complex than the next solution.
III. The way the XPath Visualizer works
The idea is:
-
Have a global variable in the XSLT stylesheet defined like this:
<xsl:variable name="vExpression" select="dummy"/>
-
Then, load the stylesheet as an XML document using DOM, and replace the
select
attribute of thevExpression
variable with the actual XPath expression that is contained in the source XML document. -
Finally, initiate the transformation using the loaded into memory and dynamically updated xslt stylesheet.
IV. With XSLT 3.0
Use the <xsl:evaluate> instruction