16

I'm having issues with xsl:variable. I want to create a variable with a value that depends on the value of another XML node attribute. This working good. But when I try to create a variable with a string value that represents XPath, it just doesn't work when I try to use it as XPath in a later XSL tag.

<xsl:variable name="test">  
  <xsl:choose>
    <xsl:when test="node/@attribute=0">string/represent/xpath/1</xsl:when>
    <xsl:otherwise>string/represent/xpath/2</xsl:otherwise>
  </xsl:choose>       
</xsl:variable>                 
<xsl:for-each select="$test">
  [...]
</xsl:for-each>

I tried: How to use xsl variable in xsl if and trouble with xsl:for-each selection using xsl:variable. But with no results.

0

3 Answers 3

14

Dynamic evaluation of an XPath expression is generally not supported in XSLT (both 1.0 and 2.0), however:

We can implement a rather general dynamic XPath evaluator if we only restrict each location path to be an element name:

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

 <xsl:param name="inputId" select="'param/yyy/value'"/>

 <xsl:variable name="vXpathExpression"
  select="concat('root/meta/url_params/', $inputId)"/>

 <xsl:template match="/">
  <xsl:value-of select="$vXpathExpression"/>: <xsl:text/>

  <xsl:call-template name="getNodeValue">
    <xsl:with-param name="pExpression"
         select="$vXpathExpression"/>
  </xsl:call-template>
 </xsl:template>

 <xsl:template name="getNodeValue">
   <xsl:param name="pExpression"/>
   <xsl:param name="pCurrentNode" select="."/>

   <xsl:choose>
    <xsl:when test="not(contains($pExpression, '/'))">
      <xsl:value-of select="$pCurrentNode/*[name()=$pExpression]"/>
    </xsl:when>
    <xsl:otherwise>
      <xsl:call-template name="getNodeValue">
        <xsl:with-param name="pExpression"
          select="substring-after($pExpression, '/')"/>
        <xsl:with-param name="pCurrentNode" select=
        "$pCurrentNode/*[name()=substring-before($pExpression, '/')]"/>
      </xsl:call-template>
    </xsl:otherwise>
   </xsl:choose>
 </xsl:template>
</xsl:stylesheet>

when this transformation is applied on this XML document:

<root>
  <meta>
    <url_params>
      <param>
        <xxx>
          <value>5</value>
        </xxx>
      </param>
      <param>
        <yyy>
          <value>8</value>
        </yyy>
      </param>
    </url_params>
  </meta>
</root>

the wanted, correct result is produced:

root/meta/url_params/param/yyy/value: 8
0
12

If those path are known in advance like this case, then you can use:

<xsl:variable name="vCondition" select="node/@attribute = 0"/>
<xsl:variable name="test" select="actual/path[$vCondition] |
                                  other/actual/path[not($vCondition)]"/>
1
  • 1
    thanks. this is not exactly what i was asking, but it exactly what i was needed)
    – Igor Milla
    Jan 13, 2011 at 17:53
2

This is not natively possible in XSLT 1.0, but you can use an extension library such as dyn:

http://www.exslt.org/dyn/functions/evaluate/dyn.evaluate.html

The dyn:evaluate function evaluates a string as an XPath expression.

1
  • I tried to use the dyn:evaluate in NP++ after following the instructions, but it didn't work. Please help to clarify how to use this function. I tired to run sample here but no luck: freeformatter.com/xsl-transformer.html
    – tarekahf
    Jul 28, 2019 at 7:46

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.