Xml 动态传递XSLT中另一个变量的变量名
我已经在当前XSL中定义并导入了变量列表Xml 动态传递XSLT中另一个变量的变量名,xml,xslt,Xml,Xslt,我已经在当前XSL中定义并导入了变量列表 $varA --> 10 $varB --> 20 $varC --> 30 在当前XSL中,我动态地获取'varA'或'varB'或'varC'的值,并将其保留在名为'VarDynamic'的变量中 ${$VarDynamic}未按预期工作。 我必须从另一个变量动态传递变量名 VariableList.xsl DynamicList.xsl 请您对此提出建议。您在这里遇到了一些严重的语法问题,否则它会起作用。试着这样
$varA --> 10
$varB --> 20
$varC --> 30
在当前XSL中,我动态地获取'varA'或'varB'或'varC'的值,并将其保留在名为'VarDynamic'的变量中
${$VarDynamic}未按预期工作。
我必须从另一个变量动态传递变量名
VariableList.xsl
DynamicList.xsl
请您对此提出建议。您在这里遇到了一些严重的语法问题,否则它会起作用。试着这样做: 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>
<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>
DynamicList.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>
<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>
编辑:
如果必须通过间接方式执行,请尝试:
<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>
请注意,此处不需要导入。实际上,VariableList.xsl文档可以是一个简单的XML文档。在XSLT中不能这样做。您试图解决的问题到底是什么?两次通过XSLT是一种选择吗?我必须从另一个变量读取变量名。您好。我想从另一个变量中读取变量名。这是直截了当的。@SivaaNethaji您的意思是要将变量的名称作为文本并从中获取变量的值吗?你为什么要走那条路?这一点都不简单。谢谢:)出于某种原因,我不得不用变量名保存XSL文件。你的解决方案在这里有效。谢谢:)