$referenceName
isn't a reference to the variable with the name "unumericValue" or otherwise. It's just the string value "unumericValue", etc. So that will never be greater than $min
. However, with a little extra work, there is a trick to find a variable by its name:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:variable name="numericValue" select="10" />
<xsl:variable name="anotherValue" select="8" />
<xsl:variable name="vars" select="document('')/*/xsl:variable" />
<xsl:template match="/">
<xsl:variable name="referenceName" select="'numericValue'" />
<xsl:variable name="referenceValue" select="$vars[@name = $referenceName]/@select" />
Reference value: <xsl:value-of select="$referenceValue" />
</xsl:template>
</xsl:stylesheet>
One big limitation to note here is that this will only work for variables that are a constant numeric value.
Here's a way to simulate variables with constant string values:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:v="variables-node"
>
<v:variables>
<v:variable n="numericValue" value="10" />
<v:variable n="nonNumericValue" value="Hello World" />
</v:variables>
<xsl:variable name="vars" select="document('')//v:variables/v:variable" />
<xsl:template match="/">
<xsl:variable name="referenceName" select="'nonNumericValue'" />
<xsl:variable name="referenceValue" select="$vars[@n = $referenceName]/@value" />
<xsl:value-of select="concat('The variable with the name ', $referenceName, ' has the value ', $referenceValue)"/>
</xsl:template>
</xsl:stylesheet>
And lastly, a way to simulate variables with calculated values:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:exslt="http://exslt.org/common"
>
<xsl:variable name="varsRaw">
<var n="computedValue" value="{concat('2 + 4 is ', 2 + 4)}" />
<var n="computedNumber" value="{22 div 7}" />
</xsl:variable>
<xsl:variable name="vars" select="exslt:node-set($varsRaw)/var" />
<xsl:template match="/">
<xsl:variable name="referenceName" select="'computedValue'" />
<xsl:variable name="referenceValue" select="$vars[@n = $referenceName]/@value" />
<xsl:value-of select="concat('The variable with the name ', $referenceName, ' has the value ', $referenceValue)"/>
<xsl:value-of select="' '"/>
<xsl:variable name="referenceName2" select="'computedNumber'" />
<xsl:variable name="referenceValue2" select="$vars[@n = $referenceName2]/@value" />
<xsl:value-of select="concat('The variable with the name ', $referenceName2, ' has the value ', $referenceValue2)"/>
</xsl:template>
</xsl:stylesheet>
The last approach is probably actually the most orthodox, but requires the XSLT processor-dependent (at least in XSLT 1.0) node-set()
function.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…