Search code examples
dynamicevaluatexslt-3.0

xslt 3.0 xsl:evaluate example


For the following xml document:

<company>
   <employee>
      <name>John Andrews</name>
      <age>23</age>
      <salary>4000</salary>
      <division>Accounting</division>
   </employee>
</company>

I have the xsl like

<xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xsl:output method="text"/>

  <xsl:template match="/">
    <xsl:value-of select="company/employee/name"/>

    <xsl:variable name="test">
      <xsl:text>company/employee/name</xsl:text>
    </xsl:variable>

    <xsl:evaluate xpath="$test"/>

  </xsl:template>
</xsl:stylesheet>

How can I use $test variable in xsl:evaluate in order to get the same result as in the:

<xsl:value-of select="company/employee/name"/>

Is it possible?


Solution

  • You need to set the context item with e.g.

        <xsl:evaluate xpath="$test" context-item="."/>