如何使用Xslt中的条件对循环中的值求和

如何使用Xslt中的条件对循环中的值求和,xslt,sum,conditional-statements,xslt-2.0,xslt-3.0,Xslt,Sum,Conditional Statements,Xslt 2.0,Xslt 3.0,我需要一些帮助,我需要转换这个文件,但我有一个条件:当数量>0且数量value=3.50 0.002--->值=1 0.80-->值=1 总额=5.50 我尝试过这样做,但实际上我一直在思考如何做到这一点: <xsl:variable name="QTYItem"> <xsl:for-each select="$Dellist"> <xsl:variable name="QTYCountItemValu

我需要一些帮助,我需要转换这个文件,但我有一个条件:当数量>0且数量<1时,我应该替换为1,最后求和所有值

我有以下XML:

<?xml version="1.0" encoding="UTF-8" ?>
<DellistDocument>
  <DELLIST>
    <QTY>3.50</QTY>
  </DELLIST>
  <DELLIST>
    <QTY>0.002</QTY>
  </DELLIST>
  <DELLIST>
    <QTY>0.80</QTY>
  </DELLIST>
</DellistDocument>

3.50
0.002
0.80
我需要对每个数量进行循环,并在变换贴图中更改值(如果必要),我需要执行如下求和:

<QTY>3.50</QTY> ----> value = 3.50
<QTY>0.002</QTY> ----> value = 1
<QTY>0.80</QTY> ----> value = 1

Total Sum = 5.50
3.50-->value=3.50
0.002--->值=1
0.80-->值=1
总额=5.50
我尝试过这样做,但实际上我一直在思考如何做到这一点:

<xsl:variable name="QTYItem">
  <xsl:for-each select="$Dellist">
    <xsl:variable name="QTYCountItemValue" select="number(normalize-space(QTY/text()))" />
    <xsl:variable name="QTYItemValidateValue">
    
        <xsl:choose>
            <xsl:when test="$QTYCountItemValue &gt; 0 and $QTYCountItemValue &lt; 1">
              <xsl:value-of select="number(1)" />
            </xsl:when>
            <xsl:otherwise>
              <xsl:value-of select="number($QTYCountItemValue)" />
            </xsl:otherwise>
        </xsl:choose>
        
    </xsl:variable>
    
    <xsl:value-of select="sum($QTYItemValidateValue)" />
  </xsl:for-each>
  
</xsl:variable>

<xsl:value-of select="$QTYItem" />

结果是:3.5011,这是放置循环值,但我需要对所有值求和,我如何才能做到这一点?我预计结果是5.50分

我在XSLT 3.0中工作

<xsl:template match="/DellistDocument">
    <output>
        <xsl:value-of select="sum(DELLIST/(if (number(QTY) gt 0 and number(QTY) lt 1) then 1 else number(QTY)))"/>
    </output>
</xsl:template>

非常感谢。

这里有一种方法可以做到这一点。 也许有更简单的方法

不确定所需的输出是什么,但如果需要,可以修改输出格式

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:my="http://www.my.com"
    exclude-result-prefixes="xs my"
    version="3.0">

  <xsl:output method="xml" indent="yes"/>

  <xsl:template match="DellistDocument">
    <xsl:copy>
      <xsl:apply-templates/>
      <TOTAL><xsl:value-of select="my:total(DELLIST[1],0)"/></TOTAL>
    </xsl:copy>
  </xsl:template>
  
  <xsl:template match="QTY[number(.)&gt;0 and number(.)&lt;1]">
    <xsl:copy>
      <xsl:value-of select="1"/>
    </xsl:copy>
  </xsl:template>
  
  <!-- Recursive function to round the numbers and add them -->
  <xsl:function name="my:total" as="xs:double">
    <xsl:param name="currDELLIST"/>
    <xsl:param name="total"/>
    <xsl:variable name="currQTY" select="($currDELLIST/QTY)[1]"/>
    <xsl:variable name="round" select="if($currQTY&gt;0 and $currQTY&lt;1) then 1 else $currQTY"/>
    <xsl:sequence select="if($currDELLIST/following-sibling::DELLIST) 
                            then my:total($currDELLIST/following-sibling::DELLIST, $round+$total) 
                            else $round+$total"/>
  </xsl:function>
  
  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>
  
</xsl:stylesheet>


查看此处的工作情况:

无需循环求和值。在这里,您可以简单地执行以下操作:

XSLT2.0

<xsl:template match="/DellistDocument">
    <output>
        <xsl:value-of select="sum(DELLIST/(if (number(QTY) gt 0 and number(QTY) lt 1) then 1 else number(QTY)))"/>
    </output>
</xsl:template>


演示: