通过动态名称引用 XSLT 变量 [英] Reference an XSLT variable via a dynamic name
问题描述
我遇到了一个小问题.
XSL 文件:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:fn="http://www.w3.org/2005/xpath-functions">
<xsl:template match="/">
<xsl:variable name="unumericValue" select="10" />
<xsl:variable name="uanotherValue" select="8" />
<xsl:for-each select="/root/try">
<xsl:value-of select="var" />
<xsl:variable name="min"><xsl:value-of select="@minimum" /></xsl:variable>
<xsl:value-of select="@type" />
<xsl:variable name="referenceName"><xsl:value-of select='concat("u",var)' /></xsl:variable>
<xsl:value-of select="$referenceName" />
<xsl:if test='$referenceName > $min'>
<p>Do something.</p>
</xsl:if>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
XML 文件:
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="q1.xsl"?>
<root>
<try type="compare" minimum="9">
<var>numericValue</var>
<something>...</something>
</try>
<try type="compare" minimum="10">
<var>anotherValue</var>
<something>...</something>
</try>
</root>
如您所见,XML 文件有两个变量元素,它们应该与 XSLT 文件中的变量相匹配.但是我不知道哪种语法是正确的.$referenceName 只是我想使用的变量的名称.但我不知道如何将名称引用到现有变量.
As you can see the XML file has two var-Elements which should match to the variables in the XSLT-File. However I don't know which Syntax is correct. $referenceName is just the name of the variable which I want to use. But I don't know how to reference the name to the existing variable.
推荐答案
$referenceName
不是对名称为unumericValue"或其他名称的变量的引用.它只是字符串值unumericValue"等.所以它永远不会大于$min
.但是,通过一些额外的工作,有一个通过名称查找变量的技巧:
$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>
最后一种方法实际上可能是最正统的,但需要依赖于 XSLT 处理器(至少在 XSLT 1.0 中)node-set()
函数.
The last approach is probably actually the most orthodox, but requires the XSLT processor-dependent (at least in XSLT 1.0) node-set()
function.
这篇关于通过动态名称引用 XSLT 变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!