0

I am writing a function that takes a parameter. I would like to use that parameter (which should be a string) to construct a variable name. As an example:

<xsl:function name="getVarWithName">
    <xsl:param name="name" />
    <xsl:variable name="return"><xsl:choose>
        <xsl:when test="$var$name"><xsl:value-of select="$var$name" /></xsl:when>
        <xsl:otherwise>Does Not Exist</xsl:otherwise>
    </xsl:choose></xsl:variable>
</xsl:function>

I would hope that calling the above function with the parameter sample would attempt to access the variable varsample.

Is this possible in XSL, and if so, how do I do that?

Nathan Merrill
  • 7,648
  • 5
  • 37
  • 56
  • 4
    No, that's not possible. Now tell us what problem you are trying to solve, and perhaps we can help you. – Michael Kay Apr 03 '15 at 17:35
  • @MichaelKay There are several instances in our code where we check for a variable, and if it doesn't contain anything, fall back to another similarly named variable. I was looking to make a function that would remove the duplicated code. – Nathan Merrill Apr 03 '15 at 18:22
  • So, you're tired of writing `` blocks? Welcome to the club. I suspect many of us have a function in our personal toolkits similar to the one I've written, called `value-or-default(arg1, arg2)`, that has some set of rules to determine if arg1 is a valid value, and returns arg1 if so, and if not, returns arg2. – bjimba Apr 04 '15 at 04:53
  • My usual coding pattern for that is `select="($var, $default)[1]"`. – Michael Kay Apr 04 '15 at 07:58
  • @bjimba actually, when I said "similarly named", I meant it. It would either be `variableName` or `prefix:variableName` with the same prefix every time. – Nathan Merrill Apr 04 '15 at 13:10

0 Answers0