When I try to use the code below I get a duplicate variable error because variables are immutable. How do I set the smaller of the two variables ($nextSubPartPos
and $nextQuestionStemPos
) as my new variable ($nextQuestionPos
)?
当我尝试使用下面的代码时,我得到一个重复的变量错误,因为变量是不可变的。如何将两个变量中较小的一个($ nextSubPartPos和$ nextQuestionStemPos)设置为我的新变量($ nextQuestionPos)?
<xsl:variable name="nextQuestionPos"/>
<xsl:choose>
<xsl:when test="$nextSubPartPos < $nextQuestionStemPos">
<xsl:variable name="nextQuestionPos" select="$nextSubPartPos"/>
</xsl:when>
<xsl:otherwise>
<xsl:variable name="nextQuestionPos" select="$nextSubPartPos"/>
</xsl:otherwise>
</xsl:choose>
4 个解决方案
#1
3
Don't close the xsl:variable node in the first line. That is, take the / out of it, then put an </xsl:variable>
after </xsl:choose>
. Next, change the <xsl:variable>
nodes inside the choose to <xsl:value-of>
nodes.
不要在第一行中关闭xsl:variable节点。也就是说,取出/取出它,然后在
之后放一个
。接下来,更改选择
That is, you want to set the value of the variable with the choose. There are two ways to set the value of a variable. One is the select attribute, the other is the inner text of the node.
也就是说,您希望使用choose设置变量的值。有两种方法可以设置变量的值。一个是select属性,另一个是节点的内部文本。
<xsl:variable name="nextQuestionPos">
<xsl:choose>
<xsl:when test="$nextSubPartPos < $nextQuestionStemPos">
<xsl:value-of select="$nextSubPartPos"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$nextSubPartPos"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
#2
5
A compact XPath 1.0 expression that evaluates to the smaller value is:
一个评估为较小值的紧凑型XPath 1.0表达式是:
$v1*($v2 >= $v1) + $v2*($v1 > $v2)
$ v1 *($ v2> = $ v1)+ $ v2 *($ v1> $ v2)
where the $v1 and $v2 variables contain the values to be compared.
其中$ v1和$ v2变量包含要比较的值。
So, an elegant one-liner XSLT 1.0 solution will look like this:
因此,优雅的单行XSLT 1.0解决方案将如下所示:
<xsl:variable name="v3" select="$v1*($v2 >= $v1) +$v2*($v1 > $v2)"/>
It is easier to define a variable as required in XSLT 2.0:
在XSLT 2.0中根据需要定义变量更容易:
One can use either the following (more readable) one-liner:
可以使用以下(更易读)单行:
if($v2 gt $v1)
then $v1
else $v2
if($ v2 gt $ v1)则$ v1 else $ v2
Or the more compact:
或者更紧凑:
min(($v1, $v2))
#3
2
Just use the min
function:
只需使用min函数:
<xsl:variable name="a" select="42" />
<xsl:variable name="b" select="23" />
<xsl:variable name="x" select="min(($a,$b))" />
In your example, replace the entire code with:
在您的示例中,将整个代码替换为:
<xsl:variable name="nextQuestionPos" select="min(($nextSubPartPos,$nextQuestionStemPos))" />
Saxon implements min in the global namespace. Other processors may require a namespace, the correct one (usually denoted fn
) is http://www.w3.org/2005/02/xpath-functions
.
Saxon在全局命名空间中实现min。其他处理器可能需要命名空间,正确的(通常表示为fn)是http://www.w3.org/2005/02/xpath-functions。
#4
0
Variables in XSLT are immutable. This has tripped me up so many times.
XSLT中的变量是不可变的。这让我绊倒了很多次。
#1
3
Don't close the xsl:variable node in the first line. That is, take the / out of it, then put an </xsl:variable>
after </xsl:choose>
. Next, change the <xsl:variable>
nodes inside the choose to <xsl:value-of>
nodes.
不要在第一行中关闭xsl:variable节点。也就是说,取出/取出它,然后在
之后放一个
。接下来,更改选择
That is, you want to set the value of the variable with the choose. There are two ways to set the value of a variable. One is the select attribute, the other is the inner text of the node.
也就是说,您希望使用choose设置变量的值。有两种方法可以设置变量的值。一个是select属性,另一个是节点的内部文本。
<xsl:variable name="nextQuestionPos">
<xsl:choose>
<xsl:when test="$nextSubPartPos < $nextQuestionStemPos">
<xsl:value-of select="$nextSubPartPos"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$nextSubPartPos"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
#2
5
A compact XPath 1.0 expression that evaluates to the smaller value is:
一个评估为较小值的紧凑型XPath 1.0表达式是:
$v1*($v2 >= $v1) + $v2*($v1 > $v2)
$ v1 *($ v2> = $ v1)+ $ v2 *($ v1> $ v2)
where the $v1 and $v2 variables contain the values to be compared.
其中$ v1和$ v2变量包含要比较的值。
So, an elegant one-liner XSLT 1.0 solution will look like this:
因此,优雅的单行XSLT 1.0解决方案将如下所示:
<xsl:variable name="v3" select="$v1*($v2 >= $v1) +$v2*($v1 > $v2)"/>
It is easier to define a variable as required in XSLT 2.0:
在XSLT 2.0中根据需要定义变量更容易:
One can use either the following (more readable) one-liner:
可以使用以下(更易读)单行:
if($v2 gt $v1)
then $v1
else $v2
if($ v2 gt $ v1)则$ v1 else $ v2
Or the more compact:
或者更紧凑:
min(($v1, $v2))
#3
2
Just use the min
function:
只需使用min函数:
<xsl:variable name="a" select="42" />
<xsl:variable name="b" select="23" />
<xsl:variable name="x" select="min(($a,$b))" />
In your example, replace the entire code with:
在您的示例中,将整个代码替换为:
<xsl:variable name="nextQuestionPos" select="min(($nextSubPartPos,$nextQuestionStemPos))" />
Saxon implements min in the global namespace. Other processors may require a namespace, the correct one (usually denoted fn
) is http://www.w3.org/2005/02/xpath-functions
.
Saxon在全局命名空间中实现min。其他处理器可能需要命名空间,正确的(通常表示为fn)是http://www.w3.org/2005/02/xpath-functions。
#4
0
Variables in XSLT are immutable. This has tripped me up so many times.
XSLT中的变量是不可变的。这让我绊倒了很多次。