2

I have list of Variables defined and imported in current XSL.

 $varA --> 10
 $varB --> 20
 $varC --> 30 

In current XSL, I get value of 'varA' or 'varB' or 'varC' dynamically and keep in variable called 'VarDynamic' ${$VarDynamic} is not working as expected. I have to pass dynamically variable name from another variable

VariableList.xsl

 <xsl:variable name="varA" select="10"></xsl:variable>
 <xsl:variable name="varB" select="20"></xsl:variable>
 <xsl:variable name="varC" select="30"></xsl:variable>

DynamicList.xsl

  <xsl:import href="VariableList.xsl"/>
  <xsl:template match="/">
  <xsl:variable name="DynamicVar">
      <xsl:choose>
          <xsl:when test="/A">
              <xsl:value-of select="'varA'"/>
          </xsl:when>
          <xsl:when test="/B">
              <xsl:value-of select="'varB'"/>
          </xsl:when>
          <xsl:otherwise>
              <xsl:value-of select="'varC'"/>
          </xsl:otherwise>
      </xsl:choose>
  </xsl:variable>

  <Result>
      <xsl:value-ofselect="${$DynamicVar}"/>
  </Result>

Can you please advise on this.

3
  • 2
    You can't do that in XSLT. What exactly is the problem you are trying to solve? Commented May 27, 2014 at 20:44
  • Would a two-pass XSLT be an option? Commented May 27, 2014 at 20:56
  • I have to read variable name from another variable. Commented May 27, 2014 at 21:42

1 Answer 1

3

You have some serious syntax issues here, otherwise it would work. Try it this way:

VariableList.xsl

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:variable name="varA" select="10"></xsl:variable>
    <xsl:variable name="varB" select="20"></xsl:variable>
    <xsl:variable name="varC" select="30"></xsl:variable>
</xsl:stylesheet>

DynamicList.xsl

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:import href="VariableList.xsl"/>
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:template match="/">
    <xsl:variable name="DynamicVar">
        <xsl:choose>
            <xsl:when test="/A">
                <xsl:value-of select="$varA"/>
            </xsl:when>
            <xsl:when test="/B">
                <xsl:value-of select="$varB"/>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="$varC"/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:variable>
    <Result>
        <xsl:value-of select="$DynamicVar"/>
    </Result>
</xsl:template>

</xsl:stylesheet>

Edit:

If you must do it by indirection, then try:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:template match="/">
    <xsl:variable name="DynamicVar">
        <xsl:choose>
            <xsl:when test="/A">
                <xsl:value-of select="'varA'"/>
            </xsl:when>
            <xsl:when test="/B">
                <xsl:value-of select="'varB'"/>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="'varC'"/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:variable>
    <Result>
        <xsl:value-of select="document('VariableList.xsl')/xsl:stylesheet/xsl:variable[@name=$DynamicVar]/@select"/>
    </Result>
</xsl:template>

</xsl:stylesheet>

Note that import is not required here. Actually, the VariableList.xsl document could be a simple XML document.

Sign up to request clarification or add additional context in comments.

3 Comments

Hi. I would like to read variable name from another variable. This is straight forward.
@SivaaNethaji Do you mean you want to take the variable's name as text and get the variable's value from that? Why would you go that route? It's not straightforward at all.
Thanks :) For some reason I have to keep XSL file with variable name. Your solution works here. Thanks :)

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.