我正在使用XSL 1.0。目前没有可能使用2.0
我有一个变量$myVar
,我有一个变量名'myVar'
作为字符串存储在另一个变量$refName = 'myVar'
中。我可以使用xsl:value-of和$myVar
以某种方式输出$refName
变量的内容,其中包含变量名称作为字符串吗?
基本上我需要在另一个变量'$myVar'
中存储的文本的xsl:value-of的select属性中动态生成变量名$refName
。
我希望${$refName}
之类的行为像$myVar
。我被卡住了。尝试了一切。
通常我会输出这样的变量:
<xsl:variable name="myVar" select="/ns1:root/ns1:node" />
<xsl:value-of select="$myVar" />
<!-- OUTPUTS: Content of /ns1:root/ns1:node -->
这就是我想要的(它不起作用):
<xsl:variable name="myVar" select="/ns1:root/ns1:node" />
<xsl:variable name="refName" select="'myVar'" />
<xsl:value-of select="${$refName}" />
<!-- ERROR, GOAL OUTPUT: Content of $myVar, Content of /ns1:root/ns1:node -->
或者甚至更复杂我的具体问题。我想动态输出所有已定义的XSL变量。我需要load-node-from-query-string()
或parse()
或类似的功能。
当你有名字时,是否有可能输出所有XSL变量或XSL变量,并选择查询将另一个变量存储为字符串?
<xsl:for-each select="document('')//xsl:variable">
<xsl:variable name="ref_name" select="./@name" />
<xsl:variable name="ref_select" select="./@select" />
<xsl:value-of select="$ref_name" />
<!-- OUTPUTS: Variable name e.g. myVar -->
<xsl:value-of select="$ref_select" />
<!-- OUTPUTS string of select attribute e.g. '/ns1:root/ns1:node' -->
<xsl:variable name="ref_value" select="load-node-from-query-string($ref_select)" />
<xsl:value-of select="$ref_value" />
<!-- ERROR, GOAL OUTPUT: Content of /ns1:root/ns1:node -->
</xsl:for-each>