如何从一堆变量中选择最小值? [英] How to select the smallest value from a bunch of variables?
问题描述
假设我有变量 $a
、$b
、$c
和 $d
,它们都保存数字.我想获得最小(最大)值.我典型的 XSLT 1.0 方法是
Assume I have variables $a
, $b
, $c
and $d
which all hold numbers. I would like to get the smallest (largest) value. My typical XSLT 1.0 approach to this is
<xsl:variable name="minimum">
<xsl:for-each select="$a | $b | $c | $d">
<xsl:sort
select="."
data-type="number"
order="ascending" />
<xsl:if test="position()=1"><xsl:value-of select="." /></xsl:if>
</xsl:for-each>
</xsl:variable>
但是,我的 xslt 1.0 处理器抱怨
However, my xslt 1.0 processor complains with
运行时错误:文件 stylesheet.xslt 第 106 行元素 for-each
'select' 表达式不计算为节点集.
runtime error: file stylesheet.xslt line 106 element for-each
The 'select' expression does not evaluate to a node set.
如何计算给定值的最小值(最大值)?
How can I compute the minimum (maximum) of the given values?
当然,我可以使用一长串
语句并检查所有组合,但我更喜欢更小的解决方案.
Of course, I could use a long series of <xsl:when>
statements and check all combinations, but I'd rather like a smaller solution.
推荐答案
如果变量具有静态定义的值(不是动态计算的),那么可以使用 XSLT 1.0 完成以下类似操作:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:variable name="vA" select="3"/>
<xsl:variable name="vB" select="1"/>
<xsl:variable name="vC" select="9"/>
<xsl:variable name="vD" select="5"/>
<xsl:template match="/">
<xsl:for-each select=
"document('')/*/xsl:variable
[contains('|vA|vB|vC|vD|', concat('|', @name, '|'))]
/@select
">
<xsl:sort data-type="number" order="ascending"/>
<xsl:if test="position() = 1">
Smallest: <xsl:value-of select="."/>
</xsl:if>
<xsl:if test="position() = last()">
Largest: <xsl:value-of select="."/>
</xsl:if>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
当此转换应用于任何 XML 文档(未使用)时,会产生所需的正确结果:
Smallest: 1
Largest: 9
<小时>
二.现在,假设变量是动态定义的.
我们可以这样做(但需要xxx:node-set()
扩展函数):
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ext="http://exslt.org/common">
<xsl:output method="text"/>
<xsl:variable name="vA" select="number(/*/*[3])"/>
<xsl:variable name="vB" select="number(/*/*[1])"/>
<xsl:variable name="vC" select="number(/*/*[9])"/>
<xsl:variable name="vD" select="number(/*/*[5])"/>
<xsl:template match="/">
<xsl:variable name="vrtfStore">
<num><xsl:value-of select="$vA"/></num>
<num><xsl:value-of select="$vB"/></num>
<num><xsl:value-of select="$vC"/></num>
<num><xsl:value-of select="$vD"/></num>
</xsl:variable>
<xsl:for-each select="ext:node-set($vrtfStore)/*">
<xsl:sort data-type="number" order="ascending"/>
<xsl:if test="position() = 1">
Smallest: <xsl:value-of select="."/>
</xsl:if>
<xsl:if test="position() = last()">
Largest: <xsl:value-of select="."/>
</xsl:if>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
当此转换应用于以下 XML 文档时:
<nums>
<num>01</num>
<num>02</num>
<num>03</num>
<num>04</num>
<num>05</num>
<num>06</num>
<num>07</num>
<num>08</num>
<num>09</num>
<num>10</num>
</nums>
产生了想要的、正确的结果:
Smallest: 1
Largest: 9
这篇关于如何从一堆变量中选择最小值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!