将 2 个数字相乘,然后求和

2024-03-16

我很难尝试做一些看起来应该很容易做的事情。我基本上想将一个节点中的 2 个数字相乘,然后将所有节点的这些数字相加。这是我尝试过的 XSLT 代码。

<xsl:value-of select="sum(Parts/Part/Quantity * Parts/Part/Rate)"/>

此代码会导致错误“函数 sum 的参数 1 无法转换为节点集”。

有谁知道出了什么问题或者我如何完成我想做的事情?


以下是三种可能的解决方案:

解决方案1 ​​XSLT2:

<xsl:stylesheet version="2.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="text"/>

    <xsl:template match="/">
      <xsl:sequence select="sum(/*/*/(rate * quantity))"/>
    </xsl:template>
</xsl:stylesheet>

当此转换应用于以下 XML 文档时:

<parts>
  <part>
        <rate>0.37</rate>
    <quantity>10</quantity>
  </part>
  <part>
        <rate>0.03</rate>
    <quantity>10</quantity>
  </part>
</parts>

想要的结果已产生:

4

The XSLT 2.0 http://www.w3.org/TR/xslt20/解决方案利用了以下事实:XPath 2.0 http://www.w3.org/TR/xpath20/#context最后一个“/”运算符的右参数可以是表达式,通常也可以是函数。该表达式/函数应用于迄今为止选择的作为上下文节点的每个节点,并且每个函数应用产生一个结果。

解决方案2 XSLT 1.0:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="text"/>

    <xsl:template match="/">
      <xsl:call-template name="sumProducts">
        <xsl:with-param name="pList" select="*/*"/>
      </xsl:call-template>
    </xsl:template>

    <xsl:template name="sumProducts">
        <xsl:param name="pList"/>
        <xsl:param name="pAccum" select="0"/>

        <xsl:choose>
          <xsl:when test="$pList">
            <xsl:variable name="vHead" select="$pList[1]"/>

            <xsl:call-template name="sumProducts">
              <xsl:with-param name="pList" select="$pList[position() > 1]"/>
              <xsl:with-param name="pAccum"
               select="$pAccum + $vHead/rate * $vHead/quantity"/>
            </xsl:call-template>
          </xsl:when>
          <xsl:otherwise>
            <xsl:value-of select="$pAccum"/>
          </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
</xsl:stylesheet>

当应用于上面的 XML 文档时,会产生正确的结果:

4

这是典型的 XSLT 1.0 递归解决方案. 请注意如何sumProducts模板递归调用自身,直到整个输入列表,传入参数$pList已处理。

解决方案3 FXSL(XSLT 1.0):

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ext="http://exslt.org/common"
xmlns:test-map-product="test-map-product"
exclude-result-prefixes="xsl ext test-map-product"
>
   <xsl:import href="sum.xsl"/>
   <xsl:import href="map.xsl"/>
   <xsl:import href="product.xsl"/>

   <!-- This transformation is to be applied on:
        salesMap.xml

        It contains the code of the "sum of products" from the 
        article "The Functional Programming Language XSLT"
     -->

   <test-map-product:test-map-product/>

   <xsl:output method="text"/>

   <xsl:template match="/">
     <!-- Get: map product /sales/sale -->
     <xsl:variable name="vSalesTotals">
         <xsl:variable name="vTestMap" select="document('')/*/test-map-product:*[1]"/>
         <xsl:call-template name="map">
           <xsl:with-param name="pFun" select="$vTestMap"/>
           <xsl:with-param name="pList1" select="/sales/sale"/>
         </xsl:call-template>
     </xsl:variable>

     <!-- Get sum map product /sales/sale -->
      <xsl:call-template name="sum">
        <xsl:with-param name="pList" select="ext:node-set($vSalesTotals)/*"/>
      </xsl:call-template>
   </xsl:template>

    <xsl:template name="makeproduct" match="*[namespace-uri() = 'test-map-product']">
      <xsl:param name="arg1"/>

      <xsl:call-template name="product">
        <xsl:with-param name="pList" select="$arg1/*"/>
      </xsl:call-template>
    </xsl:template>
</xsl:stylesheet>

当此转换应用于以下 XML 文档时:

<sales>
  <sale>
    <price>3.5</price>
    <quantity>2</quantity>
    <Discount>0.75</Discount>
    <Discount>0.80</Discount>
    <Discount>0.90</Discount>
  </sale>
  <sale>
    <price>3.5</price>
    <quantity>2</quantity>
    <Discount>0.75</Discount>
    <Discount>0.80</Discount>
    <Discount>0.90</Discount>
  </sale>
</sales>

产生了正确的结果:

7.5600000000000005

在最后一种情况下,对于每个sale我们计算的乘积price, quantity和所有可用的(数量可变)discount-s.

FXSL http://fxsl.sf.net 是高阶函数的纯 XSLT 实现。在此示例中,高阶函数f:map() is used映射函数f:product()在每个孩子的名单上sale元素。然后将结果相加得出最终结果。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

将 2 个数字相乘,然后求和 的相关文章

随机推荐