Xslt 如何从一组变量中选择最小值?

Xslt 如何从一组变量中选择最小值?,xslt,xslt-1.0,minimum,Xslt,Xslt 1.0,Minimum,假设我有变量$a,$b,$c和$d,它们都包含数字。我想得到最小(最大)值。我的典型XSLT1.0方法是 <xsl:variable name="minimum"> <xsl:for-each select="$a | $b | $c | $d"> <xsl:sort select="." data-type="number" order="ascending" /> <xsl:if test="p

假设我有变量
$a
$b
$c
$d
,它们都包含数字。我想得到最小(最大)值。我的典型XSLT1.0方法是

<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>

但是,我的xslt1.0处理器对

运行时错误:每个
“select”表达式的计算结果不是节点集

如何计算给定值的最小值(最大值)



当然,我可以使用一长串的
语句来检查所有的组合,但我更喜欢一个较小的解决方案。

我从来没有在1.0中这样做过(我使用2.0),但您可以这样做:

  <xsl:variable name="minimum">
    <xsl:choose>
      <xsl:when test="$b > $a and $c > $a and $d > $a"><xsl:value-of select="$a"/></xsl:when>
      <xsl:when test="$a > $b and $c > $b and $d > $b"><xsl:value-of select="$b"/></xsl:when>
      <xsl:when test="$b > $c and $a > $c and $d > $c"><xsl:value-of select="$c"/></xsl:when>
      <xsl:when test="$b > $d and $c > $d and $a > $d"><xsl:value-of select="$d"/></xsl:when>
    </xsl:choose>
  </xsl:variable>


但是肯定有更好的方法。

如果变量具有静态定义的值(不是动态计算的),那么可以使用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>
   Smallest: 1
   Largest: 9

II。现在,假设变量是动态定义的

我们可以这样做(但需要
xxx:node-set()
扩展函数):


此XSLT1.0解决方案使用递归模板来解析带分隔符的值列表,以从列表中返回最小/最大值

<?xml version="1.0" encoding="UTF-8"?>
<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:variable name="a" select="'3'"/>
    <xsl:variable name="b" select="'1'"/>
    <xsl:variable name="c" select="'9'"/>
    <xsl:variable name="d" select="'5'"/>

    <xsl:template match="/">
        <xsl:text>&#xa;Smallest: </xsl:text>
        <xsl:call-template name="min">
            <xsl:with-param name="values" select="concat($a,',',$b,',',$c,',',$d)"/>    
        </xsl:call-template>

        <xsl:text>&#xa;Largest: </xsl:text>
        <xsl:call-template name="max">
            <xsl:with-param name="values" select="concat($a,',',$b,',',$c,',',$d)"/>    
        </xsl:call-template>
    </xsl:template>

    <xsl:template name="min">
        <xsl:param name="values" />
        <xsl:param name="delimiter" select="','"/>
        <xsl:param name="min"/>

        <xsl:variable name="currentValue" >
            <xsl:choose>
                <xsl:when test="contains($values, $delimiter)">
                    <xsl:value-of select="substring-before($values,$delimiter)"/>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="$values"/>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:variable>

        <xsl:variable name="minimumValue">
            <xsl:choose>
                <xsl:when test="$min and $min > $currentValue">
                    <xsl:value-of select="$currentValue"/>
                </xsl:when>
                <xsl:when test="$min">
                    <xsl:value-of select="$min"/>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="$currentValue" />
                </xsl:otherwise>
            </xsl:choose>
        </xsl:variable>

       <xsl:choose>
            <xsl:when test="substring-after($values,$delimiter)">
                <xsl:call-template name="min">
                    <xsl:with-param name="min" select="$minimumValue" />
                    <xsl:with-param name="values" select="substring-after($values,$delimiter)" />
                    <xsl:with-param name="delimiter" select="$delimiter"/>
                </xsl:call-template>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="$minimumValue" />
            </xsl:otherwise>
        </xsl:choose>                
    </xsl:template>


    <xsl:template name="max">
        <xsl:param name="values" />
        <xsl:param name="delimiter" select="','"/>
        <xsl:param name="max"/>

        <xsl:variable name="currentValue" >
            <xsl:choose>
                <xsl:when test="contains($values, $delimiter)">
                    <xsl:value-of select="substring-before($values,$delimiter)"/>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="$values"/>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:variable>

        <xsl:variable name="maximumValue">
            <xsl:choose>
                <xsl:when test="$max and $currentValue > $max">
                    <xsl:value-of select="$currentValue"/>
                </xsl:when>
                <xsl:when test="$max">
                    <xsl:value-of select="$max"/>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="$currentValue" />
                </xsl:otherwise>
            </xsl:choose>
        </xsl:variable>

        <xsl:choose>
            <xsl:when test="substring-after($values,$delimiter)">
                <xsl:call-template name="max">
                    <xsl:with-param name="max" select="$maximumValue" />
                    <xsl:with-param name="values" select="substring-after($values,$delimiter)" />
                    <xsl:with-param name="delimiter" select="$delimiter"/>
                </xsl:call-template>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="$maximumValue" />
            </xsl:otherwise>
        </xsl:choose>                
    </xsl:template>
</xsl:stylesheet>

我认为主要的问题是数字不是节点。我认为主要的问题是在XSLT1.0中,
select
中的表达式必须计算为节点集。我的想法完全正确。正如我所说,我已经把这作为应急计划。但是这不好,而且肯定不能缩放。@bitmask-我完全错过了你问题的最后一段,你说你可以这样做。希望有人(@DimitreNovatchev)会有一个更好的解决方案?它必须重新解析它吗?另一个花哨的把戏,谢谢+1@PavelVeller:是的,如果希望在XSLT样式表本身而不是在单独的文件中具有内联数据,则通常使用此选项。是的,重新解析是在执行时完成的,这不是很有效。您可能也有兴趣看到解决方案的第二部分。第二部分起作用,谢谢。我的变量本身就是输入树上更复杂表达式的极小值,因此肯定不是静态的。
<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
<?xml version="1.0" encoding="UTF-8"?>
<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:variable name="a" select="'3'"/>
    <xsl:variable name="b" select="'1'"/>
    <xsl:variable name="c" select="'9'"/>
    <xsl:variable name="d" select="'5'"/>

    <xsl:template match="/">
        <xsl:text>&#xa;Smallest: </xsl:text>
        <xsl:call-template name="min">
            <xsl:with-param name="values" select="concat($a,',',$b,',',$c,',',$d)"/>    
        </xsl:call-template>

        <xsl:text>&#xa;Largest: </xsl:text>
        <xsl:call-template name="max">
            <xsl:with-param name="values" select="concat($a,',',$b,',',$c,',',$d)"/>    
        </xsl:call-template>
    </xsl:template>

    <xsl:template name="min">
        <xsl:param name="values" />
        <xsl:param name="delimiter" select="','"/>
        <xsl:param name="min"/>

        <xsl:variable name="currentValue" >
            <xsl:choose>
                <xsl:when test="contains($values, $delimiter)">
                    <xsl:value-of select="substring-before($values,$delimiter)"/>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="$values"/>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:variable>

        <xsl:variable name="minimumValue">
            <xsl:choose>
                <xsl:when test="$min and $min > $currentValue">
                    <xsl:value-of select="$currentValue"/>
                </xsl:when>
                <xsl:when test="$min">
                    <xsl:value-of select="$min"/>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="$currentValue" />
                </xsl:otherwise>
            </xsl:choose>
        </xsl:variable>

       <xsl:choose>
            <xsl:when test="substring-after($values,$delimiter)">
                <xsl:call-template name="min">
                    <xsl:with-param name="min" select="$minimumValue" />
                    <xsl:with-param name="values" select="substring-after($values,$delimiter)" />
                    <xsl:with-param name="delimiter" select="$delimiter"/>
                </xsl:call-template>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="$minimumValue" />
            </xsl:otherwise>
        </xsl:choose>                
    </xsl:template>


    <xsl:template name="max">
        <xsl:param name="values" />
        <xsl:param name="delimiter" select="','"/>
        <xsl:param name="max"/>

        <xsl:variable name="currentValue" >
            <xsl:choose>
                <xsl:when test="contains($values, $delimiter)">
                    <xsl:value-of select="substring-before($values,$delimiter)"/>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="$values"/>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:variable>

        <xsl:variable name="maximumValue">
            <xsl:choose>
                <xsl:when test="$max and $currentValue > $max">
                    <xsl:value-of select="$currentValue"/>
                </xsl:when>
                <xsl:when test="$max">
                    <xsl:value-of select="$max"/>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="$currentValue" />
                </xsl:otherwise>
            </xsl:choose>
        </xsl:variable>

        <xsl:choose>
            <xsl:when test="substring-after($values,$delimiter)">
                <xsl:call-template name="max">
                    <xsl:with-param name="max" select="$maximumValue" />
                    <xsl:with-param name="values" select="substring-after($values,$delimiter)" />
                    <xsl:with-param name="delimiter" select="$delimiter"/>
                </xsl:call-template>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="$maximumValue" />
            </xsl:otherwise>
        </xsl:choose>                
    </xsl:template>
</xsl:stylesheet>
Smallest: 1
Largest: 9