Isaac G Sivaa
Isaac G Sivaa

Reputation: 1299

Dynamically pass variable name from another variable in XSLT

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.

Upvotes: 2

Views: 2108

Answers (1)

michael.hor257k
michael.hor257k

Reputation: 117140

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.

Upvotes: 3

Related Questions